2014-06-30 04:17:04 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
2015-01-16 18:05:44 +01:00
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2014-06-30 04:17:04 +02:00
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package itdelatrisu.opsu.objects;
|
|
|
|
|
2015-01-30 02:36:23 +01:00
|
|
|
import itdelatrisu.opsu.GameData;
|
2014-07-04 22:41:52 +02:00
|
|
|
import itdelatrisu.opsu.GameImage;
|
2014-07-16 22:01:36 +02:00
|
|
|
import itdelatrisu.opsu.GameMod;
|
2014-06-30 04:17:04 +02:00
|
|
|
import itdelatrisu.opsu.OsuHitObject;
|
2014-07-02 01:32:03 +02:00
|
|
|
import itdelatrisu.opsu.Utils;
|
2015-01-08 01:29:51 +01:00
|
|
|
import itdelatrisu.opsu.audio.MusicController;
|
|
|
|
import itdelatrisu.opsu.audio.SoundController;
|
|
|
|
import itdelatrisu.opsu.audio.SoundEffect;
|
2014-06-30 04:17:04 +02:00
|
|
|
import itdelatrisu.opsu.states.Game;
|
|
|
|
|
|
|
|
import org.newdawn.slick.Color;
|
|
|
|
import org.newdawn.slick.GameContainer;
|
|
|
|
import org.newdawn.slick.Graphics;
|
|
|
|
import org.newdawn.slick.Image;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data type representing a spinner object.
|
|
|
|
*/
|
2015-01-16 09:47:37 +01:00
|
|
|
public class Spinner implements HitObject {
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Container dimensions. */
|
2014-06-30 04:17:04 +02:00
|
|
|
private static int width, height;
|
|
|
|
|
2015-03-06 05:33:40 +01:00
|
|
|
|
|
|
|
// Currently it takes about 200ms to spin up (4 * 50)
|
2015-02-14 07:07:17 +01:00
|
|
|
/** The number of rotation velocities to store. */
|
|
|
|
private static final int MAX_ROTATION_VELOCITIES = 50;
|
|
|
|
|
2015-03-06 05:33:40 +01:00
|
|
|
/** The amount of time in ms before another velocity is stored */
|
|
|
|
private static final int DELTA_UPDATE_TIME = 4;
|
|
|
|
|
|
|
|
/** The amount of time in ms to fade in the spinner*/
|
|
|
|
private static final float FADE_IN_TIME = 500;
|
|
|
|
|
|
|
|
/** The remaining amount of time that was not used */
|
|
|
|
private int deltaOverflow;
|
|
|
|
|
2015-02-14 07:07:17 +01:00
|
|
|
/** PI constants. */
|
|
|
|
private static final float TWO_PI = (float) (Math.PI * 2);
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The associated OsuHitObject. */
|
2014-06-30 04:17:04 +02:00
|
|
|
private OsuHitObject hitObject;
|
|
|
|
|
2015-01-27 09:19:39 +01:00
|
|
|
/** The associated GameData object. */
|
|
|
|
private GameData data;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The last rotation angle. */
|
2015-02-14 00:12:04 +01:00
|
|
|
private float lastAngle = 0f;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The current number of rotations. */
|
2014-06-30 04:17:04 +02:00
|
|
|
private float rotations = 0f;
|
2015-03-06 05:33:40 +01:00
|
|
|
|
|
|
|
/** The current rotation to draw */
|
|
|
|
private float drawRotation = 0f;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The total number of rotations needed to clear the spinner. */
|
2014-06-30 04:17:04 +02:00
|
|
|
private float rotationsNeeded;
|
2015-02-14 07:07:17 +01:00
|
|
|
|
|
|
|
/** The sum of all the velocities in storedVelocities. */
|
|
|
|
private float sumVelocity = 0f;
|
|
|
|
|
|
|
|
/** Array holding the most recent rotation velocities. */
|
|
|
|
private float[] storedVelocities = new float[MAX_ROTATION_VELOCITIES];
|
|
|
|
|
2015-02-14 00:12:04 +01:00
|
|
|
/** True if the mouse cursor is pressed. */
|
|
|
|
private boolean isSpinning;
|
2015-02-14 07:07:17 +01:00
|
|
|
|
|
|
|
/** Current index of the stored velocities in rotations/second. */
|
|
|
|
private int velocityIndex = 0;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the Spinner data type with images and dimensions.
|
|
|
|
* @param container the game container
|
|
|
|
*/
|
2015-01-22 00:56:53 +01:00
|
|
|
public static void init(GameContainer container) {
|
2014-06-30 04:17:04 +02:00
|
|
|
width = container.getWidth();
|
|
|
|
height = container.getHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @param hitObject the associated OsuHitObject
|
|
|
|
* @param game the associated Game object
|
2015-01-27 09:19:39 +01:00
|
|
|
* @param data the associated GameData object
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2015-01-27 09:19:39 +01:00
|
|
|
public Spinner(OsuHitObject hitObject, Game game, GameData data) {
|
2014-06-30 04:17:04 +02:00
|
|
|
this.hitObject = hitObject;
|
2015-01-27 09:19:39 +01:00
|
|
|
this.data = data;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
// calculate rotations needed
|
2015-01-27 09:19:39 +01:00
|
|
|
float spinsPerMinute = 100 + (data.getDifficulty() * 15);
|
2014-07-18 21:11:57 +02:00
|
|
|
rotationsNeeded = spinsPerMinute * (hitObject.getEndTime() - hitObject.getTime()) / 60000f;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
2015-01-16 21:44:13 +01:00
|
|
|
@Override
|
2015-01-16 09:47:37 +01:00
|
|
|
public void draw(int trackPosition, boolean currentObject, Graphics g) {
|
2014-07-18 21:11:57 +02:00
|
|
|
int timeDiff = hitObject.getTime() - trackPosition;
|
2014-06-30 04:17:04 +02:00
|
|
|
boolean spinnerComplete = (rotations >= rotationsNeeded);
|
|
|
|
|
2015-03-06 05:33:40 +01:00
|
|
|
// only draw spinners if
|
|
|
|
if (timeDiff - FADE_IN_TIME > 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
// darken screen
|
2015-03-06 05:33:40 +01:00
|
|
|
float alpha = Utils.clamp(1 - timeDiff / FADE_IN_TIME, 0f, 1f);
|
|
|
|
if (timeDiff > 0) {
|
|
|
|
Color c = new Color(Utils.COLOR_BLACK_ALPHA);
|
|
|
|
c.a *= alpha;
|
|
|
|
g.setColor(c);
|
|
|
|
} else
|
|
|
|
g.setColor(Utils.COLOR_BLACK_ALPHA);
|
2014-06-30 04:17:04 +02:00
|
|
|
g.fillRect(0, 0, width, height);
|
|
|
|
|
2015-02-20 22:56:41 +01:00
|
|
|
int rpm = Math.abs(Math.round(sumVelocity / storedVelocities.length * 60));
|
|
|
|
Image rpmImg = GameImage.SPINNER_RPM.getImage();
|
2015-03-06 05:33:40 +01:00
|
|
|
rpmImg.setAlpha(alpha);
|
2015-02-20 22:56:41 +01:00
|
|
|
rpmImg.drawCentered(width / 2f, height - rpmImg.getHeight() / 2f);
|
2015-03-06 05:33:40 +01:00
|
|
|
if(timeDiff < 0)
|
2015-02-20 22:56:41 +01:00
|
|
|
data.drawSymbolString(Integer.toString(rpm), (int) ((width + rpmImg.getWidth() * 0.95f) / 2f),
|
2015-03-04 07:30:28 +01:00
|
|
|
(int) (height - data.getScoreSymbolImage('0').getHeight() * 1.025f), 1f, 1f, true);
|
2015-02-20 22:56:41 +01:00
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
// spinner meter (subimage)
|
2014-07-04 22:41:52 +02:00
|
|
|
Image spinnerMetre = GameImage.SPINNER_METRE.getImage();
|
2014-06-30 04:17:04 +02:00
|
|
|
int spinnerMetreY = (spinnerComplete) ? 0 : (int) (spinnerMetre.getHeight() * (1 - (rotations / rotationsNeeded)));
|
|
|
|
Image spinnerMetreSub = spinnerMetre.getSubImage(
|
|
|
|
0, spinnerMetreY,
|
|
|
|
spinnerMetre.getWidth(), spinnerMetre.getHeight() - spinnerMetreY
|
|
|
|
);
|
2015-03-06 05:33:40 +01:00
|
|
|
spinnerMetreSub.setAlpha(alpha);
|
2014-06-30 04:17:04 +02:00
|
|
|
spinnerMetreSub.draw(0, height - spinnerMetreSub.getHeight());
|
|
|
|
|
|
|
|
// main spinner elements
|
2015-03-06 05:33:40 +01:00
|
|
|
float approachScale = 1 - Utils.clamp( ((float) timeDiff / (hitObject.getTime() - hitObject.getEndTime())), 0f, 1f);
|
|
|
|
|
|
|
|
GameImage.SPINNER_CIRCLE.getImage().setAlpha(alpha);
|
|
|
|
GameImage.SPINNER_CIRCLE.getImage().setRotation(drawRotation * 360f);
|
2014-07-04 22:41:52 +02:00
|
|
|
GameImage.SPINNER_CIRCLE.getImage().drawCentered(width / 2, height / 2);
|
2015-03-06 05:33:40 +01:00
|
|
|
Image approachCircleScaled = GameImage.SPINNER_APPROACHCIRCLE.getImage().getScaledCopy(approachScale);
|
|
|
|
approachCircleScaled.setAlpha(alpha);
|
|
|
|
approachCircleScaled.drawCentered(width / 2, height / 2);
|
|
|
|
GameImage.SPINNER_SPIN.getImage().setAlpha(alpha);
|
2014-07-04 22:41:52 +02:00
|
|
|
GameImage.SPINNER_SPIN.getImage().drawCentered(width / 2, height * 3 / 4);
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
if (spinnerComplete) {
|
2014-07-04 22:41:52 +02:00
|
|
|
GameImage.SPINNER_CLEAR.getImage().drawCentered(width / 2, height / 4);
|
2014-06-30 04:17:04 +02:00
|
|
|
int extraRotations = (int) (rotations - rotationsNeeded);
|
|
|
|
if (extraRotations > 0)
|
2015-01-27 09:19:39 +01:00
|
|
|
data.drawSymbolNumber(extraRotations * 1000, width / 2, height * 2 / 3, 1.0f);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-16 21:44:13 +01:00
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
/**
|
|
|
|
* Calculates and sends the spinner hit result.
|
2015-01-27 09:19:39 +01:00
|
|
|
* @return the hit result (GameData.HIT_* constants)
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2015-01-16 09:47:37 +01:00
|
|
|
private int hitResult() {
|
2014-06-30 04:17:04 +02:00
|
|
|
// TODO: verify ratios
|
|
|
|
int result;
|
|
|
|
float ratio = rotations / rotationsNeeded;
|
|
|
|
if (ratio >= 1.0f ||
|
2014-07-16 22:01:36 +02:00
|
|
|
GameMod.AUTO.isActive() || GameMod.SPUN_OUT.isActive()) {
|
2015-01-27 09:19:39 +01:00
|
|
|
result = GameData.HIT_300;
|
2015-01-08 01:29:51 +01:00
|
|
|
SoundController.playSound(SoundEffect.SPINNEROSU);
|
2015-02-20 22:56:41 +01:00
|
|
|
} else if (ratio >= 0.9f)
|
2015-01-27 09:19:39 +01:00
|
|
|
result = GameData.HIT_100;
|
2015-02-20 22:56:41 +01:00
|
|
|
else if (ratio >= 0.75f)
|
2015-01-27 09:19:39 +01:00
|
|
|
result = GameData.HIT_50;
|
2014-06-30 04:17:04 +02:00
|
|
|
else
|
2015-01-27 09:19:39 +01:00
|
|
|
result = GameData.HIT_MISS;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2015-01-27 09:19:39 +01:00
|
|
|
data.hitResult(hitObject.getEndTime(), result, width / 2, height / 2,
|
2015-03-01 19:58:03 +01:00
|
|
|
Color.transparent, true, hitObject, 0);
|
2014-06-30 04:17:04 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-16 21:44:13 +01:00
|
|
|
@Override
|
|
|
|
public boolean mousePressed(int x, int y) { return false; } // not used
|
2015-01-16 09:47:37 +01:00
|
|
|
|
2015-01-16 21:44:13 +01:00
|
|
|
@Override
|
2014-06-30 04:17:04 +02:00
|
|
|
public boolean update(boolean overlap, int delta, int mouseX, int mouseY) {
|
|
|
|
int trackPosition = MusicController.getPosition();
|
|
|
|
|
|
|
|
// end of spinner
|
2015-01-30 03:24:21 +01:00
|
|
|
if (overlap || trackPosition > hitObject.getEndTime()) {
|
2014-06-30 04:17:04 +02:00
|
|
|
hitResult();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-06 05:33:40 +01:00
|
|
|
// game button is released
|
|
|
|
if (isSpinning && !(Utils.isGameKeyPressed() || GameMod.RELAX.isActive()))
|
|
|
|
isSpinning = false;
|
|
|
|
|
|
|
|
|
|
|
|
float angle;
|
|
|
|
// http://osu.ppy.sh/wiki/FAQ#Spinners
|
2014-06-30 04:17:04 +02:00
|
|
|
// spin automatically (TODO: correct rotation angles)
|
2014-07-16 22:01:36 +02:00
|
|
|
if (GameMod.AUTO.isActive()) {
|
2014-06-30 04:17:04 +02:00
|
|
|
// "auto" mod (fast)
|
2015-03-06 05:33:40 +01:00
|
|
|
lastAngle = 0;
|
|
|
|
//angle = 477/60f * delta/1000f * TWO_PI; ~delta/20
|
|
|
|
angle = delta / 20f;
|
|
|
|
isSpinning = true;
|
2014-07-16 22:01:36 +02:00
|
|
|
} else if (GameMod.SPUN_OUT.isActive()) {
|
2014-06-30 04:17:04 +02:00
|
|
|
// "spun out" mod (slow)
|
2015-03-06 05:33:40 +01:00
|
|
|
lastAngle = 0;
|
|
|
|
//angle = 287/60f * delta/1000f * TWO_PI;
|
|
|
|
angle = delta / 32f;
|
2015-02-14 00:12:04 +01:00
|
|
|
isSpinning = true;
|
2015-03-06 05:33:40 +01:00
|
|
|
} else {
|
|
|
|
angle = (float) Math.atan2(mouseY - (height / 2), mouseX - (width / 2));
|
|
|
|
// set initial angle to current mouse position to skip first click
|
|
|
|
if (!isSpinning && (Utils.isGameKeyPressed() || GameMod.RELAX.isActive())) {
|
|
|
|
lastAngle = angle;
|
|
|
|
isSpinning = true;
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
2015-03-06 05:33:40 +01:00
|
|
|
|
2015-02-14 07:07:17 +01:00
|
|
|
|
2015-02-14 00:12:04 +01:00
|
|
|
float angleDiff = angle - lastAngle;
|
2015-02-14 07:07:17 +01:00
|
|
|
|
2015-02-14 00:12:04 +01:00
|
|
|
// make angleDiff the smallest angle change possible
|
|
|
|
// (i.e. 1/4 rotation instead of 3/4 rotation)
|
2015-02-14 07:07:17 +01:00
|
|
|
if (angleDiff < -Math.PI)
|
|
|
|
angleDiff += TWO_PI;
|
|
|
|
else if (angleDiff > Math.PI)
|
|
|
|
angleDiff -= TWO_PI;
|
|
|
|
|
2015-02-14 00:12:04 +01:00
|
|
|
// spin caused by the cursor
|
|
|
|
float cursorVelocity = 0;
|
|
|
|
if (isSpinning)
|
2015-03-06 05:33:40 +01:00
|
|
|
cursorVelocity = Utils.clamp(angleDiff / TWO_PI / delta * 1000, -8f, 8f);
|
|
|
|
|
|
|
|
deltaOverflow += delta;
|
|
|
|
while(deltaOverflow >= DELTA_UPDATE_TIME){
|
|
|
|
sumVelocity -= storedVelocities[velocityIndex];
|
|
|
|
sumVelocity += cursorVelocity;
|
|
|
|
storedVelocities[velocityIndex++] = cursorVelocity;
|
|
|
|
velocityIndex %= storedVelocities.length;
|
|
|
|
deltaOverflow -= DELTA_UPDATE_TIME;
|
|
|
|
}
|
2015-02-14 07:07:17 +01:00
|
|
|
float rotationAngle = sumVelocity / storedVelocities.length * TWO_PI * delta / 1000;
|
|
|
|
rotate(rotationAngle);
|
2015-03-06 05:33:40 +01:00
|
|
|
if (Math.abs(rotationAngle) > 0.00001f)
|
2015-02-14 07:07:17 +01:00
|
|
|
data.changeHealth(delta * GameData.HP_DRAIN_MULTIPLIER);
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
lastAngle = angle;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-14 07:07:17 +01:00
|
|
|
* Rotates the spinner by an angle.
|
2015-02-10 00:11:49 +01:00
|
|
|
* @param angle the angle to rotate (in radians)
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2015-02-10 00:11:49 +01:00
|
|
|
private void rotate(float angle) {
|
2015-03-06 05:33:40 +01:00
|
|
|
drawRotation += (angle / TWO_PI);
|
2015-02-14 00:12:04 +01:00
|
|
|
angle = Math.abs(angle);
|
2015-02-14 07:07:17 +01:00
|
|
|
float newRotations = rotations + (angle / TWO_PI);
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
// added one whole rotation...
|
|
|
|
if (Math.floor(newRotations) > rotations) {
|
2014-07-01 07:14:03 +02:00
|
|
|
if (newRotations > rotationsNeeded) { // extra rotations
|
2015-01-27 09:19:39 +01:00
|
|
|
data.changeScore(1000);
|
2015-01-08 01:29:51 +01:00
|
|
|
SoundController.playSound(SoundEffect.SPINNERBONUS);
|
2014-07-01 07:14:03 +02:00
|
|
|
} else {
|
2015-01-27 09:19:39 +01:00
|
|
|
data.changeScore(100);
|
2015-01-08 01:29:51 +01:00
|
|
|
SoundController.playSound(SoundEffect.SPINNERSPIN);
|
2014-07-01 07:14:03 +02:00
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rotations = newRotations;
|
|
|
|
}
|
2015-02-10 00:11:49 +01:00
|
|
|
}
|