improve transition base

This commit is contained in:
yugecin 2017-01-11 23:02:12 +01:00
parent cb92d45ae8
commit e9120652a1
10 changed files with 138 additions and 104 deletions

View File

@ -21,6 +21,7 @@ import com.google.inject.Inject;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.errorhandling.ErrorHandler; import yugecin.opsudance.errorhandling.ErrorHandler;
import yugecin.opsudance.states.EmptyState;
import static yugecin.opsudance.kernel.Entrypoint.sout; import static yugecin.opsudance.kernel.Entrypoint.sout;
@ -35,7 +36,7 @@ public class OpsuDance {
public void start() { public void start() {
sout("initialized"); sout("initialized");
container.init(); container.switchStateNow(EmptyState.class);
while (rungame()); while (rungame());
} }

View File

@ -22,12 +22,10 @@ import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input; import org.newdawn.slick.Input;
import org.newdawn.slick.KeyListener; import org.newdawn.slick.KeyListener;
import org.newdawn.slick.MouseListener; import org.newdawn.slick.MouseListener;
import yugecin.opsudance.core.state.BaseOpsuState;
import yugecin.opsudance.core.state.transitions.*;
import yugecin.opsudance.kernel.InstanceContainer; import yugecin.opsudance.kernel.InstanceContainer;
import yugecin.opsudance.states.EmptyState;
import yugecin.opsudance.core.state.OpsuState; import yugecin.opsudance.core.state.OpsuState;
import yugecin.opsudance.core.state.transitions.FadeInTransitionState;
import yugecin.opsudance.core.state.transitions.FadeOutTransitionState;
import yugecin.opsudance.core.state.transitions.TransitionState;
/** /**
* state demultiplexer, sends events to current state * state demultiplexer, sends events to current state
@ -36,47 +34,75 @@ public class Demux implements KeyListener, MouseListener {
private final InstanceContainer instanceContainer; private final InstanceContainer instanceContainer;
private TransitionState fadeOutTransitionState; private TransitionState outTransitionState;
private TransitionState fadeInTransitionState; private TransitionState inTransitionState;
private final TransitionFinishedListener outTransitionListener;
private final TransitionFinishedListener inTransitionListener;
private OpsuState state; private OpsuState state;
@Inject @Inject
public Demux(InstanceContainer instanceContainer) { public Demux(final InstanceContainer instanceContainer) {
this.instanceContainer = instanceContainer; this.instanceContainer = instanceContainer;
state = new BaseOpsuState() {
@Override
public void update(int delta) { }
@Override
public void preRenderUpdate(int delta) { }
@Override
public void render(Graphics g) { }
@Override
public void enter() { }
@Override
public void leave() { }
};
outTransitionListener = new TransitionFinishedListener() {
@Override
public void onFinish() {
state.leave();
outTransitionState.getApplicableState().leave();
state = inTransitionState;
state.enter();
inTransitionState.getApplicableState().enter();
}
};
inTransitionListener = new TransitionFinishedListener() {
@Override
public void onFinish() {
state.leave();
state = inTransitionState.getApplicableState();
}
};
} }
// cannot do this in constructor, would cause circular dependency
public void init() {
state = instanceContainer.provide(EmptyState.class);
fadeOutTransitionState = instanceContainer.provide(FadeOutTransitionState.class);
fadeInTransitionState = instanceContainer.provide(FadeInTransitionState.class);
}
public boolean isTransitioning() { public boolean isTransitioning() {
return state == fadeInTransitionState || state == fadeOutTransitionState; return state instanceof TransitionState;
} }
public void switchState(Class<? extends OpsuState> newState) { public void switchState(Class<? extends OpsuState> newState) {
switchState(instanceContainer.provide(newState)); switchState(newState, FadeOutTransitionState.class, 200, FadeInTransitionState.class, 300);
} }
public void switchState(OpsuState newState) { public void switchState(Class<? extends OpsuState> newState, Class<? extends TransitionState> outTransition, int outTime, Class<? extends TransitionState> inTransition, int inTime) {
if (isTransitioning()) { if (isTransitioning()) {
return; return;
} }
fadeOutTransitionState.setApplicableState(state); outTransitionState = instanceContainer.provide(outTransition).set(state, outTime, outTransitionListener);
fadeInTransitionState.setApplicableState(newState); inTransitionState = instanceContainer.provide(inTransition).set(instanceContainer.provide(newState), inTime, inTransitionListener);
state = fadeOutTransitionState; state = outTransitionState;
state.enter(); state.enter();
} }
public void switchStateNow(OpsuState newState) {
if (!isTransitioning()) {
return;
}
state = newState;
}
/* /*
* demux stuff below * demux stuff below
*/ */

View File

@ -31,6 +31,8 @@ import org.newdawn.slick.opengl.renderer.Renderer;
import org.newdawn.slick.opengl.renderer.SGL; import org.newdawn.slick.opengl.renderer.SGL;
import org.newdawn.slick.util.Log; import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.state.OpsuState; import yugecin.opsudance.core.state.OpsuState;
import yugecin.opsudance.core.state.transitions.EmptyTransitionState;
import yugecin.opsudance.core.state.transitions.TransitionState;
import yugecin.opsudance.errorhandling.ErrorDumpable; import yugecin.opsudance.errorhandling.ErrorDumpable;
import yugecin.opsudance.utils.GLHelper; import yugecin.opsudance.utils.GLHelper;
@ -84,16 +86,16 @@ public class DisplayContainer implements ErrorDumpable {
resolutionChangeListeners.add(listener); resolutionChangeListeners.add(listener);
} }
public void switchState(OpsuState newState) {
demux.switchState(newState);
}
public void switchState(Class<? extends OpsuState> newState) { public void switchState(Class<? extends OpsuState> newState) {
demux.switchState(newState); demux.switchState(newState);
} }
public void init() { public void switchStateNow(Class<? extends OpsuState> newState) {
demux.init(); demux.switchState(newState, EmptyTransitionState.class, 0, EmptyTransitionState.class, 0);
}
public void switchState(Class<? extends OpsuState> newState, Class<? extends TransitionState> outTransition, int outTime, Class<? extends TransitionState> inTransition, int inTime) {
demux.switchState(newState, outTransition, outTime, inTransition, inTime);
} }
public void run() throws LWJGLException { public void run() throws LWJGLException {

View File

@ -0,0 +1,27 @@
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.core.state.transitions;
public class EmptyTransitionState extends TransitionState {
@Override
public void enter() {
finish();
}
}

View File

@ -18,17 +18,13 @@
package yugecin.opsudance.core.state.transitions; package yugecin.opsudance.core.state.transitions;
import com.google.inject.Inject; import com.google.inject.Inject;
import yugecin.opsudance.core.Demux;
import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.DisplayContainer;
public class FadeInTransitionState extends FadeTransitionState { public class FadeInTransitionState extends FadeTransitionState {
private final Demux demux;
@Inject @Inject
public FadeInTransitionState(DisplayContainer container, Demux demux) { public FadeInTransitionState(DisplayContainer container) {
super(container, 300); super(container);
this.demux = demux;
} }
@Override @Override
@ -36,15 +32,4 @@ public class FadeInTransitionState extends FadeTransitionState {
return 1f - fadeProgress; return 1f - fadeProgress;
} }
@Override
public void enter() {
super.enter();
applicableState.enter();
}
@Override
protected void onTransitionFinished() {
demux.switchStateNow(applicableState);
}
} }

View File

@ -18,19 +18,13 @@
package yugecin.opsudance.core.state.transitions; package yugecin.opsudance.core.state.transitions;
import com.google.inject.Inject; import com.google.inject.Inject;
import yugecin.opsudance.core.Demux;
import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.DisplayContainer;
public class FadeOutTransitionState extends FadeTransitionState { public class FadeOutTransitionState extends FadeTransitionState {
private final Demux demux;
private final FadeInTransitionState fadeInTransitionState;
@Inject @Inject
public FadeOutTransitionState(DisplayContainer container, Demux demux, FadeInTransitionState fadeInTransitionState) { public FadeOutTransitionState(DisplayContainer container) {
super(container, 200); super(container);
this.demux = demux;
this.fadeInTransitionState = fadeInTransitionState;
} }
@Override @Override
@ -38,11 +32,4 @@ public class FadeOutTransitionState extends FadeTransitionState {
return fadeProgress; return fadeProgress;
} }
@Override
protected void onTransitionFinished() {
applicableState.leave();
demux.switchStateNow(fadeInTransitionState);
fadeInTransitionState.enter();
}
} }

