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:
@@ -28,12 +28,13 @@ import itdelatrisu.opsu.states.Game;
|
||||
|
||||
import org.newdawn.slick.Color;
|
||||
import org.newdawn.slick.GameContainer;
|
||||
import org.newdawn.slick.Graphics;
|
||||
import org.newdawn.slick.SlickException;
|
||||
|
||||
/**
|
||||
* Data type representing a circle object.
|
||||
*/
|
||||
public class Circle {
|
||||
public class Circle implements HitObject {
|
||||
/**
|
||||
* The associated OsuHitObject.
|
||||
*/
|
||||
@@ -89,11 +90,7 @@ public class Circle {
|
||||
this.comboEnd = comboEnd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws the circle to the graphics context.
|
||||
* @param trackPosition the current track position
|
||||
*/
|
||||
public void draw(int trackPosition) {
|
||||
public void draw(int trackPosition, boolean currentObject, Graphics g) {
|
||||
int timeDiff = hitObject.getTime() - trackPosition;
|
||||
|
||||
if (timeDiff >= 0) {
|
||||
@@ -117,7 +114,7 @@ public class Circle {
|
||||
* @param time the hit object time (difference between track time)
|
||||
* @return the hit result (GameScore.HIT_* constants)
|
||||
*/
|
||||
public int hitResult(int time) {
|
||||
private int hitResult(int time) {
|
||||
int trackPosition = MusicController.getPosition();
|
||||
int timeDiff = Math.abs(trackPosition - time);
|
||||
|
||||
@@ -136,13 +133,6 @@ public class Circle {
|
||||
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) {
|
||||
double distance = Math.hypot(hitObject.getX() - x, hitObject.getY() - y);
|
||||
int circleRadius = GameImage.HITCIRCLE.getImage().getWidth() / 2;
|
||||
@@ -160,12 +150,7 @@ public class Circle {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
public boolean update(boolean overlap, int delta, int mouseX, int mouseY) {
|
||||
int time = hitObject.getTime();
|
||||
float x = hitObject.getX(), y = hitObject.getY();
|
||||
byte hitSound = hitObject.getHitSoundType();
|
||||
|
||||
52
src/itdelatrisu/opsu/objects/HitObject.java
Normal file
52
src/itdelatrisu/opsu/objects/HitObject.java
Normal 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);
|
||||
}
|
||||
@@ -32,13 +32,14 @@ import java.io.File;
|
||||
import org.newdawn.slick.Animation;
|
||||
import org.newdawn.slick.Color;
|
||||
import org.newdawn.slick.GameContainer;
|
||||
import org.newdawn.slick.Graphics;
|
||||
import org.newdawn.slick.Image;
|
||||
import org.newdawn.slick.SlickException;
|
||||
|
||||
/**
|
||||
* Data type representing a slider object.
|
||||
*/
|
||||
public class Slider {
|
||||
public class Slider implements HitObject {
|
||||
/**
|
||||
* Slider ball animation.
|
||||
*/
|
||||
@@ -335,12 +336,7 @@ public class Slider {
|
||||
this.bezier = new Bezier();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
public void draw(int trackPosition, boolean currentObject, Graphics g) {
|
||||
float x = hitObject.getX(), y = hitObject.getY();
|
||||
float[] sliderX = hitObject.getSliderX(), sliderY = hitObject.getSliderY();
|
||||
int timeDiff = hitObject.getTime() - trackPosition;
|
||||
@@ -417,7 +413,7 @@ public class Slider {
|
||||
* @param lastCircleHit true if the cursor was held within the last circle
|
||||
* @return the hit result (GameScore.HIT_* constants)
|
||||
*/
|
||||
public int hitResult() {
|
||||
private int hitResult() {
|
||||
int lastIndex = hitObject.getSliderX().length - 1;
|
||||
float tickRatio = (float) ticksHit / tickIntervals;
|
||||
|
||||
@@ -442,13 +438,6 @@ public class Slider {
|
||||
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) {
|
||||
if (sliderClicked) // first circle already processed
|
||||
return false;
|
||||
@@ -478,14 +467,6 @@ public class Slider {
|
||||
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) {
|
||||
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
|
||||
* @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;
|
||||
if (raw)
|
||||
return t;
|
||||
|
||||
@@ -37,7 +37,7 @@ import org.newdawn.slick.SlickException;
|
||||
/**
|
||||
* Data type representing a spinner object.
|
||||
*/
|
||||
public class Spinner {
|
||||
public class Spinner implements HitObject {
|
||||
/**
|
||||
* Container dimensions.
|
||||
*/
|
||||
@@ -99,12 +99,11 @@ public class Spinner {
|
||||
rotationsNeeded = spinsPerMinute * (hitObject.getEndTime() - hitObject.getTime()) / 60000f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws the spinner to the graphics context.
|
||||
* @param trackPosition the current track position
|
||||
* @param g the graphics context
|
||||
*/
|
||||
public void draw(int trackPosition, Graphics g) {
|
||||
public void draw(int trackPosition, boolean currentObject, Graphics g) {
|
||||
// only draw spinners if current object
|
||||
if (!currentObject)
|
||||
return;
|
||||
|
||||
int timeDiff = hitObject.getTime() - trackPosition;
|
||||
boolean spinnerComplete = (rotations >= rotationsNeeded);
|
||||
|
||||
@@ -145,7 +144,7 @@ public class Spinner {
|
||||
* Calculates and sends the spinner hit result.
|
||||
* @return the hit result (GameScore.HIT_* constants)
|
||||
*/
|
||||
public int hitResult() {
|
||||
private int hitResult() {
|
||||
// TODO: verify ratios
|
||||
|
||||
int result;
|
||||
@@ -166,14 +165,9 @@ public class Spinner {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the spinner by a delta interval.
|
||||
* @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
|
||||
*/
|
||||
// not used
|
||||
public boolean mousePressed(int x, int y) { return false; }
|
||||
|
||||
public boolean update(boolean overlap, int delta, int mouseX, int mouseY) {
|
||||
int trackPosition = MusicController.getPosition();
|
||||
if (overlap)
|
||||
|
||||
Reference in New Issue
Block a user