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

@@ -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;
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 EmptyState implements GameState {
@Inject
public EmptyState() {
private int counter;
private final InstanceContainer instanceContainer;
private final Demux demux;
@Inject
public EmptyState(InstanceContainer instanceContainer, Demux demux) {
this.instanceContainer = instanceContainer;
this.demux = demux;
}
@Override
public void update(int delta) {
System.out.println("updatin' " + delta);
counter -= delta;
if (counter < 0) {
demux.switchState(instanceContainer.provide(EmptyRedState.class));
}
}
@Override
public void render(Graphics g) {
System.out.println("renderin'");
g.setColor(Color.green);
g.fillRect(0, 0, 100, 100);
}
@Override
public void enter() {
System.out.println("entered");
counter = 2000;
}
@Override
public void leave() {
System.out.println("left");
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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 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();
}