View File

@ -20,55 +20,26 @@ package yugecin.opsudance.core.state.transitions;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics; import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.state.OpsuState;
public abstract class FadeTransitionState extends TransitionState { public abstract class FadeTransitionState extends TransitionState {
protected OpsuState applicableState;
private final DisplayContainer container; private final DisplayContainer container;
protected final int fadeTargetTime;
protected int fadeTime;
private final Color black; private final Color black;
public FadeTransitionState(DisplayContainer container, int fadeTargetTime) { public FadeTransitionState(DisplayContainer container) {
super(fadeTargetTime);
this.container = container; this.container = container;
this.fadeTargetTime = fadeTargetTime;
black = new Color(Color.black); black = new Color(Color.black);
} }
public void setApplicableState(OpsuState applicableState) {
this.applicableState = applicableState;
}
@Override
public void update(int delta) {
applicableState.update(delta);
fadeTime += delta;
if (fadeTime >= fadeTargetTime) {
onTransitionFinished();
}
}
@Override @Override
public void render(Graphics g) { public void render(Graphics g) {
applicableState.render(g); applicableState.render(g);
black.a = getMaskAlphaLevel((float) fadeTime / fadeTargetTime); black.a = getMaskAlphaLevel((float) transitionTime / transitionTargetTime);
g.setColor(black); g.setColor(black);
g.fillRect(0, 0, container.width, container.height); g.fillRect(0, 0, container.width, container.height);
} }
@Override
public void enter() {
fadeTime = 0;
}
@Override
public void leave() { }
protected abstract float getMaskAlphaLevel(float fadeProgress); protected abstract float getMaskAlphaLevel(float fadeProgress);
} }

