Skip to content

Commit

Permalink
Adding transitions effects for screens
Browse files Browse the repository at this point in the history
  • Loading branch information
guidota committed Jun 18, 2019
1 parent 5b9bb0a commit fb34870
Show file tree
Hide file tree
Showing 11 changed files with 532 additions and 23 deletions.
21 changes: 9 additions & 12 deletions client/src/game/AOGame.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package game;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Interpolation;
import com.esotericsoftware.minlog.Log;
import game.handlers.AssetHandler;
import game.handlers.StateHandler;
import game.screens.ScreenEnum;
import game.screens.ScreenManager;
import game.screens.transitions.ColorFadeTransition;
import game.screens.transitions.FadingGame;
import game.screens.transitions.SlidingTransition;
import game.utils.Cursors;
import shared.model.lobby.Player;

Expand All @@ -18,14 +20,17 @@
* <p>
* This should be the primary instance of the app.
*/
public class AOGame extends Game {
public class AOGame extends FadingGame {

public static final float GAME_SCREEN_ZOOM = 1f;
public static final float GAME_SCREEN_MAX_ZOOM = 1.3f;

@Override
public void create() {
super.create();
Gdx.app.debug("AOGame", "Creating AOGame...");
setTransition(new ColorFadeTransition(Color.BLACK, Interpolation.exp10), 1.0f);


// @todo load platform-independent configuration (network, etc.)

Expand All @@ -38,14 +43,6 @@ public void create() {
toLogin();
}

@Override
public void render() {
GL20 gl = Gdx.gl;
gl.glClearColor(0.0f, 0.0f, 0.0f, 1f);
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
super.render();
}

public void toGame(String host, int port, Player player) {
ScreenManager.getInstance().showScreen(ScreenEnum.GAME, host, port, player);
}
Expand Down
2 changes: 0 additions & 2 deletions client/src/game/network/ClientResponseProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public void processResponse(StartGameResponse startGameResponse) {
clientSystem.start();
gameScreen.initWorld(clientSystem);
clientSystem.getKryonetClient().sendToAll(new PlayerLoginRequest(roomScreen.getPlayer()));
Screen currentScreen = game.getScreen();
game.setScreen(gameScreen);
currentScreen.dispose();
}
}

Expand Down
9 changes: 0 additions & 9 deletions client/src/game/screens/ScreenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,9 @@ public void initialize(AOGame game) {

// Show in the game the screen which enum type is received
public void showScreen(ScreenEnum screenEnum, Object... params) {

// Get current screen to dispose it
Screen currentScreen = game.getScreen();

// Show new screen
Screen newScreen = screenEnum.getScreen(params);
game.setScreen(newScreen);

// Dispose previous screen
if (currentScreen != null) {
currentScreen.dispose();
}
}

}
25 changes: 25 additions & 0 deletions client/src/game/screens/transitions/AlphaFadingTransition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package game.screens.transitions;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Interpolation;

public class AlphaFadingTransition implements ScreenTransition {
public AlphaFadingTransition() {
}

public void render(Batch batch, Texture currentScreenTexture, Texture nextScreenTexture, float alpha) {
alpha = Interpolation.fade.apply(alpha);
batch.begin();
batch.setColor(1.0F, 1.0F, 1.0F, 1.0F);
batch.draw(currentScreenTexture, 0.0F, 0.0F, 0.0F, 0.0F, (float) currentScreenTexture.getWidth(), (float) currentScreenTexture.getHeight(), 1.0F, 1.0F, 0.0F, 0, 0, currentScreenTexture.getWidth(), currentScreenTexture.getHeight(), false, true);
batch.setColor(1.0F, 1.0F, 1.0F, alpha);
batch.draw(nextScreenTexture, 0.0F, 0.0F, 0.0F, 0.0F, (float) nextScreenTexture.getWidth(), (float) nextScreenTexture.getHeight(), 1.0F, 1.0F, 0.0F, 0, 0, nextScreenTexture.getWidth(), nextScreenTexture.getHeight(), false, true);
batch.end();
}
}
76 changes: 76 additions & 0 deletions client/src/game/screens/transitions/ColorFadeTransition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package game.screens.transitions;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Interpolation;

/**
* A color fading transition
*
* @author iXeption
*/
public class ColorFadeTransition implements ScreenTransition {

private final Color color;
private final Interpolation interpolation;
private final Texture texture;

/**
* @param color the {@link Color} to fade to
* @param interpolation the {@link Interpolation} method
*/
public ColorFadeTransition(Color color, Interpolation interpolation) {
this.color = new Color(Color.WHITE);
this.interpolation = interpolation;

texture = new Texture(1, 1, Format.RGBA8888);
Pixmap pixmap = new Pixmap(1, 1, Format.RGBA8888);
pixmap.setColor(color);
pixmap.fillRectangle(0, 0, 1, 1);
texture.draw(pixmap, 0, 0);
}

@Override
public void render(Batch batch, Texture currentScreenTexture, Texture nextScreenTexture, float percent) {
float width = currentScreenTexture.getWidth();
float height = currentScreenTexture.getHeight();
float x = 0;
float y = 0;

if (interpolation != null) percent = interpolation.apply(percent);

batch.begin();

float fade = percent * 2;

if (fade > 1.0f) {
fade = 1.0f - (percent * 2 - 1.0f);
color.a = 1.0f - fade;
batch.setColor(color);

batch.draw(nextScreenTexture, 0, 0, width / 2, height / 2, nextScreenTexture.getWidth(), nextScreenTexture.getHeight(),
1, 1, 0, 0, 0, nextScreenTexture.getWidth(), nextScreenTexture.getHeight(), false, true);

} else {

color.a = 1.0f - fade;
batch.setColor(color);

batch.draw(currentScreenTexture, 0, 0, width / 2, height / 2, width, height, 1, 1, 0, 0, 0, (int) width, (int) height,
false, true);

}

color.a = fade;

batch.setColor(color);
batch.draw(texture, 0, 0, width, height);
batch.end();
batch.setColor(Color.WHITE);

}

}
188 changes: 188 additions & 0 deletions client/src/game/screens/transitions/FadingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package game.screens.transitions;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.utils.Array;
import java.util.Iterator;

public class FadingGame extends Game {
protected Batch batch;
private final Array<TransitionListener> listeners;
protected FrameBuffer currentScreenFBO;
protected FrameBuffer nextScreenFBO;
protected Screen nextScreen;
private float transitionDuration;
private float currentTransitionTime;
private boolean transitionRunning;
private ScreenTransition screenTransition;

public FadingGame() {
this.listeners = new Array();
}

public void create() {
this.batch = new SpriteBatch();
this.currentScreenFBO = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
this.nextScreenFBO = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
}

public void dispose() {
if (this.screen != null) {
this.screen.hide();
}

if (this.nextScreen != null) {
this.nextScreen.hide();
}

this.currentScreenFBO.dispose();
this.nextScreenFBO.dispose();
}

public void pause() {
if (this.screen != null) {
this.screen.pause();
}

if (this.nextScreen != null) {
this.nextScreen.pause();
}

}

public void resume() {
if (this.screen != null) {
this.screen.resume();
}

if (this.nextScreen != null) {
this.nextScreen.resume();
}

}

public void render() {
float delta = Gdx.graphics.getDeltaTime();
if (this.nextScreen == null) {
this.screen.render(delta);
} else if (this.transitionRunning && this.currentTransitionTime >= this.transitionDuration) {
this.screen.dispose();
this.screen = this.nextScreen;
this.screen.resume();
this.transitionRunning = false;
this.nextScreen = null;
this.notifyFinished();
this.screen.render(delta);
} else if (this.screenTransition != null) {
this.currentScreenFBO.begin();
this.screen.render(delta);
this.currentScreenFBO.end();
this.nextScreenFBO.begin();
this.nextScreen.render(delta);
this.nextScreenFBO.end();
float percent = this.currentTransitionTime / this.transitionDuration;
this.screenTransition.render(this.batch, this.currentScreenFBO.getColorBufferTexture(), this.nextScreenFBO.getColorBufferTexture(), percent);
this.currentTransitionTime += delta;
}

}

public void resize(int width, int height) {
if (this.screen != null) {
this.screen.resize(width, height);
}

if (this.nextScreen != null) {
this.nextScreen.resize(width, height);
}

this.currentScreenFBO.dispose();
this.nextScreenFBO.dispose();
this.currentScreenFBO = new FrameBuffer(Format.RGBA8888, width, height, false);
this.nextScreenFBO = new FrameBuffer(Format.RGBA8888, width, height, false);
}

public boolean setTransition(ScreenTransition screenTransition, float duration) {
if (this.transitionRunning) {
return false;
} else {
this.screenTransition = screenTransition;
this.transitionDuration = duration;
return true;
}
}

public Screen getScreen() {
return this.screen;
}

public void setScreen(Screen screen) {
screen.show();
if (this.transitionRunning) {
Gdx.app.log(FadingGame.class.getSimpleName(), "Changed Screen while transition in progress");
}

if (this.screen == null) {
this.screen = screen;
} else if (this.screenTransition == null) {
this.screen.hide();
this.screen = screen;
} else {
this.nextScreen = screen;
this.screen.pause();
this.nextScreen.pause();
this.currentTransitionTime = 0.0F;
this.transitionRunning = true;
this.notifyStarted();
}

this.screen.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}

public Screen getNextScreen() {
return this.nextScreen;
}

public void addTransitionListener(TransitionListener listener) {
this.listeners.add(listener);
}

public boolean removeTransitionListener(TransitionListener listener) {
return this.listeners.removeValue(listener, true);
}

public void clearTransitionListeners() {
this.listeners.clear();
}

private void notifyFinished() {
Iterator var1 = this.listeners.iterator();

while(var1.hasNext()) {
TransitionListener transitionListener = (TransitionListener)var1.next();
transitionListener.onTransitionFinished();
}

}

private void notifyStarted() {
Iterator var1 = this.listeners.iterator();

while(var1.hasNext()) {
TransitionListener transitionListener = (TransitionListener)var1.next();
transitionListener.onTransitionStart();
}

}
}
Loading

0 comments on commit fb34870

Please sign in to comment.