From 029190712a8f38cef760741cf53652e0ccd89172 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 3 Feb 2009 21:11:23 -0500 Subject: Started working on new Transitions The old transition implementation was old and patchy. The new one is planned to be extensible and to work properly with all transitions. Currently this is not so, but with work it hopefully will be. --- src/com/fourisland/fourpuzzle/Display.java | 137 ++++++++++++++------- .../fourisland/fourpuzzle/PuzzleApplication.java | 10 +- .../fourpuzzle/gamestate/GameOverGameState.java | 17 +-- .../fourpuzzle/gamestate/TitleScreenGameState.java | 24 +++- .../gamestate/mapview/event/SpecialEvent.java | 44 +++++++ .../transition/DoNotEraseTransition.java | 21 ++++ .../fourpuzzle/transition/InTransition.java | 18 +++ .../transition/MultidirectionalTransition.java | 16 +++ .../fourpuzzle/transition/OutTransition.java | 14 +++ .../fourpuzzle/transition/SquareTransition.java | 34 +++-- .../fourpuzzle/transition/Transition.java | 51 ++------ .../transition/TransitionCallbackThread.java | 38 ------ .../fourpuzzle/transition/TransitionDirection.java | 15 +++ .../transition/TransitionUnsupportedException.java | 14 ++- 14 files changed, 299 insertions(+), 154 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/transition/DoNotEraseTransition.java create mode 100644 src/com/fourisland/fourpuzzle/transition/InTransition.java create mode 100644 src/com/fourisland/fourpuzzle/transition/MultidirectionalTransition.java create mode 100644 src/com/fourisland/fourpuzzle/transition/OutTransition.java delete mode 100644 src/com/fourisland/fourpuzzle/transition/TransitionCallbackThread.java create mode 100644 src/com/fourisland/fourpuzzle/transition/TransitionDirection.java diff --git a/src/com/fourisland/fourpuzzle/Display.java b/src/com/fourisland/fourpuzzle/Display.java index 6fab3ea..4f1e730 100644 --- a/src/com/fourisland/fourpuzzle/Display.java +++ b/src/com/fourisland/fourpuzzle/Display.java @@ -5,12 +5,18 @@ package com.fourisland.fourpuzzle; +import com.fourisland.fourpuzzle.transition.InTransition; +import com.fourisland.fourpuzzle.transition.MultidirectionalTransition; +import com.fourisland.fourpuzzle.transition.OutTransition; import com.fourisland.fourpuzzle.transition.Transition; -import com.fourisland.fourpuzzle.transition.TransitionCallbackThread; +import com.fourisland.fourpuzzle.transition.TransitionDirection; +import com.fourisland.fourpuzzle.transition.TransitionUnsupportedException; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; +import java.awt.image.BufferedImage; import java.awt.image.VolatileImage; +import java.util.concurrent.CountDownLatch; import javax.swing.JDialog; /** @@ -23,37 +29,34 @@ public class Display { public static void render(JDialog gameFrame) { - if (enabled) - { - VolatileImage vImg = gameFrame.createVolatileImage(Game.WIDTH, Game.HEIGHT); - render(gameFrame, vImg); + VolatileImage vImg = gameFrame.createVolatileImage(Game.WIDTH, Game.HEIGHT); + render(gameFrame, vImg); - Image img = null; - do + Image img = null; + do + { + int returnCode = vImg.validate(gameFrame.getGraphicsConfiguration()); + if (returnCode == VolatileImage.IMAGE_RESTORED) { - int returnCode = vImg.validate(gameFrame.getGraphicsConfiguration()); - if (returnCode == VolatileImage.IMAGE_RESTORED) - { - render(gameFrame, vImg); - } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) - { - vImg = gameFrame.createVolatileImage(Game.WIDTH, Game.HEIGHT); - render(gameFrame, vImg); - } + render(gameFrame, vImg); + } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) + { + vImg = gameFrame.createVolatileImage(Game.WIDTH, Game.HEIGHT); + render(gameFrame, vImg); + } - img = vImg; - } while (vImg.contentsLost()); + img = vImg; + } while (vImg.contentsLost()); - gameFrame.getContentPane().getGraphics().drawImage(img, 0, 0, gameFrame.getContentPane().getWidth(), gameFrame.getContentPane().getHeight(), gameFrame); - img.flush(); - Toolkit.getDefaultToolkit().sync(); + gameFrame.getContentPane().getGraphics().drawImage(img, 0, 0, gameFrame.getContentPane().getWidth(), gameFrame.getContentPane().getHeight(), gameFrame); + img.flush(); + Toolkit.getDefaultToolkit().sync(); - if (tileAnimationFrame == 15) - { - tileAnimationFrame = 0; - } else { - tileAnimationFrame++; - } + if (tileAnimationFrame == 15) + { + tileAnimationFrame = 0; + } else { + tileAnimationFrame++; } } @@ -68,38 +71,78 @@ public class Display { if (transition != null) { - transition.render(g); + if (transition.render(g)) + { + if (startedTransition) + { + midTransition = new BufferedImage(Game.WIDTH, Game.HEIGHT, BufferedImage.TYPE_INT_ARGB); + midTransition.getGraphics().drawImage(vImg, 0, 0, null); + } else { + midTransition = null; + } + + transitionWait.countDown(); + } } Game.getGameState().render(g); g.dispose(); } - public static void transition(Transition transition, Runnable callback) - { - setTransition(transition); - - new Thread(new TransitionCallbackThread(callback)).start(); - } - + private static boolean startedTransition = false; private static Transition transition; - public static Transition getTransition() - { - return transition; - } - public static void setTransition(Transition transition) + private static CountDownLatch transitionWait; + private static boolean transitionRunning = false; + private static BufferedImage midTransition = null; + public static void transition(Transition transition) throws InterruptedException { + if (transition instanceof MultidirectionalTransition) + { + MultidirectionalTransition temp = (MultidirectionalTransition) transition; + + if (startedTransition && (temp.getDirection() != TransitionDirection.In)) + { + throw new TransitionUnsupportedException(transition.getClass().getName(), TransitionDirection.In); + } else if (!startedTransition && (temp.getDirection() != TransitionDirection.Out)) + { + throw new TransitionUnsupportedException(transition.getClass().getName(), TransitionDirection.Out); + } + + if (temp.getDirection() == TransitionDirection.In) + { + temp.setPreTransition(midTransition); + } + } else { + if (startedTransition && !(transition instanceof InTransition)) + { + throw new TransitionUnsupportedException(transition.getClass().getName(), TransitionDirection.In); + } else if (!startedTransition && !(transition instanceof OutTransition)) + { + throw new TransitionUnsupportedException(transition.getClass().getName(), TransitionDirection.Out); + } + + if (transition instanceof InTransition) + { + ((InTransition) transition).setPreTransition(midTransition); + } + } + Display.transition = transition; + startedTransition = !startedTransition; + transitionRunning = true; + + transitionWait = new CountDownLatch(1); + transitionWait.await(); + + if (!startedTransition) + { + transitionRunning = false; + } } - private static boolean enabled = true; - public static boolean isEnabled() - { - return enabled; - } - public static void setEnabled(boolean aEnabled) + public static boolean isTransitionRunning() { - enabled = aEnabled; + return transitionRunning; } } diff --git a/src/com/fourisland/fourpuzzle/PuzzleApplication.java b/src/com/fourisland/fourpuzzle/PuzzleApplication.java index 1c1dde2..9225c90 100644 --- a/src/com/fourisland/fourpuzzle/PuzzleApplication.java +++ b/src/com/fourisland/fourpuzzle/PuzzleApplication.java @@ -117,12 +117,16 @@ public class PuzzleApplication extends Application { if ((iTickCount > iTickTrigger) && (!gameSleep)) { - if (Game.getKey() != null) + if (!Display.isTransitionRunning()) { - Game.getGameState().processInput(); + if (Game.getKey() != null) + { + Game.getGameState().processInput(); + } + + Game.getGameState().doGameCycle(); } - Game.getGameState().doGameCycle(); Display.render(gameFrame); if (!debugSpeed) diff --git a/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java b/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java index 48706ad..d20691f 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java @@ -10,11 +10,10 @@ import com.fourisland.fourpuzzle.Display; import com.fourisland.fourpuzzle.Game; import com.fourisland.fourpuzzle.SaveFile; import com.fourisland.fourpuzzle.transition.SquareTransition; +import com.fourisland.fourpuzzle.transition.TransitionDirection; import com.fourisland.fourpuzzle.util.ObjectLoader; import java.awt.Graphics2D; import java.awt.event.KeyEvent; -import java.util.logging.Level; -import java.util.logging.Logger; /** * @@ -37,16 +36,18 @@ public class GameOverGameState implements GameState { if ((Game.getKey().getKeyCode() == KeyEvent.VK_ENTER) || (Game.getKey().getKeyCode() == KeyEvent.VK_SPACE)) { Game.setSaveFile(new SaveFile()); - //Display.transition(SquareTransition.class, this, new TitleScreenGameState()); - Display.transition(new SquareTransition(true), new Runnable() { + + new Thread(new Runnable() { public void run() { try { - Game.setGameState(new TitleScreenGameState()); - } catch (Exception ex) { - Logger.getLogger(GameOverGameState.class.getName()).log(Level.SEVERE, null, ex); + Display.transition(new SquareTransition(TransitionDirection.Out)); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); } + + Game.setGameState(new TitleScreenGameState()); } - }); + }).start(); } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java index 683b361..0b032eb 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java @@ -7,6 +7,8 @@ package com.fourisland.fourpuzzle.gamestate; import com.fourisland.fourpuzzle.*; import com.fourisland.fourpuzzle.gamestate.mapview.MapViewGameState; +import com.fourisland.fourpuzzle.transition.SquareTransition; +import com.fourisland.fourpuzzle.transition.TransitionDirection; import com.fourisland.fourpuzzle.util.ObjectLoader; import java.awt.Graphics2D; import java.awt.event.KeyEvent; @@ -32,10 +34,24 @@ public class TitleScreenGameState implements GameState { if (Game.getKey().getKeyCode() == KeyEvent.VK_ENTER) { Game.setSaveFile(new SaveFile()); - Game.setGameState(new MapViewGameState("TestMap", 1, 2)); - //Game.setGameState(new SquareTransition(this, new MapViewGameState("TestMap", 0, 0))); - //Game.setGameState(new TransitionGameState(this, this)); - //Game.setGameState(new GameOverGameState()); + + new Thread(new Runnable() { + public void run() { + try { + Display.transition(new SquareTransition(TransitionDirection.Out)); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + + Game.setGameState(new MapViewGameState("TestMap", 1, 2)); + + try { + Display.transition(new SquareTransition(TransitionDirection.In)); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + }).start(); } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java index faa7a48..cf31bf1 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java @@ -13,6 +13,8 @@ import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.AutomaticViewpoint; import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.FixedViewpoint; import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.MovingViewpoint; import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.Viewpoint; +import com.fourisland.fourpuzzle.transition.InTransition; +import com.fourisland.fourpuzzle.transition.OutTransition; import java.util.concurrent.CountDownLatch; /** @@ -149,9 +151,33 @@ public class SpecialEvent { throw new UnsupportedOperationException("Not yet implemented"); } + private boolean startedTransition = false; + + /** + * Displays a transition from the current map to emptiness + * + * If this method is executed before Teleport(), Teleport() will not use + * the database-default out transition and instead immeditatly jump to the + * new map. It will also not use the database-default in transition which + * requires you to also execute EndTransition(). + * + * @param trans The transition to use + * @throws InterruptedException + */ + public void StartTransition(OutTransition trans) throws InterruptedException + { + startedTransition = true; + + Display.transition(trans); + } + /** * Moves the player to a different map * + * If StartTransition() is executed prior to this method, then this will + * not preform the database-default transitions, which requires that + * EndTransition() is executed after this method. + * * @param map The name of the map to move to * @param x The X position on the map to move to * @param y The Y position on the map to move to @@ -161,6 +187,24 @@ public class SpecialEvent { throw new UnsupportedOperationException("Not yet implemented"); } + /** + * Displays a transition from the emptiness to the new map + * + * This method is only required if you called StartTransition() before + * Teleport(), in which case it will display the transition. Otherwise, + * this action will do nothing. + * + * @param trans + * @throws InterruptedException + */ + public void EndTransition(InTransition trans) throws InterruptedException + { + if (startedTransition) + { + Display.transition(trans); + } + } + /** * Waits for a specified interval * diff --git a/src/com/fourisland/fourpuzzle/transition/DoNotEraseTransition.java b/src/com/fourisland/fourpuzzle/transition/DoNotEraseTransition.java new file mode 100644 index 0000000..63c3ea3 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/transition/DoNotEraseTransition.java @@ -0,0 +1,21 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.transition; + +import java.awt.Graphics2D; + +/** + * + * @author hatkirby + */ +public class DoNotEraseTransition implements OutTransition { + + public boolean render(Graphics2D g) + { + return true; + } + +} diff --git a/src/com/fourisland/fourpuzzle/transition/InTransition.java b/src/com/fourisland/fourpuzzle/transition/InTransition.java new file mode 100644 index 0000000..a326f52 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/transition/InTransition.java @@ -0,0 +1,18 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.transition; + +import java.awt.image.BufferedImage; + +/** + * + * @author hatkirby + */ +public interface InTransition extends Transition { + + public void setPreTransition(BufferedImage preTransition); + +} diff --git a/src/com/fourisland/fourpuzzle/transition/MultidirectionalTransition.java b/src/com/fourisland/fourpuzzle/transition/MultidirectionalTransition.java new file mode 100644 index 0000000..7177179 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/transition/MultidirectionalTransition.java @@ -0,0 +1,16 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.transition; + +/** + * + * @author hatkirby + */ +public interface MultidirectionalTransition extends OutTransition, InTransition { + + public TransitionDirection getDirection(); + +} diff --git a/src/com/fourisland/fourpuzzle/transition/OutTransition.java b/src/com/fourisland/fourpuzzle/transition/OutTransition.java new file mode 100644 index 0000000..37bf5b4 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/transition/OutTransition.java @@ -0,0 +1,14 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.transition; + +/** + * + * @author hatkirby + */ +public interface OutTransition extends Transition { + +} diff --git a/src/com/fourisland/fourpuzzle/transition/SquareTransition.java b/src/com/fourisland/fourpuzzle/transition/SquareTransition.java index d60282d..1d8748a 100644 --- a/src/com/fourisland/fourpuzzle/transition/SquareTransition.java +++ b/src/com/fourisland/fourpuzzle/transition/SquareTransition.java @@ -7,19 +7,21 @@ package com.fourisland.fourpuzzle.transition; import java.awt.Color; import java.awt.Graphics2D; +import java.awt.image.BufferedImage; /** * * @author hatkirby */ -public class SquareTransition extends Transition { +public class SquareTransition implements MultidirectionalTransition { private int tick; - public SquareTransition(boolean from) + private TransitionDirection direction; + public SquareTransition(TransitionDirection direction) { - setDirection(from); + this.direction = direction; - if (from) + if (direction == TransitionDirection.Out) { tick = 160; } else { @@ -27,24 +29,36 @@ public class SquareTransition extends Transition { } } - public void render(Graphics2D g) + public boolean render(Graphics2D g) { - if (((!getDirection()) && (tick == 0)) || ((getDirection()) && (tick == 160))) + if (((direction == TransitionDirection.Out) && (tick == 0)) || ((direction == TransitionDirection.In) && (tick == 160))) { - setRunning(false); - return; + return true; } - if (getDirection()) + if (direction == TransitionDirection.In) { tick+=8; } else { - tick-=8; + tick-=8; } g.setBackground(Color.BLACK); g.fillRect(0, 0, 320, 240); g.setClip(160-tick, 140-tick, tick*2, tick*2-40); + + return false; + } + + public TransitionDirection getDirection() + { + return direction; + } + + private BufferedImage preTransition; + public void setPreTransition(BufferedImage preTransition) + { + this.preTransition = preTransition; } } diff --git a/src/com/fourisland/fourpuzzle/transition/Transition.java b/src/com/fourisland/fourpuzzle/transition/Transition.java index e25fbc9..5b4dbe8 100644 --- a/src/com/fourisland/fourpuzzle/transition/Transition.java +++ b/src/com/fourisland/fourpuzzle/transition/Transition.java @@ -11,47 +11,14 @@ import java.awt.Graphics2D; * * @author hatkirby */ -public abstract class Transition { - - private boolean way; - protected void setDirection(boolean from) - { - if ((from) && !(isFromSupported())) - { - throw new TransitionUnsupportedException(this.getClass().getSimpleName(), "From"); - } else if ((!from) && !(isToSupported())) - { - throw new TransitionUnsupportedException(this.getClass().getSimpleName(), "To"); - } else { - way = from; - } - } - - public boolean getDirection() - { - return way; - } +public interface Transition { - public boolean isFromSupported() - { - return true; - } - - public boolean isToSupported() - { - return true; - } - - public abstract void render(Graphics2D g); - - private boolean running = true; - public boolean isRunning() - { - return running; - } - public void setRunning(boolean running) - { - this.running = running; - } + /** + * Render the transition to the display + * + * @param g The graphics device to render the transition to + * @return If the transition has completed, true. Otherwise false. + */ + public boolean render(Graphics2D g); -} +} \ No newline at end of file diff --git a/src/com/fourisland/fourpuzzle/transition/TransitionCallbackThread.java b/src/com/fourisland/fourpuzzle/transition/TransitionCallbackThread.java deleted file mode 100644 index 50eacc2..0000000 --- a/src/com/fourisland/fourpuzzle/transition/TransitionCallbackThread.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - -package com.fourisland.fourpuzzle.transition; - -import com.fourisland.fourpuzzle.Display; - -/** - * - * @author hatkirby - */ -public class TransitionCallbackThread implements Runnable { - - private Runnable callback; - public TransitionCallbackThread(Runnable callback) - { - this.callback = callback; - } - - public void run() - { - while (Display.getTransition().isRunning()) - { - try { - Thread.sleep(300); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - } - - //Display.setEnabled(false); - - callback.run(); - } - -} diff --git a/src/com/fourisland/fourpuzzle/transition/TransitionDirection.java b/src/com/fourisland/fourpuzzle/transition/TransitionDirection.java new file mode 100644 index 0000000..305bb5c --- /dev/null +++ b/src/com/fourisland/fourpuzzle/transition/TransitionDirection.java @@ -0,0 +1,15 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.transition; + +/** + * + * @author hatkirby + */ +public enum TransitionDirection { + In, + Out +} diff --git a/src/com/fourisland/fourpuzzle/transition/TransitionUnsupportedException.java b/src/com/fourisland/fourpuzzle/transition/TransitionUnsupportedException.java index 40f3db8..dfea7aa 100644 --- a/src/com/fourisland/fourpuzzle/transition/TransitionUnsupportedException.java +++ b/src/com/fourisland/fourpuzzle/transition/TransitionUnsupportedException.java @@ -11,9 +11,19 @@ package com.fourisland.fourpuzzle.transition; */ public class TransitionUnsupportedException extends RuntimeException { - public TransitionUnsupportedException(String className, String direction) + private String className; + private TransitionDirection direction; + + public TransitionUnsupportedException(String className, TransitionDirection direction) + { + this.className = className; + this.direction = direction; + } + + @Override + public String getMessage() { - super("Transition \"" + className + "\" does not support the " + direction + " direction"); + return "Transition \"" + className + "\" does not support the " + direction.toString() + " direction"; } } -- cgit 1.4.1