View File

@ -0,0 +1,24 @@
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.core.state.transitions;
public interface TransitionFinishedListener {
void onFinish();
}

View File

@ -25,15 +25,20 @@ public abstract class TransitionState extends BaseOpsuState {
protected OpsuState applicableState; protected OpsuState applicableState;
protected final int transitionTargetTime; protected int transitionTargetTime;
protected int transitionTime; protected int transitionTime;
public TransitionState(int transitionTargetTime) { private TransitionFinishedListener listener;
this.transitionTargetTime = transitionTargetTime;
public final TransitionState set(OpsuState applicableState, int targetTime, TransitionFinishedListener listener) {
this.applicableState = applicableState;
this.transitionTargetTime = targetTime;
this.listener = listener;
return this;
} }
public void setApplicableState(OpsuState applicableState) { public final OpsuState getApplicableState() {
this.applicableState = applicableState; return applicableState;
} }
@Override @Override
@ -41,7 +46,7 @@ public abstract class TransitionState extends BaseOpsuState {
applicableState.update(delta); applicableState.update(delta);
transitionTime += delta; transitionTime += delta;
if (transitionTime >= transitionTargetTime) { if (transitionTime >= transitionTargetTime) {
onTransitionFinished(); finish();
} }
} }
@ -62,6 +67,8 @@ public abstract class TransitionState extends BaseOpsuState {
@Override @Override
public void leave() { } public void leave() { }
protected abstract void onTransitionFinished(); protected final void finish() {
listener.onFinish();
}
} }

View File

@ -21,6 +21,7 @@ import com.google.inject.AbstractModule;
import yugecin.opsudance.PreStartupInitializer; import yugecin.opsudance.PreStartupInitializer;
import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.Demux; import yugecin.opsudance.core.Demux;
import yugecin.opsudance.core.state.transitions.EmptyTransitionState;
import yugecin.opsudance.states.EmptyRedState; import yugecin.opsudance.states.EmptyRedState;
import yugecin.opsudance.states.EmptyState; import yugecin.opsudance.states.EmptyState;
import yugecin.opsudance.core.state.transitions.FadeInTransitionState; import yugecin.opsudance.core.state.transitions.FadeInTransitionState;
@ -31,12 +32,15 @@ public class OpsuDanceModule extends AbstractModule {
protected void configure() { protected void configure() {
bind(InstanceContainer.class).to(InstanceResolver.class); bind(InstanceContainer.class).to(InstanceResolver.class);
bind(PreStartupInitializer.class).asEagerSingleton(); bind(PreStartupInitializer.class).asEagerSingleton();
bind(Demux.class).asEagerSingleton();
bind(DisplayContainer.class).asEagerSingleton(); bind(DisplayContainer.class).asEagerSingleton();
bind(EmptyTransitionState.class).asEagerSingleton();
bind(FadeInTransitionState.class).asEagerSingleton(); bind(FadeInTransitionState.class).asEagerSingleton();
bind(FadeOutTransitionState.class).asEagerSingleton(); bind(FadeOutTransitionState.class).asEagerSingleton();
bind(EmptyRedState.class).asEagerSingleton(); bind(EmptyRedState.class).asEagerSingleton();
bind(EmptyState.class).asEagerSingleton(); bind(EmptyState.class).asEagerSingleton();
bind(Demux.class).asEagerSingleton();
} }
} }