switching states using fade transitions

This commit is contained in:
yugecin 2017-01-07 23:14:10 +01:00
parent ebda622c2c
commit 6d8f48a03f
8 changed files with 276 additions and 17 deletions

View File

@ -30,6 +30,7 @@ public class Container extends AppGameContainer {
@Inject @Inject
public Container(Demux demux) throws SlickException { public Container(Demux demux) throws SlickException {
super(demux); super(demux);
setShowFPS(false);
} }
@Override @Override

View File

@ -18,19 +18,22 @@
package yugecin.opsudance.core; package yugecin.opsudance.core;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.newdawn.slick.Game; import org.newdawn.slick.*;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import yugecin.opsudance.kernel.InstanceContainer; import yugecin.opsudance.kernel.InstanceContainer;
import yugecin.opsudance.states.EmptyState; import yugecin.opsudance.states.EmptyState;
import yugecin.opsudance.states.GameState; import yugecin.opsudance.states.GameState;
import yugecin.opsudance.states.transitions.FadeInTransitionState;
import yugecin.opsudance.states.transitions.FadeOutTransitionState;
import yugecin.opsudance.states.transitions.TransitionState;
public class Demux implements Game { public class Demux implements Game {
private final InstanceContainer instanceContainer; private final InstanceContainer instanceContainer;
private GameState currentState; private TransitionState fadeOutTransitionState;
private TransitionState fadeInTransitionState;
private GameState state;
@Inject @Inject
public Demux(InstanceContainer instanceContainer) { public Demux(InstanceContainer instanceContainer) {
@ -39,22 +42,26 @@ public class Demux implements Game {
@Override @Override
public void init(GameContainer container) throws SlickException { public void init(GameContainer container) throws SlickException {
currentState = instanceContainer.provide(EmptyState.class); fadeOutTransitionState = instanceContainer.provide(FadeOutTransitionState.class);
fadeInTransitionState = instanceContainer.provide(FadeInTransitionState.class);
state = instanceContainer.provide(EmptyState.class);
state.enter();
} }
@Override @Override
public void update(GameContainer container, int delta) throws SlickException { public void update(GameContainer container, int delta) throws SlickException {
currentState.update(delta); state.update(delta);
} }
@Override @Override
public void render(GameContainer container, Graphics g) throws SlickException { public void render(GameContainer container, Graphics g) throws SlickException {
currentState.render(g); state.render(g);
} }
@Override @Override
public boolean closeRequested() { public boolean closeRequested() {
return false; // TODO for what is this used
return !isTransitioning();
} }
@Override @Override
@ -62,4 +69,25 @@ public class Demux implements Game {
return "opsu!dance"; return "opsu!dance";
} }
public boolean isTransitioning() {
return state == fadeInTransitionState || state == fadeOutTransitionState;
}
public void switchState(GameState newState) {
if (isTransitioning()) {
return;
}
fadeOutTransitionState.setApplicableState(state);
fadeInTransitionState.setApplicableState(newState);
state = fadeOutTransitionState;
state.enter();
}
public void switchStateNow(GameState newState) {
if (!isTransitioning()) {
return;
}
state = newState;
}
} }

View File

@ -21,6 +21,10 @@ import com.google.inject.AbstractModule;
import yugecin.opsudance.PreStartupInitializer; import yugecin.opsudance.PreStartupInitializer;
import yugecin.opsudance.core.Container; import yugecin.opsudance.core.Container;
import yugecin.opsudance.core.Demux; import yugecin.opsudance.core.Demux;
import yugecin.opsudance.states.EmptyRedState;
import yugecin.opsudance.states.EmptyState;
import yugecin.opsudance.states.transitions.FadeInTransitionState;
import yugecin.opsudance.states.transitions.FadeOutTransitionState;
public class OpsuDanceModule extends AbstractModule { public class OpsuDanceModule extends AbstractModule {
@ -29,6 +33,10 @@ public class OpsuDanceModule extends AbstractModule {
bind(PreStartupInitializer.class).asEagerSingleton(); bind(PreStartupInitializer.class).asEagerSingleton();
bind(Demux.class).asEagerSingleton(); bind(Demux.class).asEagerSingleton();
bind(Container.class).asEagerSingleton(); bind(Container.class).asEagerSingleton();
bind(FadeInTransitionState.class).asEagerSingleton();
bind(FadeOutTransitionState.class).asEagerSingleton();
bind(EmptyRedState.class).asEagerSingleton();
bind(EmptyState.class).asEagerSingleton();
} }
} }

View File

@ -0,0 +1,62 @@
/*
* 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.states;
import com.google.inject.Inject;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.Demux;
import yugecin.opsudance.kernel.InstanceContainer;
public class EmptyRedState implements GameState {
private int counter;
private final Demux demux;
private final InstanceContainer instanceContainer;
@Inject
public EmptyRedState(InstanceContainer instanceContainer, Demux demux) {
this.instanceContainer = instanceContainer;
this.demux = demux;
}
@Override
public void update(int delta) {
counter -= delta;
if (counter < 0) {
demux.switchState(instanceContainer.provide(EmptyState.class));
}
}
@Override
public void render(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, 100, 100);
}
@Override
public void enter() {
counter = 5000;
}
@Override
public void leave() {
}
}

View File

@ -18,33 +18,45 @@
package yugecin.opsudance.states; package yugecin.opsudance.states;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics; import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.Demux;
import yugecin.opsudance.kernel.InstanceContainer;
public class EmptyState implements GameState { public class EmptyState implements GameState {
@Inject private int counter;
public EmptyState() {
private final InstanceContainer instanceContainer;
private final Demux demux;
@Inject
public EmptyState(InstanceContainer instanceContainer, Demux demux) {
this.instanceContainer = instanceContainer;
this.demux = demux;
} }
@Override @Override
public void update(int delta) { public void update(int delta) {
System.out.println("updatin' " + delta); counter -= delta;
if (counter < 0) {
demux.switchState(instanceContainer.provide(EmptyRedState.class));
}
} }
@Override @Override
public void render(Graphics g) { public void render(Graphics g) {
System.out.println("renderin'"); g.setColor(Color.green);
g.fillRect(0, 0, 100, 100);
} }
@Override @Override
public void enter() { public void enter() {
System.out.println("entered"); counter = 2000;
} }
@Override @Override
public void leave() { public void leave() {
System.out.println("left");
} }
} }

View File

@ -15,9 +15,36 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>. * along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/ */
package yugecin.opsudance.core; package yugecin.opsudance.states.transitions;
public class ContainerWrapper { import com.google.inject.Inject;
import yugecin.opsudance.core.Container;
import yugecin.opsudance.core.Demux;
public class FadeInTransitionState extends TransitionState {
private final Demux demux;
@Inject
public FadeInTransitionState(Container container, Demux demux) {
super(container, 300);
this.demux = demux;
}
@Override
protected float getMaskAlphaLevel() {
return 1f - (float) fadeTime / fadeTargetTime;
}
@Override
public void enter() {
super.enter();
applicableState.enter();
}
@Override
protected void onFadeFinished() {
demux.switchStateNow(applicableState);
}
} }

View File

@ -0,0 +1,47 @@
/*
* 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.states.transitions;
import com.google.inject.Inject;
import yugecin.opsudance.core.Container;
import yugecin.opsudance.core.Demux;
public class FadeOutTransitionState extends TransitionState {
private final Demux demux;
private final FadeInTransitionState fadeInTransitionState;
@Inject
public FadeOutTransitionState(Container container, Demux demux, FadeInTransitionState fadeInTransitionState) {
super(container, 200);
this.demux = demux;
this.fadeInTransitionState = fadeInTransitionState;
}
@Override
protected float getMaskAlphaLevel() {
return (float) fadeTime / fadeTargetTime;
}
@Override
protected void onFadeFinished() {
applicableState.leave();
demux.switchStateNow(fadeInTransitionState);
fadeInTransitionState.enter();
}
}

View File

@ -0,0 +1,74 @@
/*
* 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.states.transitions;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.Container;
import yugecin.opsudance.states.GameState;
public abstract class TransitionState implements GameState {
protected GameState applicableState;
private final Container container;
protected final int fadeTargetTime;
protected int fadeTime;
private final Color black;
public TransitionState(Container container, int fadeTargetTime) {
this.container = container;
this.fadeTargetTime = fadeTargetTime;
black = new Color(Color.black);
}
public void setApplicableState(GameState applicableState) {
this.applicableState = applicableState;
}
@Override
public void update(int delta) {
applicableState.update(delta);
fadeTime += delta;
if (fadeTime >= fadeTargetTime) {
onFadeFinished();
}
}
@Override
public void render(Graphics g) {
applicableState.render(g);
black.a = getMaskAlphaLevel();
g.setColor(black);
g.fillRect(0, 0, container.getWidth(), container.getHeight());
}
@Override
public void enter() {
fadeTime = 0;
}
@Override
public void leave() { }
protected abstract float getMaskAlphaLevel();
protected abstract void onFadeFinished();
}