It doesn't crash anymore

This commit is contained in:
Awlex 2016-11-19 15:41:53 +01:00
parent 6f5f17da74
commit eb561d8369
5 changed files with 81 additions and 32 deletions

View File

@ -8,19 +8,19 @@ import static java.lang.Math.*;
/** /**
* Created by Awlex on 13.11.2016. * Created by Awlex on 13.11.2016.
*/ */
public class Arc extends PolyMover { public class ArcMover extends PolyMover {
private GameObject p1, middle, p2; private GameObject p1, middle, p2;
private double xm, ym, r, alpha, beta, gamma; private double xm, ym, r, alpha, beta, gamma;
public Arc(GameObject p1, GameObject middle, GameObject p2) { public ArcMover(GameObject p1, GameObject middle, GameObject p2) {
this.p1 = p1; this.p1 = p1;
this.middle = middle; this.middle = middle;
this.p2 = p2; this.p2 = p2;
init(); init();
} }
public Arc(PolyMover mover, GameObject p) { public ArcMover(PolyMover mover, GameObject p) {
GameObject[] items = mover.getItems(); GameObject[] items = mover.getItems();
p1 = items[items.length - 2]; p1 = items[items.length - 2];
middle = items[items.length - 1]; middle = items[items.length - 1];
@ -40,17 +40,12 @@ public class Arc extends PolyMover {
@Override @Override
public double[] getPointAt(int time) { public double[] getPointAt(int time) {
double percent;
double angle; double angle;
if (time < middle.getTime()) { if (time < middle.getTime()) {
time -= p1.getEndTime(); angle = alpha + beta * ((double) time - p1.getEndTime()) / ((middle.getTime() - p1.getEndTime()));
percent = ((double) time) / ((middle.getTime() - p1.getEndTime()));
angle = alpha + beta * percent;
} }
else { else {
time -= middle.getTime(); angle = beta + gamma * ((double) time - middle.getTime()) / (p2.getTime() - middle.getTime());
percent = ((double) time) / (p2.getTime() - middle.getTime());
angle = beta + gamma * percent;
} }
return new double[]{ return new double[]{
xm + r * cos(angle), xm + r * cos(angle),
@ -67,7 +62,7 @@ public class Arc extends PolyMover {
}; };
} }
private Matrix prepareMatrix(GameObject p1, GameObject middle, GameObject p2) { private static Matrix prepareMatrix(GameObject p1, GameObject middle, GameObject p2) {
Matrix a = new Matrix(new double[][]{ Matrix a = new Matrix(new double[][]{
{1, -p1.end.x, -p1.end.y}, {1, -p1.end.x, -p1.end.y},
{1, -middle.end.x, -middle.end.y}, {1, -middle.end.x, -middle.end.y},
@ -83,6 +78,11 @@ public class Arc extends PolyMover {
} }
public static boolean canCricleExistBetweenItems(GameObject p1, GameObject p2, GameObject p3) { public static boolean canCricleExistBetweenItems(GameObject p1, GameObject p2, GameObject p3) {
return !((p1.end.x == p2.start.x && p1.end.x == p3.start.x) || (p1.end.y == p2.start.y && p1.end.y == p3.start.y)); try {
prepareMatrix(p1, p2, p3);
} catch (RuntimeException e) {
return false;
}
return true;
} }
} }

View File

@ -0,0 +1,32 @@
package awlex.ospu.polymover;
import itdelatrisu.opsu.objects.DummyObject;
import itdelatrisu.opsu.objects.GameObject;
import yugecin.opsudance.movers.LinearMover;
/**
* Created by Awlex on 19.11.2016.
*/
public class LineMover extends PolyMover {
GameObject[] objects;
public LineMover(GameObject[] objects, int startIndex, int count) {
this.objects = new GameObject[count];
System.arraycopy(objects, startIndex, this.objects, 0, count);
}
@Override
public double[] getPointAt(int time) {
int i = 0;
while (time > objects[i].getTime() && i < objects.length - 1)
i++;
return new LinearMover(i == 0 ? new DummyObject() : objects[i - 1], objects[i], 1).getPointAt(time);
}
@Override
public GameObject[] getItems() {
return new GameObject[0];
}
}

View File

@ -1,6 +1,7 @@
package awlex.ospu.polymover.factory; package awlex.ospu.polymover.factory;
import awlex.ospu.polymover.Arc; import awlex.ospu.polymover.ArcMover;
import awlex.ospu.polymover.LineMover;
import awlex.ospu.polymover.PolyMover; import awlex.ospu.polymover.PolyMover;
import itdelatrisu.opsu.objects.GameObject; import itdelatrisu.opsu.objects.GameObject;
@ -10,15 +11,16 @@ import itdelatrisu.opsu.objects.GameObject;
public class ArcFactory implements PolyMoverFactory { public class ArcFactory implements PolyMoverFactory {
private static final int PREFFERED_BUFFER_SIZE = 3; private static final int PREFFERED_BUFFER_SIZE = 3;
private PolyMover arc1, arc2; private PolyMover current, previous;
private int lastIndex;
public double[] getPointAt(int time) { public double[] getPointAt(int time) {
if (arc2 == null) { if (previous == null) {
return arc1.getPointAt(time); return current.getPointAt(time);
} }
double[] point1 = arc1.getPointAt(time); double[] point1 = current.getPointAt(time);
double[] point2 = arc2.getPointAt(time); double[] point2 = previous.getPointAt(time);
return new double[]{ return new double[]{
(point1[0] + point2[0]) * 0.5, (point1[0] + point2[0]) * 0.5,
@ -30,19 +32,27 @@ public class ArcFactory implements PolyMoverFactory {
public void init(GameObject[] objects, int startIndex) { public void init(GameObject[] objects, int startIndex) {
if (objects == null) if (objects == null)
throw new NullPointerException("Objects musn't be null"); throw new NullPointerException("Objects musn't be null");
arc1 = new Arc(objects[startIndex], objects[startIndex + 1], objects[startIndex + 2]);
arc2 = null; GameObject middle = objects[startIndex + 1];
if (middle.isSlider() || middle.isSpinner() || !ArcMover.canCricleExistBetweenItems(objects[startIndex], middle, objects[startIndex + 2]))
current = new LineMover(objects, startIndex, 3);
else
current = new ArcMover(objects[startIndex], middle, objects[startIndex + 2]);
lastIndex = startIndex + 2;
previous = null;
} }
public void update(GameObject p) { public void update(GameObject p) {
GameObject[] items = (arc2 == null ? arc1 : arc2).getItems(); GameObject[] items = (previous == null ? current : previous).getItems();
GameObject last = items[items.length - 1]; GameObject last = items[items.length - 1];
if (last != p) { if (last != p) {
if (arc2 == null) if (ArcMover.canCricleExistBetweenItems(items[items.length - 2], items[items.length - 1], p)) {
arc2 = arc1; previous = current;
arc1 = new Arc(arc2, p); current = new ArcMover(previous, p);
} }
} }
lastIndex++;
}
@Override @Override
public int getPrefferedBufferSize() { public int getPrefferedBufferSize() {
@ -56,6 +66,12 @@ public class ArcFactory implements PolyMoverFactory {
@Override @Override
public boolean isInitialized() { public boolean isInitialized() {
return arc1 != null; return current != null;
} }
@Override
public int getLatestIndex() {
return lastIndex;
}
} }

View File

@ -25,4 +25,6 @@ public interface PolyMoverFactory {
int getPrefferedBufferSize(); int getPrefferedBufferSize();
boolean isInitialized(); boolean isInitialized();
int getLatestIndex();
} }

View File

@ -266,10 +266,9 @@ public class Dancer {
double[] spinnerStartPoint = spinner.getPoint(); double[] spinnerStartPoint = spinner.getPoint();
c.start = new Vec2f((float) spinnerStartPoint[0], (float) spinnerStartPoint[1]); c.start = new Vec2f((float) spinnerStartPoint[0], (float) spinnerStartPoint[1]);
} }
GameObject g = gameObjects[objectIndex + 2];
if (!g.isSpinner() && !g.isSlider()) if (polyMoverFactory.isInitialized() && polyMoverFactory.getLatestIndex() < objectIndex + polyMoverFactory.getPrefferedBufferSize() - 1) {
if (polyMoverFactory.isInitialized()) { polyMoverFactory.update(gameObjects[polyMoverFactory.getPrefferedBufferSize() - 1]);
polyMoverFactory.update(g);
} else { } else {
polyMoverFactory.init(gameObjects, objectIndex); polyMoverFactory.init(gameObjects, objectIndex);
} }