Permalink
Browse files

Lesson 16: screen fadeout/fadein, game restart on victory

  • Loading branch information...
vladimirslav committed Oct 17, 2016
1 parent b1a2c49 commit 1a2fb0896293a80e1b12ae4fc60ee73167cdcabf
@@ -10,7 +10,7 @@
*/
public class WarningEffect extends Effect {
private static final float WARNING_TIME = 2.0f;
private static final float WARNING_TIME = 0.5f;
private int fieldX;
private int fieldY;
private Resources resources;
@@ -22,6 +22,11 @@
private static final float BONUS_SPAWN_INTERVAL = 2.0f; // spawn bonus every 2 seconds
private static final int MAX_BONUSES_ON_FIELD = 3;
public interface GameEventListener
{
void OnGameEnd(boolean playerWon);
}
Player player;
Enemy enemy;
@@ -32,7 +37,9 @@
float gameTime;
float lastBonusSpawnTime;
public GameLogic(DodgingHero _game) {
GameEventListener eventListener;
public GameLogic(DodgingHero _game, GameEventListener _listener) {
eventListener = _listener;
game = _game;
player = new Player(
MathUtils.random(MAX_BASE_X),
@@ -129,6 +136,7 @@ else if (currentBonus.getBonusType() == Bonus.BONUS_TYPE_ATTACK)
if (enemy.getLives() <= 0)
{
player.markVictorious();
eventListener.OnGameEnd(true);
}
}
@@ -7,7 +7,9 @@
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.coldwild.dodginghero.DodgingHero;
@@ -24,7 +26,8 @@
/**
* Created by comrad_gremlin on 9/6/2016.
*/
public class GameScreen extends DefaultScreen implements InputProcessor {
public class GameScreen extends DefaultScreen
implements InputProcessor, GameLogic.GameEventListener {
SpriteBatch batch;
// 8 height
@@ -43,6 +46,9 @@
private Player player;
public static final float GAME_END_FADEOUT = 0.5f;
public static final float GAME_START_FADEIN = 0.25f;
public GameScreen(DodgingHero _game) {
super(_game);
batch = new SpriteBatch();
@@ -56,10 +62,29 @@ public GameScreen(DodgingHero _game) {
GameLogic.MAX_BASE_Y,
gameStage.getWidth());
logic = new GameLogic(game);
logic = new GameLogic(game, this);
player = logic.getPlayer();
Gdx.input.setInputProcessor(this);
gameStage.addAction(
new Action() {
float time = 0;
@Override
public boolean act(float delta) {
time += delta;
float t = time / GAME_START_FADEIN;
t *= t;
if (t > 1.0f)
{
t = 1.0f;
}
batch.setColor(1, 1, 1, t);
return time >= GAME_START_FADEIN;
}
}
);
}
public void update(float delta)
@@ -255,4 +280,33 @@ public boolean mouseMoved(int screenX, int screenY) {
public boolean scrolled(int amount) {
return false;
}
@Override
public void OnGameEnd(boolean playerWon) {
gameStage.addAction(
Actions.sequence(
new Action() {
float time = 0;
@Override
public boolean act(float delta) {
time += delta;
float t = time / GAME_END_FADEOUT; // 0 .. 1
t *= t;
batch.setColor(1, 1, 1, 1 - t);
return time >= GAME_END_FADEOUT;
}
},
new Action()
{
@Override
public boolean act(float delta) {
dispose();
game.setScreen(new GameScreen(game));
return true;
}
}
)
);
}
}

0 comments on commit 1a2fb08

Please sign in to comment.