From e18eba17de2535734dbeadb308f673a05b34049b Mon Sep 17 00:00:00 2001 From: yugecin Date: Sun, 20 Nov 2016 18:35:20 +0100 Subject: [PATCH] make dancer code more resilient to storyboard changes --- src/itdelatrisu/opsu/states/Game.java | 4 +- src/yugecin/opsudance/Dancer.java | 59 ++++++++++++++++++--------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/itdelatrisu/opsu/states/Game.java b/src/itdelatrisu/opsu/states/Game.java index ccb4f1f9..91fc1c0a 100644 --- a/src/itdelatrisu/opsu/states/Game.java +++ b/src/itdelatrisu/opsu/states/Game.java @@ -328,6 +328,7 @@ public class Game extends BasicGameState { objectIndex++; } objectIndex--; + Dancer.instance.setObjectIndex(objectIndex); sbOverlay.updateIndex(objectIndex); lastReplayTime = beatmap.objects[objectIndex].getTime(); } catch (SlickException e) { @@ -1450,13 +1451,13 @@ public class Game extends BasicGameState { } + Dancer.instance.setGameObjects(gameObjects); sbOverlay.setGameObjects(gameObjects); if (!checkpointLoaded) { sbOverlay.enter(); sbOverlay.updateIndex(0); } - Dancer.instance.setGameObjects(gameObjects); Pippi.reset(); mirrorFrom = 0; mirrorTo = gameObjects.length; @@ -1473,6 +1474,7 @@ public class Game extends BasicGameState { // container.setMouseGrabbed(false); sbOverlay.leave(); + Dancer.instance.setGameObjects(null); Cursor.lastObjColor = Color.white; Cursor.lastMirroredObjColor = Color.white; diff --git a/src/yugecin/opsudance/Dancer.java b/src/yugecin/opsudance/Dancer.java index 43ed9c9c..0e47156a 100644 --- a/src/yugecin/opsudance/Dancer.java +++ b/src/yugecin/opsudance/Dancer.java @@ -97,9 +97,9 @@ public class Dancer { private int dir; public static final GameObject d = new DummyObject(); - private GameObject p; private GameObject[] gameObjects; + private int objectIndex; private MoverFactory moverFactory; private Mover mover; @@ -124,7 +124,7 @@ public class Dancer { public void reset() { isCurrentLazySlider = false; - p = null; + objectIndex = -1; dir = 1; for (Spinner s : spinners) { s.init(); @@ -154,6 +154,8 @@ public class Dancer { this.moverFactoryIndex = moverFactoryIndex; moverFactory = moverFactories[moverFactoryIndex]; multipoint = moverFactory.isMultiPoint(); + // to prevent crashes when changing mover in storyboard, create mover now + createNewMover(); } public int getPolyMoverFactoryMinBufferSize() { @@ -167,6 +169,12 @@ public class Dancer { this.gameObjects = objs; } + public void setObjectIndex(int objectIndex) { + this.objectIndex = objectIndex; + // storyboard + createNewMover(); + } + public void update(int time, int objectIndex) { GameObject p; if (objectIndex == 0) { @@ -180,9 +188,9 @@ public class Dancer { p = e[0]; c = e[1]; } - if (this.p != p) { - this.p = p; - if (this.p == d) { + if (this.objectIndex != objectIndex) { + this.objectIndex = objectIndex; + if (objectIndex == 0) { if (c.isSpinner()) { double[] spinnerStartPoint = spinner.getPoint(); c.start.set((float) spinnerStartPoint[0], (float) spinnerStartPoint[1]); @@ -206,20 +214,7 @@ public class Dancer { c.start = new Vec2f((float) spinnerStartPoint[0], (float) spinnerStartPoint[1]); } - if (multipoint) { - PolyMoverFactory pmf = (PolyMoverFactory) moverFactory; - if (pmf.isInitialized() && pmf.getLatestIndex() < objectIndex + pmf.getLatestIndex() - 1) { - pmf.update(gameObjects[objectIndex + pmf.getMaxBufferSize() - 1]); - } else { - pmf.create(gameObjects, objectIndex - 1); - } - } else if (mover == null || mover.getEnd() != c) { - if (p == d) { - mover = new LinearMover(p, c, dir); - } else { - mover = moverFactory.create(p, c, dir); - } - } + createNewMover(); } if (time < c.getTime()) { @@ -254,4 +249,30 @@ public class Dancer { y = Utils.clamp(y, 10, Options.height - 10); } + private void createNewMover() { + if (gameObjects == null) { + return; + } + if (objectIndex < 0) { + objectIndex = 0; + } + GameObject c = gameObjects[objectIndex]; + if (multipoint) { + PolyMoverFactory pmf = (PolyMoverFactory) moverFactory; + if (pmf.isInitialized() && pmf.getLatestIndex() < objectIndex + pmf.getLatestIndex() - 1) { + pmf.update(gameObjects[objectIndex + pmf.getMaxBufferSize() - 1]); + } else { + pmf.create(gameObjects, objectIndex - 1); + } + return; + } + if (mover == null || mover.getEnd() != c) { + if (objectIndex == 0) { + mover = new LinearMover(d, c, dir); + } else { + mover = moverFactory.create(gameObjects[objectIndex - 1], c, dir); + } + } + } + }