refactor state transition code

This commit is contained in:
yugecin 2018-06-25 22:58:07 +02:00
parent a9e66df07a
commit 6dd343da83
No known key found for this signature in database
GPG Key ID: 2C5AC035A7068E44

View File

@ -101,45 +101,11 @@ public class DisplayContainer implements ErrorDumpable, SkinChangedListener {
private final List<ResolutionChangedListener> resolutionChangedListeners; private final List<ResolutionChangedListener> resolutionChangedListeners;
class Transition { private int tIn;
int in; private int tOut;
int out; private int tProgress = -1;
int total; private OpsuState tNextState;
int progress = -1; private final Color tOVERLAY = new Color(Color.black);
OpsuState nextstate;
Color OVERLAY = new Color(Color.black);
public void update() {
if (progress == -1) {
return;
}
progress += delta;
if (progress > out && nextstate != null) {
switchStateInstantly(nextstate);
nextstate = null;
}
if (progress > total) {
progress = -1;
}
}
public void render(Graphics graphics) {
if (progress == -1) {
return;
}
int relprogress = progress;
int reltotal = out;
if (progress > out) {
reltotal = in;
relprogress = total - progress;
}
OVERLAY.a = (float) relprogress / reltotal;
graphics.setColor(OVERLAY);
graphics.fillRect(0, 0, width, height);
}
}
private final Transition transition = new Transition();
public DisplayContainer() public DisplayContainer()
{ {
@ -220,7 +186,17 @@ public class DisplayContainer implements ErrorDumpable, SkinChangedListener {
mouseX = input.getMouseX(); mouseX = input.getMouseX();
mouseY = input.getMouseY(); mouseY = input.getMouseY();
transition.update(); // state transition
if (tProgress != -1) {
tProgress += delta;
if (tProgress > tOut && tNextState != null) {
switchStateInstantly(tNextState);
tNextState = null;
}
if (tProgress > tIn + tOut) {
tProgress = -1;
}
}
fpsDisplay.update(); fpsDisplay.update();
state.update(); state.update();
@ -260,7 +236,16 @@ public class DisplayContainer implements ErrorDumpable, SkinChangedListener {
} }
UI.drawTooltip(graphics); UI.drawTooltip(graphics);
transition.render(graphics); // transition
if (tProgress != -1) {
if (tProgress > tOut) {
tOVERLAY.a = 1f - (tProgress - tOut) / (float) tIn;
} else {
tOVERLAY.a = tProgress / (float) tOut;
}
graphics.setColor(tOVERLAY);
graphics.fillRect(0, 0, width, height);
}
timeSinceLastRender = 0; timeSinceLastRender = 0;
@ -491,18 +476,17 @@ public class DisplayContainer implements ErrorDumpable, SkinChangedListener {
} }
public void switchState(OpsuState newstate, int outtime, int intime) { public void switchState(OpsuState newstate, int outtime, int intime) {
if (transition.progress != -1) { if (tProgress != -1) {
return; return;
} }
if (outtime == 0) { if (outtime == 0) {
switchStateInstantly(newstate); switchStateInstantly(newstate);
newstate = null; newstate = null;
} }
transition.nextstate = newstate; tNextState = newstate;
transition.total = transition.in = intime; tIn = intime;
transition.out = outtime; tOut = outtime;
transition.total += outtime; tProgress = 0;
transition.progress = 0;
} }
public void switchStateInstantly(OpsuState state) { public void switchStateInstantly(OpsuState state) {