- Revision
- 610
- Author
- sirenian
- Date
- 2006-12-04 11:13:30 -0600 (Mon, 04 Dec 2006)
Log Message
[EK] Removed the duplication from 6 different ways of moving glyphs.
Modified Paths
- trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/engine/GameBehaviour.java
- trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/engine/ThreadedEngineQueueBehaviour.java
- trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/gui/HellboundFrameBehaviour.java
- trunk/examples/hellbound/src/java/com/sirenian/hellbound/domain/game/GameRequestListener.java
- trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/Game.java
- trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/ThreadedEngineQueue.java
- trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/ActionFactory.java
- trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/HellboundFrame.java
Added Paths
Diff
Modified: trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/engine/GameBehaviour.java (609 => 610)
--- trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/engine/GameBehaviour.java 2006-12-04 16:25:01 UTC (rev 609) +++ trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/engine/GameBehaviour.java 2006-12-04 17:13:30 UTC (rev 610) @@ -8,6 +8,7 @@ import com.sirenian.hellbound.domain.game.GameListener; import com.sirenian.hellbound.domain.game.GameState; import com.sirenian.hellbound.domain.glyph.GlyphListener; +import com.sirenian.hellbound.domain.glyph.GlyphMovement; import com.sirenian.hellbound.domain.glyph.GlyphType; import com.sirenian.hellbound.domain.glyph.Heartbeat; import com.sirenian.hellbound.domain.glyph.Junk; @@ -81,7 +82,12 @@ glyphListener.stubs("reportGlyphMovement").never(); heartbeat.beat(); - game.requestDropGlyph(); + game.requestGlyphMovement(GlyphMovement.DOWN); + game.requestGlyphMovement(GlyphMovement.LEFT); + game.requestGlyphMovement(GlyphMovement.RIGHT); + game.requestGlyphMovement(GlyphMovement.DROP); + game.requestGlyphMovement(GlyphMovement.ROTATE_LEFT); + game.requestGlyphMovement(GlyphMovement.ROTATE_RIGHT); verifyMocks(); } @@ -92,21 +98,21 @@ Segments latestSegments = GlyphType.T.getSegments(0).movedRight(3); game.requestStartGame(); - game.requestRotateGlyphLeft(); + game.requestGlyphMovement(GlyphMovement.ROTATE_LEFT); ensureThat(factory.glyph.getSegments(), eq(GlyphType.T.getSegments(1).movedRight(3))); - game.requestRotateGlyphRight(); + game.requestGlyphMovement(GlyphMovement.ROTATE_RIGHT); ensureThat(factory.glyph.getSegments(), eq(GlyphType.T.getSegments(0).movedRight(3))); - game.requestMoveGlyphLeft(); + game.requestGlyphMovement(GlyphMovement.LEFT); ensureThat(factory.glyph.getSegments(), eq(latestSegments.movedLeft())); latestSegments = latestSegments.movedLeft(); - game.requestMoveGlyphRight(); + game.requestGlyphMovement(GlyphMovement.RIGHT); ensureThat(factory.glyph.getSegments(), eq(latestSegments.movedRight())); latestSegments = latestSegments.movedRight(); - - game.requestMoveGlyphDown(); + + game.requestGlyphMovement(GlyphMovement.DOWN); ensureThat(factory.glyph.getSegments(), eq(latestSegments.movedDown())); latestSegments = latestSegments.movedDown(); @@ -134,7 +140,7 @@ game.requestStartGame(); // When... - game.requestDropGlyph(); + game.requestGlyphMovement(GlyphMovement.DROP); heartbeat.beat(); //Then...
Modified: trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/engine/ThreadedEngineQueueBehaviour.java (609 => 610)
--- trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/engine/ThreadedEngineQueueBehaviour.java 2006-12-04 16:25:01 UTC (rev 609) +++ trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/engine/ThreadedEngineQueueBehaviour.java 2006-12-04 17:13:30 UTC (rev 610) @@ -3,6 +3,7 @@ import jbehave.core.mock.Mock; import com.sirenian.hellbound.domain.game.GameRequestListener; +import com.sirenian.hellbound.domain.glyph.GlyphMovement; import com.sirenian.hellbound.util.ThreadedQueueBehaviour; public class ThreadedEngineQueueBehaviour extends ThreadedQueueBehaviour { @@ -10,23 +11,12 @@ public void shouldPassAlongGameRequests() throws Exception { Mock listener = mock(GameRequestListener.class); - listener.expects("requestStartGame"); - listener.expects("requestDropGlyph"); - listener.expects("requestMoveGlyphLeft"); - listener.expects("requestMoveGlyphRight"); - listener.expects("requestMoveGlyphDown"); - listener.expects("requestRotateGlyphLeft"); - listener.expects("requestRotateGlyphRight"); + listener.expects("requestGlyphMovement").with(GlyphMovement.DROP); final ThreadedEngineQueue queue = new ThreadedEngineQueue(); queue.setGameRequestDelegate((GameRequestListener) listener); queue.requestStartGame(); - queue.requestDropGlyph(); - queue.requestMoveGlyphLeft(); - queue.requestMoveGlyphRight(); - queue.requestMoveGlyphDown(); - queue.requestRotateGlyphLeft(); - queue.requestRotateGlyphRight(); + queue.requestGlyphMovement(GlyphMovement.DROP); synchronized(this) { wait(20); } // needs to be long enough for request queue to start up and pass along the request
Modified: trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/gui/HellboundFrameBehaviour.java (609 => 610)
--- trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/gui/HellboundFrameBehaviour.java 2006-12-04 16:25:01 UTC (rev 609) +++ trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/gui/HellboundFrameBehaviour.java 2006-12-04 17:13:30 UTC (rev 610) @@ -11,6 +11,7 @@ import com.sirenian.hellbound.domain.game.GameRequestListener; import com.sirenian.hellbound.domain.game.GameState; +import com.sirenian.hellbound.domain.glyph.GlyphMovement; public class HellboundFrameBehaviour extends UsingMiniMock { @@ -50,34 +51,34 @@ } public void shouldRequestThatTheShapeIsDroppedWhenTheSpaceKeyIsPressed() throws Exception { - ensureThatKeycodeProducesRequest(' ', "requestDropGlyph"); + ensureThatKeycodeProducesRequest(' ', GlyphMovement.DROP); } public void shouldRequestThatTheShapeIsMovedRightWhenTheRightKeyIsPressed() throws Exception { - ensureThatKeycodeProducesRequest(KeyEvent.VK_RIGHT, "requestMoveGlyphRight"); + ensureThatKeycodeProducesRequest(KeyEvent.VK_RIGHT, GlyphMovement.RIGHT); } public void shouldRequestThatTheShapeIsMovedLeftWhenTheMoveLeftKeyIsPressed() throws Exception { - ensureThatKeycodeProducesRequest(KeyEvent.VK_LEFT, "requestMoveGlyphLeft"); + ensureThatKeycodeProducesRequest(KeyEvent.VK_LEFT, GlyphMovement.LEFT); } public void shouldRequestThatTheShapeIsMovedDownWhenTheMoveDownKeyIsPressed() throws Exception { - ensureThatKeycodeProducesRequest(KeyEvent.VK_DOWN, "requestMoveGlyphDown"); + ensureThatKeycodeProducesRequest(KeyEvent.VK_DOWN, GlyphMovement.DOWN); } public void shouldRequestThatTheShapeIsRotatedLeftWhenTheZKeyIsPressed() throws Exception { - ensureThatKeycodeProducesRequest('z', "requestRotateGlyphLeft"); + ensureThatKeycodeProducesRequest('z', GlyphMovement.ROTATE_LEFT); } public void shouldRequestThatTheShapeIsRotatedRightWhenTheXKeyIsPressed() throws Exception { - ensureThatKeycodeProducesRequest('x', "requestRotateGlyphRight"); + ensureThatKeycodeProducesRequest('x', GlyphMovement.ROTATE_RIGHT); } - private void ensureThatKeycodeProducesRequest(int keycode, String expectedRequest) throws TimeoutException { + private void ensureThatKeycodeProducesRequest(int keycode, GlyphMovement movement) throws TimeoutException { setUp(); try { - gameRequestListener.expects(expectedRequest).once(); + gameRequestListener.expects("requestGlyphMovement").with(movement).once(); frame.reportGameStateChanged(GameState.RUNNING); windowWrapper.pressKeycode(keycode); @@ -87,10 +88,10 @@ } } - private void ensureThatKeycodeProducesRequest(char keyChar, String expectedRequest) throws TimeoutException { + private void ensureThatKeycodeProducesRequest(char keyChar, GlyphMovement movement) throws TimeoutException { setUp(); try { - gameRequestListener.expects(expectedRequest).once(); + gameRequestListener.expects("requestGlyphMovement").with(movement).once(); frame.reportGameStateChanged(GameState.RUNNING); windowWrapper.pressKeychar(keyChar);
Modified: trunk/examples/hellbound/src/java/com/sirenian/hellbound/domain/game/GameRequestListener.java (609 => 610)
--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/domain/game/GameRequestListener.java 2006-12-04 16:25:01 UTC (rev 609) +++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/domain/game/GameRequestListener.java 2006-12-04 17:13:30 UTC (rev 610) @@ -1,29 +1,15 @@ package com.sirenian.hellbound.domain.game; +import com.sirenian.hellbound.domain.glyph.GlyphMovement; + public interface GameRequestListener { GameRequestListener NULL = new GameRequestListener() { public void requestStartGame() {} - public void requestDropGlyph() {} - public void requestMoveGlyphDown() {} - public void requestMoveGlyphLeft() {} - public void requestMoveGlyphRight() {} - public void requestRotateGlyphLeft() {} - public void requestRotateGlyphRight() {} + public void requestGlyphMovement(GlyphMovement movement) {} }; void requestStartGame(); - void requestDropGlyph(); - - void requestMoveGlyphRight(); - - void requestMoveGlyphLeft(); - - void requestMoveGlyphDown(); - - void requestRotateGlyphLeft(); - - void requestRotateGlyphRight(); - + void requestGlyphMovement(GlyphMovement movement); }
Added: trunk/examples/hellbound/src/java/com/sirenian/hellbound/domain/glyph/GlyphMovement.java (0 => 610)
--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/domain/glyph/GlyphMovement.java (rev 0) +++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/domain/glyph/GlyphMovement.java 2006-12-04 17:13:30 UTC (rev 610) @@ -0,0 +1,53 @@ +package com.sirenian.hellbound.domain.glyph; + +public abstract class GlyphMovement { + + public static final GlyphMovement DOWN = new GlyphMovement("down"){ + public boolean performOn(LivingGlyph glyph) { + return glyph.requestMoveDown(); + } + }; + + public static final GlyphMovement LEFT = new GlyphMovement("left"){ + public boolean performOn(LivingGlyph glyph) { + return glyph.requestMoveLeft(); + } + }; + + public static final GlyphMovement RIGHT = new GlyphMovement("right"){ + public boolean performOn(LivingGlyph glyph) { + return glyph.requestMoveRight(); + } + }; + + public static final GlyphMovement ROTATE_LEFT = new GlyphMovement("rotate left"){ + public boolean performOn(LivingGlyph glyph) { + return glyph.requestRotateLeft(); + } + }; + + public static final GlyphMovement ROTATE_RIGHT = new GlyphMovement("rotate right"){ + public boolean performOn(LivingGlyph glyph) { + return glyph.requestRotateRight(); + } + }; + + public static final GlyphMovement DROP = new GlyphMovement("drop"){ + public boolean performOn(LivingGlyph glyph) { + glyph.drop(); + return true; + } + }; + + private final String description; + + public GlyphMovement(String description) { + this.description = description; + } + + public abstract boolean performOn(LivingGlyph glyph); + + public String toString() { + return description; + } +}
Modified: trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/Game.java (609 => 610)
--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/Game.java 2006-12-04 16:25:01 UTC (rev 609) +++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/Game.java 2006-12-04 17:13:30 UTC (rev 610) @@ -5,6 +5,7 @@ import com.sirenian.hellbound.domain.game.GameRequestListener; import com.sirenian.hellbound.domain.game.GameState; import com.sirenian.hellbound.domain.glyph.GlyphListener; +import com.sirenian.hellbound.domain.glyph.GlyphMovement; import com.sirenian.hellbound.domain.glyph.Heartbeat; import com.sirenian.hellbound.domain.glyph.HeartbeatListener; import com.sirenian.hellbound.domain.glyph.Junk; @@ -58,7 +59,7 @@ public void beat() { if (state == GameState.RUNNING) { Logger.debug(this, "Hearing heartbeat; moving glyph down"); - moveGlyphDownOrJunkIt(); + requestGlyphMovement(GlyphMovement.DOWN); } } }; @@ -95,46 +96,18 @@ glyph.addListener(listener); } - public void requestDropGlyph() { - Logger.debug(this, "Drop of glyph requested"); + public void requestGlyphMovement(GlyphMovement movement) { if (state == GameState.RUNNING) { - glyph.drop(); + Logger.debug(this, "Glyph movement " + movement + "requested"); + boolean result = movement.performOn(glyph); + + if (result == false && movement == GlyphMovement.DOWN) { + Logger.debug(this, "Could not move glyph down; junking it"); + junk.absorb(glyph); + resetGlyph(); + } } } - - private void moveGlyphDownOrJunkIt() { - if (!glyph.requestMoveDown()) { - Logger.debug(this, "Could not move glyph down; junking it"); - junk.absorb(glyph); - resetGlyph(); - } - } - - public void requestMoveGlyphDown() { - Logger.debug(this, "Move glyph down requested"); - moveGlyphDownOrJunkIt(); - } - - public void requestMoveGlyphLeft() { - Logger.debug(this, "Move glyph left requested"); - glyph.requestMoveLeft(); - - } - - public void requestMoveGlyphRight() { - Logger.debug(this, "Move glyph right requested"); - glyph.requestMoveRight(); - } - - public void requestRotateGlyphLeft() { - Logger.debug(this, "Rotate glyph left requested"); - glyph.requestRotateLeft(); - } - - public void requestRotateGlyphRight() { - Logger.debug(this, "Rotate glyph left requested"); - glyph.requestRotateRight(); - }
Modified: trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/ThreadedEngineQueue.java (609 => 610)
--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/ThreadedEngineQueue.java 2006-12-04 16:25:01 UTC (rev 609) +++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/ThreadedEngineQueue.java 2006-12-04 17:13:30 UTC (rev 610) @@ -1,6 +1,7 @@ package com.sirenian.hellbound.engine; import com.sirenian.hellbound.domain.game.GameRequestListener; +import com.sirenian.hellbound.domain.glyph.GlyphMovement; import com.sirenian.hellbound.util.ThreadedQueue; public class ThreadedEngineQueue extends ThreadedQueue implements EngineQueue { @@ -28,59 +29,13 @@ action.run(); } - public void requestDropGlyph() { + public void requestGlyphMovement(final GlyphMovement movement) { queue(new Runnable() { - public void run() { gameRequestListener.requestDropGlyph(); } + public void run() { gameRequestListener.requestGlyphMovement(movement); } public String toString() { - return "runnable gameRequestListener.requestDropGlyph()"; + return "runnable gameRequestListener.requestGlyphMovement(" + movement + ")"; } }); - } - public void requestMoveGlyphDown() { - queue(new Runnable() { - public void run() { gameRequestListener.requestMoveGlyphDown(); } - public String toString() { - return "runnable gameRequestListener.requestMoveGlyphDown()"; - } - }); - } - - public void requestMoveGlyphLeft() { - queue(new Runnable() { - public void run() { gameRequestListener.requestMoveGlyphLeft(); } - public String toString() { - return "runnable gameRequestListener.requestMoveGlyphLeft()"; - } - }); - } - - public void requestMoveGlyphRight() { - queue(new Runnable() { - public void run() { gameRequestListener.requestMoveGlyphRight(); } - public String toString() { - return "runnable gameRequestListener.requestMoveGlyphRight()"; - } - }); - } - - public void requestRotateGlyphLeft() { - queue(new Runnable() { - public void run() { gameRequestListener.requestRotateGlyphLeft(); } - public String toString() { - return "runnable gameRequestListener.requestRotateGlyphLeft()"; - } - }); - } - - public void requestRotateGlyphRight() { - queue(new Runnable() { - public void run() { gameRequestListener.requestRotateGlyphRight(); } - public String toString() { - return "runnable gameRequestListener.requestRotateGlyphRight()"; - } - }); - } - }
Modified: trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/ActionFactory.java (609 => 610)
--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/ActionFactory.java 2006-12-04 16:25:01 UTC (rev 609) +++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/ActionFactory.java 2006-12-04 17:13:30 UTC (rev 610) @@ -6,62 +6,18 @@ import javax.swing.Action; import com.sirenian.hellbound.domain.game.GameRequestListener; +import com.sirenian.hellbound.domain.glyph.GlyphMovement; import com.sirenian.hellbound.util.Logger; public class ActionFactory { - public Action right(final GameRequestListener gameRequestListener) { + public Action createAction(final GameRequestListener gameRequestListener, final GlyphMovement movement) { + Logger.debug(this, "Creating " + movement + " action "); return new AbstractAction() { public void actionPerformed(ActionEvent e) { - Logger.debug(this, "Adding right action to frame action map"); - gameRequestListener.requestMoveGlyphRight(); + gameRequestListener.requestGlyphMovement(movement); } }; - } - - public Action left(final GameRequestListener gameRequestListener) { - Logger.debug(this, "Adding left action to frame action map"); - return new AbstractAction() { - public void actionPerformed(ActionEvent e) { - gameRequestListener.requestMoveGlyphLeft(); - } - }; - } - - public Action down(final GameRequestListener gameRequestListener) { - return new AbstractAction() { - public void actionPerformed(ActionEvent e) { - Logger.debug(this, "Adding down action to frame action map"); - gameRequestListener.requestMoveGlyphDown(); - } - }; - } - - public Action drop(final GameRequestListener gameRequestListener) { - return new AbstractAction() { - public void actionPerformed(ActionEvent e) { - Logger.debug(this, "Adding drop action to frame action map"); - gameRequestListener.requestDropGlyph(); - } - }; - } - - public Action leftRotate(final GameRequestListener gameRequestListener) { - return new AbstractAction() { - public void actionPerformed(ActionEvent e) { - Logger.debug(this, "Adding left rotate action to frame action map"); - gameRequestListener.requestRotateGlyphLeft(); - } - }; - } - - public Action rightRotate(final GameRequestListener gameRequestListener) { - return new AbstractAction() { - public void actionPerformed(ActionEvent e) { - Logger.debug(this, "Adding right rotate action to frame action map"); - gameRequestListener.requestRotateGlyphRight(); - } - }; } }
Modified: trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/HellboundFrame.java (609 => 610)
--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/HellboundFrame.java 2006-12-04 16:25:01 UTC (rev 609) +++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/HellboundFrame.java 2006-12-04 17:13:30 UTC (rev 610) @@ -10,6 +10,7 @@ import com.sirenian.hellbound.domain.game.GameListener; import com.sirenian.hellbound.domain.game.GameRequestListener; import com.sirenian.hellbound.domain.game.GameState; +import com.sirenian.hellbound.domain.glyph.GlyphMovement; import com.sirenian.hellbound.util.Logger; public class HellboundFrame extends JFrame implements GameListener { @@ -44,12 +45,12 @@ public void setGameRequestListener(final GameRequestListener gameRequestListener) { contentPanel.getActionMap().clear(); - contentPanel.getActionMap().put("drop", actionFactory.drop(gameRequestListener)); - contentPanel.getActionMap().put("left", actionFactory.left(gameRequestListener)); - contentPanel.getActionMap().put("right", actionFactory.right(gameRequestListener)); - contentPanel.getActionMap().put("down", actionFactory.down(gameRequestListener)); - contentPanel.getActionMap().put("leftRotate", actionFactory.leftRotate(gameRequestListener)); - contentPanel.getActionMap().put("rightRotate", actionFactory.rightRotate(gameRequestListener)); + contentPanel.getActionMap().put("drop", actionFactory.createAction(gameRequestListener, GlyphMovement.DROP)); + contentPanel.getActionMap().put("left", actionFactory.createAction(gameRequestListener, GlyphMovement.LEFT)); + contentPanel.getActionMap().put("right", actionFactory.createAction(gameRequestListener, GlyphMovement.RIGHT)); + contentPanel.getActionMap().put("down", actionFactory.createAction(gameRequestListener, GlyphMovement.DOWN)); + contentPanel.getActionMap().put("leftRotate", actionFactory.createAction(gameRequestListener, GlyphMovement.ROTATE_LEFT)); + contentPanel.getActionMap().put("rightRotate", actionFactory.createAction(gameRequestListener, GlyphMovement.ROTATE_RIGHT)); } public void reportGameStateChanged(GameState state) {
To unsubscribe from this list please visit: