From 0540ce566ddce97a60c3279c51d5c5ddf1645321 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Mon, 16 Feb 2009 13:03:07 -0500 Subject: Engine: Abstracted game state transitions Previously, every game state transition was manually executed with a thread, some exception handling and Display. Now, Display has a method that takes care of it without leaving all of the bulk in random classes. --- src/com/fourisland/fourpuzzle/Display.java | 33 ++++++++++++++++++++++ .../fourpuzzle/gamestate/GameOverGameState.java | 18 +----------- .../fourpuzzle/gamestate/TitleScreenGameState.java | 18 +----------- .../gamestate/mapview/event/SpecialEvent.java | 23 ++++----------- 4 files changed, 41 insertions(+), 51 deletions(-) (limited to 'src/com/fourisland') diff --git a/src/com/fourisland/fourpuzzle/Display.java b/src/com/fourisland/fourpuzzle/Display.java index b29ab01..cf1cb2d 100755 --- a/src/com/fourisland/fourpuzzle/Display.java +++ b/src/com/fourisland/fourpuzzle/Display.java @@ -5,6 +5,7 @@ package com.fourisland.fourpuzzle; +import com.fourisland.fourpuzzle.gamestate.GameState; import com.fourisland.fourpuzzle.transition.InTransition; import com.fourisland.fourpuzzle.transition.MultidirectionalTransition; import com.fourisland.fourpuzzle.transition.OutTransition; @@ -28,6 +29,8 @@ import java.io.InputStream; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.Logger; import org.jdesktop.application.ResourceMap; @@ -204,6 +207,36 @@ public class Display { } } + private static Executor transitioner = Executors.newSingleThreadExecutor(); + public static void transition(final Transition out, final GameState gameState, final Transition in, boolean thread) + { + Runnable transitionCall = new Runnable() { + public void run() + { + try { + Display.transition(out); + Game.setGameState(gameState); + Display.transition(in); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } catch (RuntimeException ex) + { + PuzzleApplication.INSTANCE.reportError(ex); + } catch (Error ex) + { + PuzzleApplication.INSTANCE.reportError(ex); + } + } + }; + + if (thread) + { + transitioner.execute(transitionCall); + } else { + transitionCall.run(); + } + } + public static boolean isTransitionRunning() { return transitionRunning; diff --git a/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java b/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java index e87f80a..670d900 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java @@ -38,23 +38,7 @@ public class GameOverGameState implements GameState { { Game.setSaveFile(new SaveFile()); - new Thread(new Runnable() { - public void run() { - try { - Display.transition(Database.getTransition(Transitions.GameOverToTitle)); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - - Game.setGameState(new TitleScreenGameState()); - - try { - Display.transition(Database.getTransition(Transitions.TitleEnter)); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - }).start(); + Display.transition(Database.getTransition(Transitions.GameOverToTitle), new TitleScreenGameState(), Database.getTransition(Transitions.TitleEnter), true); } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java index d40b3f9..a72a22a 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java @@ -56,23 +56,7 @@ public class TitleScreenGameState implements GameState { { Game.setSaveFile(new SaveFile()); - new Thread(new Runnable() { - public void run() { - try { - Display.transition(Database.getTransition(Transitions.TitleExit)); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - - Game.setGameState(new MapViewGameState("TestMap", 1, 2)); - - try { - Display.transition(Database.getTransition(Transitions.TitleToMap)); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - }).start(); + Display.transition(Database.getTransition(Transitions.TitleExit), new MapViewGameState("TestMap", 1, 2), Database.getTransition(Transitions.TitleToMap), true); } else if (choices.getSelected().equals(Database.getVocab(Vocabulary.LoadGame))) { // Do nothing, yet diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java index 9336616..ebb0e27 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java @@ -155,12 +155,9 @@ public class SpecialEvent { */ public void GameOver() throws InterruptedException { - Display.transition(Database.getTransition(Transitions.MapToGameOver)); - Audio.stopMusic(); - Game.setGameState(new GameOverGameState()); - - Display.transition(Database.getTransition(Transitions.GameOverEnter)); + + Display.transition(Database.getTransition(Transitions.MapToGameOver), new GameOverGameState(), Database.getTransition(Transitions.GameOverEnter), false); } /** @@ -170,12 +167,9 @@ public class SpecialEvent { */ public void TitleScreen() throws InterruptedException { - Display.transition(Database.getTransition(Transitions.MapToTitle)); - Audio.stopMusic(); - Game.setGameState(new TitleScreenGameState()); - Display.transition(Database.getTransition(Transitions.TitleEnter)); + Display.transition(Database.getTransition(Transitions.MapToTitle), new TitleScreenGameState(), Database.getTransition(Transitions.TitleEnter), false); } private boolean startedTransition = false; @@ -217,14 +211,9 @@ public class SpecialEvent { { if (!startedTransition) { - Display.transition(Database.getTransition(Transitions.MapExit)); - } - - Game.setGameState(new MapViewGameState(map, x, y)); - - if (!startedTransition) - { - Display.transition(Database.getTransition(Transitions.MapEnter)); + Display.transition(Database.getTransition(Transitions.MapExit), new MapViewGameState(map, x, y), Database.getTransition(Transitions.MapEnter), false); + } else { + Game.setGameState(new MapViewGameState(map, x, y)); } } -- cgit 1.4.1