From bbf294dbf6b552751e5d9f3fb66188bd1bee724b Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Thu, 12 Feb 2009 16:36:07 -0500 Subject: Engine: Wrote Message System MessageWindow now has a static method run by SpecialEvent that triggers the message box. This method blocks until the message is complete and renders via Display's new feature. The message box also now features the "next" arrow and the letters gradually appear. Display has also been re-worked to have a list of a new interface called Renderable, which is any object that can be rendered. Such objects (such as MessageWindow) can register to Display, which will render them onto the game frame after the GameState has been rendered. Closes #5. --- src/com/fourisland/fourpuzzle/Display.java | 52 +++++++++--- .../fourisland/fourpuzzle/gamestate/GameState.java | 18 +--- .../fourpuzzle/gamestate/TitleScreenGameState.java | 9 +- .../gamestate/mapview/MapViewGameState.java | 12 --- .../gamestate/mapview/event/SpecialEvent.java | 6 +- src/com/fourisland/fourpuzzle/util/Renderable.java | 18 ++++ .../fourisland/fourpuzzle/window/ChoiceWindow.java | 45 +++++++++- .../fourpuzzle/window/MessageWindow.java | 99 ++++++++++++++++++++-- 8 files changed, 203 insertions(+), 56 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/util/Renderable.java (limited to 'src/com') diff --git a/src/com/fourisland/fourpuzzle/Display.java b/src/com/fourisland/fourpuzzle/Display.java index 85acaf4..ec835ab 100755 --- a/src/com/fourisland/fourpuzzle/Display.java +++ b/src/com/fourisland/fourpuzzle/Display.java @@ -11,6 +11,7 @@ import com.fourisland.fourpuzzle.transition.OutTransition; import com.fourisland.fourpuzzle.transition.Transition; import com.fourisland.fourpuzzle.transition.TransitionDirection; import com.fourisland.fourpuzzle.transition.TransitionUnsupportedException; +import com.fourisland.fourpuzzle.util.Renderable; import java.awt.Font; import java.awt.FontFormatException; import java.awt.Graphics2D; @@ -20,6 +21,8 @@ import java.awt.image.BufferedImage; import java.awt.image.VolatileImage; import java.io.IOException; import java.io.InputStream; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.logging.Level; import java.util.logging.Logger; @@ -33,6 +36,8 @@ import org.jdesktop.application.ResourceMap; public class Display { public static int tileAnimationFrame = 0; + + private static List renderables = new CopyOnWriteArrayList(); public static void render(JDialog gameFrame) { @@ -95,18 +100,37 @@ public class Display { g.drawImage(midTransition, 0, 0, null); } } else { - Game.getGameState().render(g); + render(g); } g.dispose(); } + private static void render(Graphics2D g) + { + Game.getGameState().render(g); + + for (Renderable r : renderables) + { + r.render(g); + } + } + + public static void registerRenderable(Renderable r) + { + renderables.add(r); + } + + public static void unregisterRenderable(Renderable r) + { + renderables.remove(r); + } + private static boolean startedTransition = false; private static Transition transition; private static CountDownLatch transitionWait; private static boolean transitionRunning = false; private static BufferedImage midTransition = null; - private static BufferedImage postTransition = null; public static void transition(Transition transition) throws InterruptedException { if (transition instanceof MultidirectionalTransition) @@ -125,13 +149,13 @@ public class Display { { temp.setPreTransition(midTransition); - postTransition = Display.createCanvas(Game.WIDTH, Game.HEIGHT); - Game.getGameState().render(postTransition.createGraphics()); - temp.setPostTransition(postTransition); + BufferedImage bImg = Display.createCanvas(Game.WIDTH, Game.HEIGHT); + render(bImg.createGraphics()); + temp.setPostTransition(bImg); } else { - BufferedImage preTransition = Display.createCanvas(Game.WIDTH, Game.HEIGHT); - Game.getGameState().render(preTransition.createGraphics()); - temp.setPreTransition(preTransition); + BufferedImage bImg = Display.createCanvas(Game.WIDTH, Game.HEIGHT); + render(bImg.createGraphics()); + temp.setPreTransition(bImg); } } else { if (startedTransition && !(transition instanceof InTransition)) @@ -146,13 +170,13 @@ public class Display { { transition.setPreTransition(midTransition); - postTransition = Display.createCanvas(Game.WIDTH, Game.HEIGHT); - Game.getGameState().render(postTransition.createGraphics()); - ((InTransition) transition).setPostTransition(postTransition); + BufferedImage bImg = Display.createCanvas(Game.WIDTH, Game.HEIGHT); + render(bImg.createGraphics()); + ((InTransition) transition).setPostTransition(bImg); } else { - BufferedImage preTransition = Display.createCanvas(Game.WIDTH, Game.HEIGHT); - Game.getGameState().render(preTransition.createGraphics()); - transition.setPreTransition(preTransition); + BufferedImage bImg = Display.createCanvas(Game.WIDTH, Game.HEIGHT); + render(bImg.createGraphics()); + transition.setPreTransition(bImg); } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/GameState.java b/src/com/fourisland/fourpuzzle/gamestate/GameState.java index de4d7de..e7bd783 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/GameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/GameState.java @@ -5,30 +5,18 @@ package com.fourisland.fourpuzzle.gamestate; -import java.awt.Graphics2D; +import com.fourisland.fourpuzzle.util.Renderable; /** * * @author hatkirby */ -public interface GameState { +public interface GameState extends Renderable { public void initalize(); public void deinitalize(); public void processInput(); public void doGameCycle(); - public void render(Graphics2D g); -} - -/* - TitleScreen - MapView - Battle - GameOver - Menu - LoadFile - SaveFile - Transition -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java index 272c4f0..d5b4f90 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java @@ -27,20 +27,20 @@ import java.util.Arrays; public class TitleScreenGameState implements GameState { ChoiceWindow choices; - int wx, wy; public void initalize() { Audio.playMusic(Database.getMusic(Music.Title)); - choices = new ChoiceWindow(Arrays.asList(Database.getVocab(Vocabulary.NewGame), Database.getVocab(Vocabulary.LoadGame), Database.getVocab(Vocabulary.EndGame)), true); - wx = (Game.WIDTH/5)-(choices.getWidth()/2); - wy = (Game.HEIGHT/4*3)-(choices.getHeight()/2); + choices = new ChoiceWindow(Arrays.asList(Database.getVocab(Vocabulary.NewGame), Database.getVocab(Vocabulary.LoadGame), Database.getVocab(Vocabulary.EndGame)), true, ChoiceWindow.ChoiceWindowLocation.BottomLeft); + Display.registerRenderable(choices); } public void deinitalize() { Audio.stopMusic(); + + Display.unregisterRenderable(choices); } PauseTimer pt = new PauseTimer(0); @@ -112,7 +112,6 @@ public class TitleScreenGameState implements GameState { public void render(Graphics2D g) { g.drawImage(ObjectLoader.getImage("Picture", "Title"), 0, 0, null); - choices.render(g, wx, wy); } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index 3ed23c3..a20062d 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java @@ -23,7 +23,6 @@ import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventTh import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.AutomaticViewpoint; import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.Viewpoint; import com.fourisland.fourpuzzle.util.Functions; -import com.fourisland.fourpuzzle.window.MessageWindow; import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; @@ -229,11 +228,6 @@ public class MapViewGameState implements GameState { g.drawImage(eventLayer, 0, 0, Game.WIDTH, Game.HEIGHT, x, y, x+Game.WIDTH, y+Game.HEIGHT, null); g.drawImage(currentMap.renderUpper(), 0, 0, Game.WIDTH, Game.HEIGHT, x, y, x+Game.WIDTH, y+Game.HEIGHT, null); - - if (mw != null) - { - mw.render(g); - } } public void setCurrentMap(String mapName) @@ -256,11 +250,5 @@ public class MapViewGameState implements GameState { { currentViewpoint = viewpoint; } - - volatile MessageWindow mw = null; - public void displayMessage(String message) - { - mw = new MessageWindow(message); - } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java index 7ca08ff..2b54dc9 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java @@ -17,6 +17,7 @@ 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 com.fourisland.fourpuzzle.window.MessageWindow; import java.util.concurrent.CountDownLatch; /** @@ -50,10 +51,11 @@ public class SpecialEvent { * been read. * * @param message The message to display + * @throws InterruptedException */ - public void DisplayMessage(String message) + public void DisplayMessage(String message) throws InterruptedException { - mapView.displayMessage(message); + MessageWindow.displayMessage(message); } /** diff --git a/src/com/fourisland/fourpuzzle/util/Renderable.java b/src/com/fourisland/fourpuzzle/util/Renderable.java new file mode 100644 index 0000000..a6aeb1a --- /dev/null +++ b/src/com/fourisland/fourpuzzle/util/Renderable.java @@ -0,0 +1,18 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.util; + +import java.awt.Graphics2D; + +/** + * + * @author hatkirby + */ +public interface Renderable { + + public void render(Graphics2D g); + +} diff --git a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java index ca84383..8399422 100755 --- a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java +++ b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java @@ -7,8 +7,10 @@ package com.fourisland.fourpuzzle.window; import com.fourisland.fourpuzzle.Audio; import com.fourisland.fourpuzzle.Display; +import com.fourisland.fourpuzzle.Game; import com.fourisland.fourpuzzle.database.Database; import com.fourisland.fourpuzzle.database.Sound; +import com.fourisland.fourpuzzle.util.Renderable; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.TexturePaint; @@ -19,7 +21,7 @@ import java.util.List; * * @author hatkirby */ -public class ChoiceWindow { +public class ChoiceWindow implements Renderable { private static final int SPACER = 4; @@ -29,7 +31,9 @@ public class ChoiceWindow { private int width; private int height; BufferedImage cacheBase; - public ChoiceWindow(List choices, boolean center) + int x; + int y; + public ChoiceWindow(List choices, boolean center, ChoiceWindowLocation cwl) { this.choices = choices; numChoices = choices.size(); @@ -52,9 +56,12 @@ public class ChoiceWindow { width += SPACER*2; cacheBase = Window.Default.getImage(width, height); + + x = cwl.getX(width); + y = cwl.getY(height); } - public void render(Graphics2D g2, int x, int y) + public void render(Graphics2D g2) { Display.setFont(g2); @@ -119,5 +126,37 @@ public class ChoiceWindow { { return choices.get(selected); } + + public static enum ChoiceWindowLocation + { + BottomLeft + { + public int getX(int width) + { + return (Game.WIDTH/5)-(width/2); + } + }, + BottomCenter + { + public int getX(int width) + { + return (Game.WIDTH/2)-(width/2); + } + }, + BottomRight + { + public int getX(int width) + { + return (Game.WIDTH/5*4)-(width/2); + } + }; + + public abstract int getX(int width); + + public int getY(int height) + { + return (Game.HEIGHT/4*3)-(height/2); + } + } } \ No newline at end of file diff --git a/src/com/fourisland/fourpuzzle/window/MessageWindow.java b/src/com/fourisland/fourpuzzle/window/MessageWindow.java index deab252..3272fcc 100644 --- a/src/com/fourisland/fourpuzzle/window/MessageWindow.java +++ b/src/com/fourisland/fourpuzzle/window/MessageWindow.java @@ -7,26 +7,36 @@ package com.fourisland.fourpuzzle.window; import com.fourisland.fourpuzzle.Display; import com.fourisland.fourpuzzle.Game; +import com.fourisland.fourpuzzle.PuzzleApplication; +import com.fourisland.fourpuzzle.util.Interval; +import com.fourisland.fourpuzzle.util.Renderable; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.TexturePaint; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CountDownLatch; /** * * @author hatkirby */ -public class MessageWindow { +public class MessageWindow implements Renderable { private static final int SPACER = 4; private static final int HEIGHT = 4*(Display.createCanvas(1, 1).createGraphics().getFontMetrics().getHeight()+SPACER); - private List messages; + private volatile List messages; int width; BufferedImage cacheBase; - public MessageWindow(String message) + int num = 0; + int upTo = 0; + boolean bounceArrow = false; + Interval in = Interval.createTickInterval(4); + private MessageWindow(String message) { width = Game.WIDTH - Window.Default.getFullWidth(0); messages = new ArrayList(); @@ -36,6 +46,36 @@ public class MessageWindow { cacheBase = Window.Default.getImage(width, HEIGHT); } + public static void displayMessage(String message) throws InterruptedException + { + final MessageWindow mw = new MessageWindow(message); + final CountDownLatch cdl = new CountDownLatch(1); + + Display.registerRenderable(mw); + + KeyAdapter ka = new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if ((e.getKeyCode() == KeyEvent.VK_ENTER) || (e.getKeyCode() == KeyEvent.VK_SPACE)) + { + if (mw.pushEnter()) + { + cdl.countDown(); + } + } + + Game.setKey(null); + } + }; + + PuzzleApplication.gameFrame.addKeyListener(ka); + + cdl.await(); + + PuzzleApplication.gameFrame.removeKeyListener(ka); + Display.unregisterRenderable(mw); + } + private void initalizeMessages(String message, Graphics2D g) { Display.setFont(g); @@ -68,12 +108,24 @@ public class MessageWindow { len = 0; } + + setLength(); + } + + private void setLength() + { + num = 0; + + for (int i=0;i= num) + { + int msgs = messages.size(); + for (int i=0;i 0) + { + upTo = 0; + setLength(); + } else { + return true; + } } + + return false; } public static enum MessageWindowLocation -- cgit 1.4.1