Remodeled PolyMoverFactories and Movers, to be more effizient and userfriendly

This commit is contained in:
Awlex 2016-11-20 04:42:46 +01:00
parent 5a5c13f64c
commit b36d7d487f
9 changed files with 203 additions and 120 deletions

View File

@ -10,8 +10,9 @@ import static java.lang.Math.*;
/**
* Created by Awlex on 13.11.2016.
*/
public class ArcMover extends PolyMover {
public class ArcMover implements PolyMover {
public static final int ITEMS_NEEDED = 3;
private GameObject p1, middle, p2;
private double xm, ym, r, alpha, beta, gamma;
@ -20,14 +21,7 @@ public class ArcMover extends PolyMover {
this.middle = middle;
this.p2 = p2;
init();
}
public ArcMover(PolyMover mover, GameObject p) {
GameObject[] items = mover.getItems();
p1 = items[items.length - 2];
middle = items[items.length - 1];
p2 = p;
init();
System.out.println("r: " + r + " xm: " + xm + " ym: " + ym);
}
private void init() {
@ -64,10 +58,11 @@ public class ArcMover extends PolyMover {
p2
};
}
private static Matrix prepareMatrix(GameObject p1, GameObject middle, GameObject p2) {
Matrix a, b;
if (!p2.isSlider()) {
//if (!p2.isSlider()) {
a = new Matrix(new double[][]{
{1, -p1.end.x, -p1.end.y},
{1, -middle.start.x, -middle.start.y},
@ -78,6 +73,7 @@ public class ArcMover extends PolyMover {
{-(pow(middle.start.x, 2) + pow(middle.start.y, 2))},
{-(pow(p2.start.x, 2) + pow(p2.start.y, 2))},
});
/*
} else {
Circle c = ((Slider) p2).getTickPositionCircles()[0];
a = new Matrix(new double[][]{
@ -91,6 +87,7 @@ public class ArcMover extends PolyMover {
{-(pow(c.start.x, 2) + pow(c.start.y, 2))},
});
}
*/
return a.solve(b);
}

View File

@ -2,30 +2,43 @@ package awlex.ospu.polymover;
import itdelatrisu.opsu.objects.DummyObject;
import itdelatrisu.opsu.objects.GameObject;
import itdelatrisu.opsu.states.Game;
import yugecin.opsudance.movers.LinearMover;
import java.util.Arrays;
/**
* Created by Awlex on 19.11.2016.
*/
public class LineMover extends PolyMover {
GameObject[] objects;
public class LineMover implements PolyMover {
public static final int ITEMS_NEEDED = 2;
private GameObject[] objects;
private LinearMover m;
private int currentIndex;
public LineMover(GameObject[] objects, int startIndex, int count) {
this.objects = new GameObject[count];
System.arraycopy(objects, startIndex, this.objects, 0, count - 1);
this.objects = objects;
m = new LinearMover(objects[startIndex], objects[startIndex + 1], 1);
currentIndex = startIndex + 1;
}
public LineMover(GameObject[] objects, int count) {
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);
if (objects[currentIndex].getEndTime() < time)
m = new LinearMover(objects[currentIndex], objects[currentIndex + 1], 1);
return m.getPointAt(time);
}
@Override
public GameObject[] getItems() {
return objects;
}
}

View File

@ -5,8 +5,9 @@ import itdelatrisu.opsu.objects.GameObject;
/**
* Created by Awlex on 18.11.2016.
*/
public abstract class PolyMover {
public abstract double[] getPointAt(int time);
public abstract GameObject[] getItems();
public interface PolyMover {
double[] getPointAt(int time);
GameObject[] getItems();
}

View File

@ -11,73 +11,26 @@ import java.util.Arrays;
/**
* Created by Awlex on 18.11.2016.
*/
public class ArcFactory implements PolyMoverFactory {
public class ArcFactory extends PolyMoverFactory {
private static final int PREFFERED_BUFFER_SIZE = 3;
private PolyMover current, previous;
private int lastIndex;
public double[] getPointAt(int time) {
if (previous == null) {
return current.getPointAt(time);
}
if (current == null) {
return previous.getPointAt(time);
}
double[] point1 = current.getPointAt(time);
double[] point2 = previous.getPointAt(time);
return new double[]{
(point1[0] + point2[0]) * 0.5,
(point1[1] + point2[1]) * 0.5
};
}
public void init(GameObject[] objects, int startIndex) {
if (objects == null)
throw new NullPointerException("Objects musn't be null");
boolean b;
if (b = startIndex == -1) {
objects = new GameObject[]{
new DummyObject(),
objects[0],
objects[1],
};
startIndex = 0;
}
GameObject middle = objects[startIndex + 1];
if (!ArcMover.canCricleExistBetweenItems(objects[startIndex], middle, objects[startIndex + 2]))
current = new LineMover(objects, startIndex, 3);
public void init(GameObject[] objects, int count) {
System.out.println("count: " + count);
if (count < 3 || !ArcMover.canCricleExistBetweenItems(objects[0], objects[1], objects[2]))
addMover(new LineMover(objects, 3));
else
current = new ArcMover(objects[startIndex], middle, objects[startIndex + 2]);
lastIndex = startIndex + 2;
if (b)
lastIndex--;
previous = null;
}
public void update(GameObject p) {
GameObject[] items = (previous == null ? current : previous).getItems();
GameObject last = items[items.length - 1];
if (last != p) {
previous = current;
if (ArcMover.canCricleExistBetweenItems(items[items.length - 2], last, p)) {
current = new ArcMover(items[items.length - 2], last, p);
} else {
GameObject[] lineObjects = new GameObject[PREFFERED_BUFFER_SIZE];
System.arraycopy(items, items.length - 2, lineObjects, 0, PREFFERED_BUFFER_SIZE - 2);
lineObjects[lineObjects.length - 1] = p;
current = new LineMover(lineObjects, 0, PREFFERED_BUFFER_SIZE);
}
}
lastIndex++;
addMover(new ArcMover(objects[0], objects[1], objects[2]));
}
@Override
public int getPrefferedBufferSize() {
return PREFFERED_BUFFER_SIZE;
public int getMaxBufferSize() {
return ArcMover.ITEMS_NEEDED;
}
@Override
public int getMinBufferSize() {
return ArcMover.ITEMS_NEEDED;
}
@Override
@ -85,14 +38,4 @@ public class ArcFactory implements PolyMoverFactory {
return "Arcs";
}
@Override
public boolean isInitialized() {
return current != null;
}
@Override
public int getLatestIndex() {
return lastIndex;
}
}

View File

@ -0,0 +1,38 @@
package awlex.ospu.polymover.factory;
import awlex.ospu.polymover.LineMover;
import itdelatrisu.opsu.objects.GameObject;
/**
* Created by Awlex on 20.11.2016.
*/
public class LinearFactory extends PolyMoverFactory {
public final static int PREFFERED_BUFFER_SIZE = 2;
@Override
public double[] getPointAt(int time) {
return getCurrent().getPointAt(time);
}
@Override
public void init(GameObject[] objects, int count) {
addMover(new LineMover(objects, count));
}
@Override
public int getMaxBufferSize() {
return LineMover.ITEMS_NEEDED;
}
@Override
public int getMinBufferSize() {
return LineMover.ITEMS_NEEDED;
}
@Override
public String toString() {
return "Linear";
}
}

View File

@ -1,30 +1,122 @@
package awlex.ospu.polymover.factory;
import awlex.ospu.polymover.LineMover;
import awlex.ospu.polymover.PolyMover;
import itdelatrisu.opsu.objects.DummyObject;
import itdelatrisu.opsu.objects.GameObject;
import java.util.LinkedList;
import java.util.List;
/**
* Created by Awlex on 18.11.2016.
*/
public interface PolyMoverFactory {
public abstract class PolyMoverFactory {
private LinkedList<PolyMover> movers;
private int latestIndex;
public PolyMoverFactory() {
movers = new LinkedList<>();
}
/**
* @param time point in time whose cursor position has to be calculated
* @return [x, y]
*/
double[] getPointAt(int time);
void init(GameObject[] objects, int startIndex);
void update(GameObject g);
public double[] getPointAt(int time) {
double[] ret = new double[2];
int i = 0;
while (i < movers.size()) {
GameObject[] items = movers.get(i).getItems();
if (items[items.length - 1].getEndTime() < time)
break;
double[] point = movers.get(i).getPointAt(time);
ret[0] += point[0];
ret[1] += point[1];
i++;
}
ret[0] /= i;
ret[1] /= i;
return ret;
}
public final void create(GameObject[] objects, int startIndex) {
if (latestIndex <= startIndex) {
movers.clear();
}
GameObject[] items = new GameObject[getMaxBufferSize()];
int i = 1;
items[0] = startIndex == -1 ? new DummyObject() : objects[startIndex];
while (i < items.length - 2) {
GameObject g = objects[startIndex + i];
if (g.isSlider() || g.isSpinner())
break;
items[i++] = g;
}
items[i] = objects[startIndex + i];
i++;
latestIndex = startIndex + getMaxBufferSize() + i - items.length;
if (i >= getMinBufferSize()) {
init(items, i);
}
else {
addMover(new LineMover(objects, startIndex + 1, i));
}
}
protected abstract void init(GameObject[] objects, int count);
public final void update(GameObject g) {
GameObject[] items = movers.get(movers.size() - 1).getItems();
if (items[items.length - 1] != g) {
System.arraycopy(items, 1, items, 0, items.length - 1);
items[items.length - 1] = g;
create(items, 0);
latestIndex++;
}
}
/**
* How many items the Factory would like to look in the future
* How many items the Factory would like to look in the future at most
*
* @return
*/
int getPrefferedBufferSize();
boolean isInitialized();
int getLatestIndex();
public abstract int getMaxBufferSize();
/**
* How many items the Factory would like to look in the future at least
*
* @return
*/
public abstract int getMinBufferSize();
public boolean isInitialized() {
return movers.isEmpty();
}
protected PolyMover getCurrent() {
return movers.peekLast();
}
protected List<PolyMover> getMovers() {
return movers;
}
/**
* Adds a Mover to the end of the list. It will also remove the first mover, if the list is bigger than the buffersize.
*
* @param mover the mover to be added
*/
protected void addMover(PolyMover mover) {
movers.add(mover);
if (movers.size() >= getMaxBufferSize())
movers.remove();
}
public int getLatestIndex() {
return latestIndex;
}
}

View File

@ -617,7 +617,7 @@ public class Options {
@Override
public void clickListItem(int index) {
if (Dancer.multipoint)
Dancer.instance.setMoverFactoryIndex(index);
Dancer.instance.setPolyMoverFactoryIndex(index);
else
Dancer.instance.setMoverFactoryIndex(index);
}

View File

@ -421,7 +421,7 @@ public class Game extends BasicGameState {
autoMousePressed = false;
if (GameMod.AUTO.isActive() || GameMod.AUTOPILOT.isActive()) {
Vec2f autoPoint;
if (objectIndex < beatmap.objects.length - (Dancer.multipoint ? Dancer.polyMoverFactories[Dancer.instance.getPolyMoverFactoryIndex()].getPrefferedBufferSize() : 0)) {
if (objectIndex < beatmap.objects.length - (Dancer.multipoint ? Dancer.polyMoverFactories[Dancer.instance.getPolyMoverFactoryIndex()].getMinBufferSize() : 0)) {
Dancer d = Dancer.instance;
if (Dancer.multipoint) {
d.update(trackPosition, gameObjects, objectIndex);

View File

@ -19,8 +19,8 @@ package yugecin.opsudance;
import awlex.ospu.movers.factories.CenterSpiralMoverFactory;
import awlex.ospu.movers.factories.SpiralMoverFactory;
import awlex.ospu.polymover.PolyMover;
import awlex.ospu.polymover.factory.ArcFactory;
import awlex.ospu.polymover.factory.LinearFactory;
import awlex.ospu.polymover.factory.PolyMoverFactory;
import awlex.ospu.spinners.SpiralSpinner;
import itdelatrisu.opsu.Options;
@ -74,6 +74,7 @@ public class Dancer {
};
public static PolyMoverFactory[] polyMoverFactories = new PolyMoverFactory[]{
new LinearFactory(),
new ArcFactory()
};
@ -267,13 +268,11 @@ public class Dancer {
c.start = new Vec2f((float) spinnerStartPoint[0], (float) spinnerStartPoint[1]);
}
if (polyMoverFactory.getLatestIndex() < objectIndex + polyMoverFactory.getPrefferedBufferSize() - 1) {
if (polyMoverFactory.isInitialized()) {
polyMoverFactory.update(gameObjects[objectIndex + polyMoverFactory.getPrefferedBufferSize()]);
}
else {
polyMoverFactory.init(gameObjects, objectIndex - 1);
}
if (polyMoverFactory.isInitialized() && polyMoverFactory.getLatestIndex() < objectIndex + polyMoverFactory.getLatestIndex() - 1) {
polyMoverFactory.update(gameObjects[objectIndex + polyMoverFactory.getMaxBufferSize() - 1]);
}
else {
polyMoverFactory.create(gameObjects, objectIndex - 1);
}
}