Replace xy float[] arrays with Vec2f (mostly in game objects).

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han
2015-09-05 12:53:42 -05:00
parent 1b8f85942f
commit 01fb9c50c9
12 changed files with 114 additions and 117 deletions

View File

@@ -25,6 +25,7 @@ import itdelatrisu.opsu.GameMod;
import itdelatrisu.opsu.Options;
import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.beatmap.HitObject;
import itdelatrisu.opsu.objects.curves.Vec2f;
import itdelatrisu.opsu.states.Game;
import itdelatrisu.opsu.ui.Colors;
@@ -194,7 +195,7 @@ public class Circle implements GameObject {
}
@Override
public float[] getPointAt(int trackPosition) { return new float[] { x, y }; }
public Vec2f getPointAt(int trackPosition) { return new Vec2f(x, y); }
@Override
public int getEndTime() { return hitObject.getTime(); }

View File

@@ -19,6 +19,7 @@
package itdelatrisu.opsu.objects;
import itdelatrisu.opsu.beatmap.HitObject;
import itdelatrisu.opsu.objects.curves.Vec2f;
import org.newdawn.slick.Graphics;
@@ -53,7 +54,7 @@ public class DummyObject implements GameObject {
public boolean mousePressed(int x, int y, int trackPosition) { return false; }
@Override
public float[] getPointAt(int trackPosition) { return new float[] { x, y }; }
public Vec2f getPointAt(int trackPosition) { return new Vec2f(x, y); }
@Override
public int getEndTime() { return hitObject.getTime(); }

View File

@@ -18,6 +18,8 @@
package itdelatrisu.opsu.objects;
import itdelatrisu.opsu.objects.curves.Vec2f;
import org.newdawn.slick.Graphics;
/**
@@ -55,9 +57,9 @@ public interface GameObject {
/**
* Returns the coordinates of the hit object at a given track position.
* @param trackPosition the track position
* @return the [x,y] coordinates
* @return the position vector
*/
public float[] getPointAt(int trackPosition);
public Vec2f getPointAt(int trackPosition);
/**
* Returns the end time of the hit object.

View File

@@ -27,6 +27,7 @@ import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.beatmap.Beatmap;
import itdelatrisu.opsu.beatmap.HitObject;
import itdelatrisu.opsu.objects.curves.Curve;
import itdelatrisu.opsu.objects.curves.Vec2f;
import itdelatrisu.opsu.states.Game;
import itdelatrisu.opsu.ui.Colors;
@@ -184,14 +185,14 @@ public class Slider implements GameObject {
Colors.WHITE_FADE.a = color.a = alpha;
Image hitCircleOverlay = GameImage.HITCIRCLE_OVERLAY.getImage();
Image hitCircle = GameImage.HITCIRCLE.getImage();
float[] endPos = curve.pointAt(1);
Vec2f endPos = curve.pointAt(1);
curve.draw(color);
color.a = alpha;
// end circle
hitCircle.drawCentered(endPos[0], endPos[1], color);
hitCircleOverlay.drawCentered(endPos[0], endPos[1], Colors.WHITE_FADE);
hitCircle.drawCentered(endPos.x, endPos.y, color);
hitCircleOverlay.drawCentered(endPos.x, endPos.y, Colors.WHITE_FADE);
// start circle
hitCircle.drawCentered(x, y, color);
@@ -202,8 +203,8 @@ public class Slider implements GameObject {
if (ticksT != null) {
Image tick = GameImage.SLIDER_TICK.getImage();
for (int i = 0; i < ticksT.length; i++) {
float[] c = curve.pointAt(ticksT[i]);
tick.drawCentered(c[0], c[1], Colors.WHITE_FADE);
Vec2f c = curve.pointAt(ticksT[i]);
tick.drawCentered(c.x, c.y, Colors.WHITE_FADE);
}
}
if (GameMod.HIDDEN.isActive()) {
@@ -236,7 +237,7 @@ public class Slider implements GameObject {
if (tcurRepeat % 2 == 0) {
// last circle
arrow.setRotation(curve.getEndAngle());
arrow.drawCentered(endPos[0], endPos[1]);
arrow.drawCentered(endPos.x, endPos.y);
} else {
// first circle
arrow.setRotation(curve.getStartAngle());
@@ -255,20 +256,20 @@ public class Slider implements GameObject {
if (sliderTime == 0)
return;
float[] c = curve.pointAt(getT(trackPosition, false));
float[] c2 = curve.pointAt(getT(trackPosition, false) + 0.01f);
Vec2f c = curve.pointAt(getT(trackPosition, false));
Vec2f c2 = curve.pointAt(getT(trackPosition, false) + 0.01f);
float t = getT(trackPosition, false);
// float dis = hitObject.getPixelLength() * HitObject.getXMultiplier() * (t - (int) t);
// Image sliderBallFrame = sliderBallImages[(int) (dis / (diameter * Math.PI) * 30) % sliderBallImages.length];
Image sliderBallFrame = sliderBallImages[(int) (t * sliderTime * 60 / 1000) % sliderBallImages.length];
float angle = (float) (Math.atan2(c2[1] - c[1], c2[0] - c[0]) * 180 / Math.PI);
float angle = (float) (Math.atan2(c2.y - c.y, c2.x - c.x) * 180 / Math.PI);
sliderBallFrame.setRotation(angle);
sliderBallFrame.drawCentered(c[0], c[1]);
sliderBallFrame.drawCentered(c.x, c.y);
// follow circle
if (followCircleActive) {
GameImage.SLIDER_FOLLOWCIRCLE.getImage().drawCentered(c[0], c[1]);
GameImage.SLIDER_FOLLOWCIRCLE.getImage().drawCentered(c.x, c.y);
// "flashlight" mod: dim the screen
if (GameMod.FLASHLIGHT.isActive()) {
@@ -351,9 +352,9 @@ public class Slider implements GameObject {
float cx, cy;
HitObjectType type;
if (currentRepeats % 2 == 0) { // last circle
float[] lastPos = curve.pointAt(1);
cx = lastPos[0];
cy = lastPos[1];
Vec2f lastPos = curve.pointAt(1);
cx = lastPos.x;
cy = lastPos.y;
type = HitObjectType.SLIDER_LAST;
} else { // first circle
cx = x;
@@ -434,8 +435,8 @@ public class Slider implements GameObject {
// check if cursor pressed and within end circle
if (keyPressed || GameMod.RELAX.isActive()) {
float[] c = curve.pointAt(getT(trackPosition, false));
double distance = Math.hypot(c[0] - mouseX, c[1] - mouseY);
Vec2f c = curve.pointAt(getT(trackPosition, false));
double distance = Math.hypot(c.x - mouseX, c.y - mouseY);
if (distance < followRadius)
sliderHeldToEnd = true;
}
@@ -478,8 +479,8 @@ public class Slider implements GameObject {
}
// holding slider...
float[] c = curve.pointAt(getT(trackPosition, false));
double distance = Math.hypot(c[0] - mouseX, c[1] - mouseY);
Vec2f c = curve.pointAt(getT(trackPosition, false));
double distance = Math.hypot(c.x - mouseX, c.y - mouseY);
if (((keyPressed || GameMod.RELAX.isActive()) && distance < followRadius) || isAutoMod) {
// mouse pressed and within follow circle
followCircleActive = true;
@@ -493,14 +494,14 @@ public class Slider implements GameObject {
curve.getX(lastIndex), curve.getY(lastIndex), hitObject, currentRepeats);
} else // first circle
data.sliderTickResult(trackPosition, GameData.HIT_SLIDER30,
c[0], c[1], hitObject, currentRepeats);
c.x, c.y, hitObject, currentRepeats);
}
// held during new tick
if (isNewTick) {
ticksHit++;
data.sliderTickResult(trackPosition, GameData.HIT_SLIDER10,
c[0], c[1], hitObject, currentRepeats);
c.x, c.y, hitObject, currentRepeats);
}
// held near end of slider
@@ -526,12 +527,12 @@ public class Slider implements GameObject {
}
@Override
public float[] getPointAt(int trackPosition) {
public Vec2f getPointAt(int trackPosition) {
if (trackPosition <= hitObject.getTime())
return new float[] { x, y };
return new Vec2f(x, y);
else if (trackPosition >= hitObject.getTime() + sliderTimeTotal) {
if (hitObject.getRepeatCount() % 2 == 0)
return new float[] { x, y };
return new Vec2f(x, y);
else
return curve.pointAt(1);
} else

View File

@@ -27,6 +27,7 @@ import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.audio.SoundController;
import itdelatrisu.opsu.audio.SoundEffect;
import itdelatrisu.opsu.beatmap.HitObject;
import itdelatrisu.opsu.objects.curves.Vec2f;
import itdelatrisu.opsu.states.Game;
import itdelatrisu.opsu.ui.Colors;
@@ -347,7 +348,7 @@ public class Spinner implements GameObject {
public void updatePosition() {}
@Override
public float[] getPointAt(int trackPosition) {
public Vec2f getPointAt(int trackPosition) {
// get spinner time
int timeDiff;
float x = hitObject.getScaledX(), y = hitObject.getScaledY();
@@ -362,10 +363,7 @@ public class Spinner implements GameObject {
float multiplier = (GameMod.AUTO.isActive()) ? AUTO_MULTIPLIER : SPUN_OUT_MULTIPLIER;
float angle = (timeDiff * multiplier) - HALF_PI;
final float r = height / 10f;
return new float[] {
(float) (x + r * Math.cos(angle)),
(float) (y + r * Math.sin(angle))
};
return new Vec2f((float) (x + r * Math.cos(angle)), (float) (y + r * Math.sin(angle)));
}
@Override

View File

@@ -115,10 +115,8 @@ public class CircumscribedCircle extends Curve {
// calculate points
float step = hitObject.getPixelLength() / CURVE_POINTS_SEPERATION;
curve = new Vec2f[(int) step + 1];
for (int i = 0; i < curve.length; i++) {
float[] xy = pointAt(i / step);
curve[i] = new Vec2f(xy[0], xy[1]);
}
for (int i = 0; i < curve.length; i++)
curve[i] = pointAt(i / step);
}
/**
@@ -157,12 +155,12 @@ public class CircumscribedCircle extends Curve {
}
@Override
public float[] pointAt(float t) {
public Vec2f pointAt(float t) {
float ang = Utils.lerp(startAng, endAng, t);
return new float[] {
return new Vec2f(
(float) (Math.cos(ang) * radius + circleCenter.x),
(float) (Math.sin(ang) * radius + circleCenter.y)
};
);
}
@Override

View File

@@ -106,9 +106,9 @@ public abstract class Curve {
/**
* Returns the point on the curve at a value t.
* @param t the t value [0, 1]
* @return the point [x, y]
* @return the position vector
*/
public abstract float[] pointAt(float t);
public abstract Vec2f pointAt(float t);
/**
* Draws the full curve to the graphics context.

View File

@@ -124,20 +124,19 @@ public abstract class EqualDistanceMultiCurve extends Curve {
}
@Override
public float[] pointAt(float t) {
public Vec2f pointAt(float t) {
float indexF = t * ncurve;
int index = (int) indexF;
if (index >= ncurve) {
Vec2f poi = curve[ncurve];
return new float[] { poi.x, poi.y };
} else {
if (index >= ncurve)
return curve[ncurve].cpy();
else {
Vec2f poi = curve[index];
Vec2f poi2 = curve[index + 1];
float t2 = indexF - index;
return new float[] {
return new Vec2f(
Utils.lerp(poi.x, poi2.x, t2),
Utils.lerp(poi.y, poi2.y, t2)
};
);
}
}

View File

@@ -40,6 +40,16 @@ public class Vec2f {
*/
public Vec2f() {}
/**
* Sets the x and y components of this vector.
* @return itself (for chaining)
*/
public Vec2f set(float nx, float ny) {
x = nx;
y = ny;
return this;
}
/**
* Finds the midpoint between this vector and another vector.
* @param o the other vector