Added HitObject interface for OsuHitObjects during gameplay.

This greatly simplifies object-handling code, and is slightly more time and memory efficient.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-01-16 03:47:37 -05:00
parent bcf1fa301a
commit 7bd9eba2f5
5 changed files with 90 additions and 118 deletions

View File

@ -28,12 +28,13 @@ import itdelatrisu.opsu.states.Game;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer; import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException; import org.newdawn.slick.SlickException;
/** /**
* Data type representing a circle object. * Data type representing a circle object.
*/ */
public class Circle { public class Circle implements HitObject {
/** /**
* The associated OsuHitObject. * The associated OsuHitObject.
*/ */
@ -89,11 +90,7 @@ public class Circle {
this.comboEnd = comboEnd; this.comboEnd = comboEnd;
} }
/** public void draw(int trackPosition, boolean currentObject, Graphics g) {
* Draws the circle to the graphics context.
* @param trackPosition the current track position
*/
public void draw(int trackPosition) {
int timeDiff = hitObject.getTime() - trackPosition; int timeDiff = hitObject.getTime() - trackPosition;
if (timeDiff >= 0) { if (timeDiff >= 0) {
@ -117,7 +114,7 @@ public class Circle {
* @param time the hit object time (difference between track time) * @param time the hit object time (difference between track time)
* @return the hit result (GameScore.HIT_* constants) * @return the hit result (GameScore.HIT_* constants)
*/ */
public int hitResult(int time) { private int hitResult(int time) {
int trackPosition = MusicController.getPosition(); int trackPosition = MusicController.getPosition();
int timeDiff = Math.abs(trackPosition - time); int timeDiff = Math.abs(trackPosition - time);
@ -136,13 +133,6 @@ public class Circle {
return result; return result;
} }
/**
* Processes a mouse click.
* @param x the x coordinate of the mouse
* @param y the y coordinate of the mouse
* @param comboEnd if this is the last object in the combo
* @return true if a hit result was processed
*/
public boolean mousePressed(int x, int y) { public boolean mousePressed(int x, int y) {
double distance = Math.hypot(hitObject.getX() - x, hitObject.getY() - y); double distance = Math.hypot(hitObject.getX() - x, hitObject.getY() - y);
int circleRadius = GameImage.HITCIRCLE.getImage().getWidth() / 2; int circleRadius = GameImage.HITCIRCLE.getImage().getWidth() / 2;
@ -160,12 +150,7 @@ public class Circle {
return false; return false;
} }
/** public boolean update(boolean overlap, int delta, int mouseX, int mouseY) {
* Updates the circle object.
* @param overlap true if the next object's start time has already passed
* @return true if a hit result (miss) was processed
*/
public boolean update(boolean overlap) {
int time = hitObject.getTime(); int time = hitObject.getTime();
float x = hitObject.getX(), y = hitObject.getY(); float x = hitObject.getX(), y = hitObject.getY();
byte hitSound = hitObject.getHitSoundType(); byte hitSound = hitObject.getHitSoundType();

View File

@ -0,0 +1,52 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014 Jeffrey Han
*
* 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;
import org.newdawn.slick.Graphics;
/**
* Hit object interface.
*/
public interface HitObject {
/**
* Draws the hit object to the graphics context.
* @param trackPosition the current track position
* @param currentObject true if this is the current hit object
* @param g the graphics context
*/
public void draw(int trackPosition, boolean currentObject, Graphics g);
/**
* Updates the hit object.
* @param overlap true if the next object's start time has already passed
* @param delta the delta interval since the last call
* @param mouseX the x coordinate of the mouse
* @param mouseY the y coordinate of the mouse
* @return true if object ended
*/
public boolean update(boolean overlap, int delta, int mouseX, int mouseY);
/**
* Processes a mouse click.
* @param x the x coordinate of the mouse
* @param y the y coordinate of the mouse
* @return true if a hit result was processed
*/
public boolean mousePressed(int x, int y);
}

View File

@ -32,13 +32,14 @@ import java.io.File;
import org.newdawn.slick.Animation; import org.newdawn.slick.Animation;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer; import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image; import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException; import org.newdawn.slick.SlickException;
/** /**
* Data type representing a slider object. * Data type representing a slider object.
*/ */
public class Slider { public class Slider implements HitObject {
/** /**
* Slider ball animation. * Slider ball animation.
*/ */
@ -335,12 +336,7 @@ public class Slider {
this.bezier = new Bezier(); this.bezier = new Bezier();
} }
/** public void draw(int trackPosition, boolean currentObject, Graphics g) {
* Draws the slider to the graphics context.
* @param trackPosition the current track position
* @param currentObject true if this is the current hit object
*/
public void draw(int trackPosition, boolean currentObject) {
float x = hitObject.getX(), y = hitObject.getY(); float x = hitObject.getX(), y = hitObject.getY();
float[] sliderX = hitObject.getSliderX(), sliderY = hitObject.getSliderY(); float[] sliderX = hitObject.getSliderX(), sliderY = hitObject.getSliderY();
int timeDiff = hitObject.getTime() - trackPosition; int timeDiff = hitObject.getTime() - trackPosition;
@ -417,7 +413,7 @@ public class Slider {
* @param lastCircleHit true if the cursor was held within the last circle * @param lastCircleHit true if the cursor was held within the last circle
* @return the hit result (GameScore.HIT_* constants) * @return the hit result (GameScore.HIT_* constants)
*/ */
public int hitResult() { private int hitResult() {
int lastIndex = hitObject.getSliderX().length - 1; int lastIndex = hitObject.getSliderX().length - 1;
float tickRatio = (float) ticksHit / tickIntervals; float tickRatio = (float) ticksHit / tickIntervals;
@ -442,13 +438,6 @@ public class Slider {
return result; return result;
} }
/**
* Processes a mouse click.
* @param x the x coordinate of the mouse
* @param y the y coordinate of the mouse
* @param comboEnd if this is the last object in the combo
* @return true if a hit result was processed
*/
public boolean mousePressed(int x, int y) { public boolean mousePressed(int x, int y) {
if (sliderClicked) // first circle already processed if (sliderClicked) // first circle already processed
return false; return false;
@ -478,14 +467,6 @@ public class Slider {
return false; return false;
} }
/**
* Updates the slider object.
* @param overlap true if the next object's start time has already passed
* @param delta the delta interval since the last call
* @param mouseX the x coordinate of the mouse
* @param mouseY the y coordinate of the mouse
* @return true if slider ended
*/
public boolean update(boolean overlap, int delta, int mouseX, int mouseY) { public boolean update(boolean overlap, int delta, int mouseX, int mouseY) {
int repeatCount = hitObject.getRepeatCount(); int repeatCount = hitObject.getRepeatCount();
@ -629,7 +610,7 @@ public class Slider {
* @param raw if false, ensures that the value lies within [0, 1] by looping repeats * @param raw if false, ensures that the value lies within [0, 1] by looping repeats
* @return the t value: raw [0, repeats] or looped [0, 1] * @return the t value: raw [0, repeats] or looped [0, 1]
*/ */
public float getT(int trackPosition, boolean raw) { private float getT(int trackPosition, boolean raw) {
float t = (trackPosition - hitObject.getTime()) / sliderTime; float t = (trackPosition - hitObject.getTime()) / sliderTime;
if (raw) if (raw)
return t; return t;

View File

@ -37,7 +37,7 @@ import org.newdawn.slick.SlickException;
/** /**
* Data type representing a spinner object. * Data type representing a spinner object.
*/ */
public class Spinner { public class Spinner implements HitObject {
/** /**
* Container dimensions. * Container dimensions.
*/ */
@ -99,12 +99,11 @@ public class Spinner {
rotationsNeeded = spinsPerMinute * (hitObject.getEndTime() - hitObject.getTime()) / 60000f; rotationsNeeded = spinsPerMinute * (hitObject.getEndTime() - hitObject.getTime()) / 60000f;
} }
/** public void draw(int trackPosition, boolean currentObject, Graphics g) {
* Draws the spinner to the graphics context. // only draw spinners if current object
* @param trackPosition the current track position if (!currentObject)
* @param g the graphics context return;
*/
public void draw(int trackPosition, Graphics g) {
int timeDiff = hitObject.getTime() - trackPosition; int timeDiff = hitObject.getTime() - trackPosition;
boolean spinnerComplete = (rotations >= rotationsNeeded); boolean spinnerComplete = (rotations >= rotationsNeeded);
@ -145,7 +144,7 @@ public class Spinner {
* Calculates and sends the spinner hit result. * Calculates and sends the spinner hit result.
* @return the hit result (GameScore.HIT_* constants) * @return the hit result (GameScore.HIT_* constants)
*/ */
public int hitResult() { private int hitResult() {
// TODO: verify ratios // TODO: verify ratios
int result; int result;
@ -166,14 +165,9 @@ public class Spinner {
return result; return result;
} }
/** // not used
* Updates the spinner by a delta interval. public boolean mousePressed(int x, int y) { return false; }
* @param overlap true if the next object's start time has already passed
* @param delta the delta interval since the last call
* @param mouseX the x coordinate of the mouse
* @param mouseY the y coordinate of the mouse
* @return true if spinner ended
*/
public boolean update(boolean overlap, int delta, int mouseX, int mouseY) { public boolean update(boolean overlap, int delta, int mouseX, int mouseY) {
int trackPosition = MusicController.getPosition(); int trackPosition = MusicController.getPosition();
if (overlap) if (overlap)

View File

@ -33,11 +33,11 @@ import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.audio.SoundController; import itdelatrisu.opsu.audio.SoundController;
import itdelatrisu.opsu.audio.SoundEffect; import itdelatrisu.opsu.audio.SoundEffect;
import itdelatrisu.opsu.objects.Circle; import itdelatrisu.opsu.objects.Circle;
import itdelatrisu.opsu.objects.HitObject;
import itdelatrisu.opsu.objects.Slider; import itdelatrisu.opsu.objects.Slider;
import itdelatrisu.opsu.objects.Spinner; import itdelatrisu.opsu.objects.Spinner;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.Stack; import java.util.Stack;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -89,19 +89,9 @@ public class Game extends BasicGameState {
private int objectIndex = 0; private int objectIndex = 0;
/** /**
* This map's hit circles objects, keyed by objectIndex. * The map's HitObjects, indexed by objectIndex.
*/ */
private HashMap<Integer, Circle> circles; private HitObject[] hitObjects;
/**
* This map's slider objects, keyed by objectIndex.
*/
private HashMap<Integer, Slider> sliders;
/**
* This map's spinner objects, keyed by objectIndex.
*/
private HashMap<Integer, Spinner> spinners;
/** /**
* Delay time, in milliseconds, before song starts. * Delay time, in milliseconds, before song starts.
@ -405,21 +395,8 @@ public class Game extends BasicGameState {
for (int i = objectIndex; i < osu.objects.length && osu.objects[i].getTime() < trackPosition + approachTime; i++) for (int i = objectIndex; i < osu.objects.length && osu.objects[i].getTime() < trackPosition + approachTime; i++)
stack.add(i); stack.add(i);
while (!stack.isEmpty()) { while (!stack.isEmpty())
int i = stack.pop(); hitObjects[stack.pop()].draw(trackPosition, stack.isEmpty(), g);
OsuHitObject hitObject = osu.objects[i];
if (hitObject.isCircle())
circles.get(i).draw(trackPosition);
else if (hitObject.isSlider())
sliders.get(i).draw(trackPosition, stack.isEmpty());
else if (hitObject.isSpinner()) {
if (stack.isEmpty()) // only draw spinner at objectIndex
spinners.get(i).draw(trackPosition, g);
else
continue;
}
}
// draw OsuHitObjectResult objects // draw OsuHitObjectResult objects
score.drawHitResults(trackPosition); score.drawHitResults(trackPosition);
@ -577,24 +554,13 @@ public class Game extends BasicGameState {
// update objects (loop in unlikely event of any skipped indexes) // update objects (loop in unlikely event of any skipped indexes)
while (objectIndex < osu.objects.length && trackPosition > osu.objects[objectIndex].getTime()) { while (objectIndex < osu.objects.length && trackPosition > osu.objects[objectIndex].getTime()) {
OsuHitObject hitObject = osu.objects[objectIndex];
// check if we've already passed the next object's start time // check if we've already passed the next object's start time
boolean overlap = (objectIndex + 1 < osu.objects.length && boolean overlap = (objectIndex + 1 < osu.objects.length &&
trackPosition > osu.objects[objectIndex + 1].getTime() - hitResultOffset[GameScore.HIT_300]); trackPosition > osu.objects[objectIndex + 1].getTime() - hitResultOffset[GameScore.HIT_300]);
// check completion status of the hit object // update hit object and check completion status
boolean done = false; if (hitObjects[objectIndex].update(overlap, delta, mouseX, mouseY))
if (hitObject.isCircle()) objectIndex++; // done, so increment object index
done = circles.get(objectIndex).update(overlap);
else if (hitObject.isSlider())
done = sliders.get(objectIndex).update(overlap, delta, mouseX, mouseY);
else if (hitObject.isSpinner())
done = spinners.get(objectIndex).update(overlap, delta, mouseX, mouseY);
// increment object index?
if (done)
objectIndex++;
else else
break; break;
} }
@ -731,15 +697,12 @@ public class Game extends BasicGameState {
return; return;
// circles // circles
if (hitObject.isCircle()) { if (hitObject.isCircle() && hitObjects[objectIndex].mousePressed(x, y))
boolean hit = circles.get(objectIndex).mousePressed(x, y); objectIndex++; // circle hit
if (hit)
objectIndex++;
}
// sliders // sliders
else if (hitObject.isSlider()) else if (hitObject.isSlider())
sliders.get(objectIndex).mousePressed(x, y); hitObjects[objectIndex].mousePressed(x, y);
} }
@Override @Override
@ -781,13 +744,12 @@ public class Game extends BasicGameState {
comboEnd = true; comboEnd = true;
Color color = osu.combo[hitObject.getComboIndex()]; Color color = osu.combo[hitObject.getComboIndex()];
if (hitObject.isCircle()) { if (hitObject.isCircle())
circles.put(i, new Circle(hitObject, this, score, color, comboEnd)); hitObjects[i] = new Circle(hitObject, this, score, color, comboEnd);
} else if (hitObject.isSlider()) { else if (hitObject.isSlider())
sliders.put(i, new Slider(hitObject, this, score, color, comboEnd)); hitObjects[i] = new Slider(hitObject, this, score, color, comboEnd);
} else if (hitObject.isSpinner()) { else if (hitObject.isSpinner())
spinners.put(i, new Spinner(hitObject, this, score)); hitObjects[i] = new Spinner(hitObject, this, score);
}
} }
// load the first timingPoint // load the first timingPoint
@ -818,9 +780,7 @@ public class Game extends BasicGameState {
* Resets all game data and structures. * Resets all game data and structures.
*/ */
public void resetGameData() { public void resetGameData() {
circles = new HashMap<Integer, Circle>(); hitObjects = new HitObject[osu.objects.length];
sliders = new HashMap<Integer, Slider>();
spinners = new HashMap<Integer, Spinner>();
score.clear(); score.clear();
objectIndex = 0; objectIndex = 0;
breakIndex = 0; breakIndex = 0;