From 28e41757f11ea216f641b4889bd43be2b6373484 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 17 Mar 2009 09:34:18 -0400 Subject: Engine: Added ShakeScreen() special action Also implemented F12 to return to the title screen and added a move frequency variable to events that decides how often they move. --- nbproject/project.properties | 1 - .../fourisland/fourpuzzle/PuzzleApplication.java | 9 ++ .../gamestate/mapview/MapViewGameState.java | 6 ++ .../gamestate/mapview/event/AbstractEvent.java | 11 +++ .../fourpuzzle/gamestate/mapview/event/Event.java | 3 + .../gamestate/mapview/event/HeroEvent.java | 1 + .../gamestate/mapview/event/LayerEvent.java | 29 +++++-- .../gamestate/mapview/event/MoveFrequency.java | 34 ++++++++ .../gamestate/mapview/event/PossibleEvent.java | 20 +++++ .../gamestate/mapview/event/SpecialEvent.java | 50 ++++++++++++ .../mapview/viewpoint/ShakingViewpoint.java | 95 ++++++++++++++++++++++ src/com/fourisland/fourpuzzle/util/Interval.java | 6 +- .../event/precondition/SwitchPreconditionTest.java | 52 ++++++++++++ 13 files changed, 308 insertions(+), 9 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/event/MoveFrequency.java create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/ShakingViewpoint.java create mode 100644 test/com/fourisland/fourpuzzle/gamestate/mapview/event/precondition/SwitchPreconditionTest.java diff --git a/nbproject/project.properties b/nbproject/project.properties index b91a61d..162d339 100755 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -30,7 +30,6 @@ javac.target=1.5 javac.test.classpath=\ ${javac.classpath}:\ ${build.classes.dir}:\ - ${libs.junit.classpath}:\ ${libs.junit_4.classpath} javadoc.additionalparam= javadoc.author=false diff --git a/src/com/fourisland/fourpuzzle/PuzzleApplication.java b/src/com/fourisland/fourpuzzle/PuzzleApplication.java index 36dda19..f88b0ad 100755 --- a/src/com/fourisland/fourpuzzle/PuzzleApplication.java +++ b/src/com/fourisland/fourpuzzle/PuzzleApplication.java @@ -5,6 +5,7 @@ package com.fourisland.fourpuzzle; import com.fourisland.fourpuzzle.database.Database; +import com.fourisland.fourpuzzle.database.Transitions; import com.fourisland.fourpuzzle.database.Vocabulary; import com.fourisland.fourpuzzle.gamestate.TitleScreenGameState; import com.fourisland.fourpuzzle.util.Interval; @@ -104,6 +105,14 @@ public class PuzzleApplication extends Application { { debugSpeed = true; } + } else if (e.getKeyCode() == KeyEvent.VK_F12) + { + try { + /* If the user presses F12, return to the title screen */ + Display.transition(Database.getTransition(Transitions.Generic), new TitleScreenGameState(), false); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } } else { // If anything else is pressed, let the GameState handle it KeyboardInput.getKey().keyInput(e); diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index ec35db6..89fac99 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java @@ -25,6 +25,7 @@ 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.gamestate.menu.MenuGameState; +import java.awt.Color; import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; @@ -260,6 +261,11 @@ public class MapViewGameState implements GameState { int x = currentViewpoint.getX(); int y = currentViewpoint.getY(); + /* Fill the background with black so specialized viewpoints that go off + * the screen such as ShakingViewpoint render properly */ + g.setColor(Color.BLACK); + g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT); + // Render the lower layer of the map g.drawImage(currentMap.renderLower(), 0, 0, Game.WIDTH, Game.HEIGHT, x, y, x+Game.WIDTH, y+Game.HEIGHT, null); diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java index e9482f9..aa2c3b3 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java @@ -218,4 +218,15 @@ public abstract class AbstractEvent implements Event { { return moveSpeed; } + + private MoveFrequency freq; + public void setFrequency(MoveFrequency freq) + { + this.freq = freq; + } + + public MoveFrequency getFrequency() + { + return freq; + } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java index 0cda403..936394b 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java @@ -51,4 +51,7 @@ public interface Event { public void setMoveSpeed(MoveSpeed moveSpeed); public MoveSpeed getMoveSpeed(); + + public void setFrequency(MoveFrequency freq); + public MoveFrequency getFrequency(); } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java index 5db87a0..1df6c1d 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java @@ -25,6 +25,7 @@ public class HeroEvent extends AbstractEvent implements Event { { setLocation(new Point()); setMoveSpeed(MoveSpeed.Faster); + setFrequency(MoveFrequency.EIGHT); } public String getLabel() diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java index 9788b2e..afaf923 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java @@ -12,6 +12,7 @@ import com.fourisland.fourpuzzle.Direction; import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.BlankEventGraphic; import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.MoveableEventGraphic; import com.fourisland.fourpuzzle.gamestate.mapview.event.precondition.Precondition; +import com.fourisland.fourpuzzle.util.PauseTimer; /** * @@ -97,15 +98,21 @@ public class LayerEvent extends AbstractEvent implements Event { } } + PauseTimer pt = new PauseTimer(0); public void startMoving() { - Direction toMove = getPossibleEvent().getMovement().nextMovement(new ImmutableEvent(this)); - - if (toMove != null) + if (pt.isElapsed()) { - if (getPossibleEvent().getGraphic() instanceof MoveableEventGraphic) + pt.setTimer(getFrequency().getFrequency()-1); + + Direction toMove = getPossibleEvent().getMovement().nextMovement(new ImmutableEvent(this)); + + if (toMove != null) { - startMoving(toMove); + if (getPossibleEvent().getGraphic() instanceof MoveableEventGraphic) + { + startMoving(toMove); + } } } } @@ -169,4 +176,16 @@ public class LayerEvent extends AbstractEvent implements Event { return getPossibleEvent().getMoveSpeed(); } + @Override + public void setFrequency(MoveFrequency freq) + { + getPossibleEvent().setFrequency(freq); + } + + @Override + public MoveFrequency getFrequency() + { + return getPossibleEvent().getFrequency(); + } + } \ No newline at end of file diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/MoveFrequency.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/MoveFrequency.java new file mode 100644 index 0000000..3e3cc0e --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/MoveFrequency.java @@ -0,0 +1,34 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview.event; + +/** + * + * @author hatkirby + */ +public enum MoveFrequency { + + EIGHT(1), + SEVEN(2), + SIX(3), + FIVE(4), + FOUR(5), + THREE(6), + TWO(7), + ONE(8); + + private int freq; + private MoveFrequency(int freq) + { + this.freq = freq; + } + + public int getFrequency() + { + return freq; + } + +} diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java index 7f255dd..3f35943 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java @@ -25,6 +25,7 @@ public class PossibleEvent { private AnimationType animation; private MovementType movement; private MoveSpeed moveSpeed; + private MoveFrequency freq; private EventCallTime calltime; private EventCall callback; @@ -44,6 +45,7 @@ public class PossibleEvent { private AnimationType animation = AnimationType.CommonWithoutStepping; private MovementType movement = new StayStillMovementType(); private MoveSpeed moveSpeed = MoveSpeed.Normal; + private MoveFrequency freq = MoveFrequency.EIGHT; private EventCallTime calltime = EventCallTime.PushKey; private EventCall callback = EventCall.getEmptyEventCall(); @@ -77,6 +79,12 @@ public class PossibleEvent { return this; } + public Builder freq(MoveFrequency freq) + { + this.freq = freq; + return this; + } + public Builder calltime(EventCallTime calltime) { this.calltime = calltime; @@ -102,6 +110,7 @@ public class PossibleEvent { animation = builder.animation; movement = builder.movement; moveSpeed = builder.moveSpeed; + freq = builder.freq; calltime = builder.calltime; callback = builder.callback; } @@ -114,6 +123,7 @@ public class PossibleEvent { .animation(animation) .movement(movement) .speed(moveSpeed) + .freq(freq) .calltime(calltime) .callback(callback) .build(); @@ -154,6 +164,16 @@ public class PossibleEvent { { return moveSpeed; } + + void setFrequency(MoveFrequency freq) + { + this.freq = freq; + } + + public MoveFrequency getFrequency() + { + return freq; + } private boolean moving = false; void setMoving(boolean moving) diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java index ca3ba5e..2db33f7 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java @@ -16,6 +16,8 @@ import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventTh 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.ShakingViewpoint; +import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.ShakingViewpoint.ShakeSpeed; import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.Viewpoint; import com.fourisland.fourpuzzle.transition.InTransition; import com.fourisland.fourpuzzle.transition.OutTransition; @@ -366,4 +368,52 @@ public class SpecialEvent { throw new InterruptedException(); } + /** + * Shake the screen like an earthquake + * + * @param speed How fast the screen should shake + * @param length The amount of time (in milliseconds) the shaking should + * last + * @param block If true, the game will wait for the shaking to complete + * before executing any more commands + * @throws java.lang.InterruptedException + */ + public void ShakeScreen(ShakeSpeed speed, int length, final boolean block) throws InterruptedException + { + Viewpoint viewpoint = mapView.getViewpoint(); + final CountDownLatch blocker; + + if (block) + { + blocker = new CountDownLatch(1); + } else { + blocker = null; + } + + mapView.setViewpoint(new ShakingViewpoint(viewpoint.getX(), viewpoint.getY(), speed, length, new Runnable() { + public void run() + { + if (block) + { + blocker.countDown(); + } else { + ResetViewpoint(); + } + } + })); + + if (block) + { + try + { + blocker.await(); + } catch (InterruptedException ex) + { + throw ex; + } finally { + ResetViewpoint(); + } + } + } + } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/ShakingViewpoint.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/ShakingViewpoint.java new file mode 100644 index 0000000..f2b40e7 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/ShakingViewpoint.java @@ -0,0 +1,95 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview.viewpoint; + +import com.fourisland.fourpuzzle.util.Interval; + +/** + * + * @author hatkirby + */ +public class ShakingViewpoint implements Viewpoint { + + int sx; + int length; + int x; + int y; + ShakeSpeed speed; + Interval in; + Runnable callback; + boolean back = false; + long start; + public ShakingViewpoint(int x, int y, ShakeSpeed speed, int length, Runnable callback) + { + this.sx = x; + this.length = length; + this.x = x; + this.y = y; + this.in = Interval.createMillisInterval(1000 / speed.getSpeed()); + this.speed = speed; + this.callback = callback; + this.start = System.currentTimeMillis(); + } + + private void refresh() + { + if (back) + { + x -= speed.getSpeed(); + + if (x < sx - 16) + { + back = false; + } + } else { + x += speed.getSpeed(); + + if (x > sx + 16) + { + back = true; + } + } + + if (start + length <= System.currentTimeMillis()) + { + callback.run(); + } + } + + public int getX() + { + if (in.isElapsed()) + { + refresh(); + } + + return x; + } + + public int getY() + { + return y; + } + + public static enum ShakeSpeed + { + Slow(16), + Medium(24), + Fast(32); + + private int speed; + private ShakeSpeed(int speed) + { + this.speed = speed; + } + + public int getSpeed() + { + return speed; + } + } + +} diff --git a/src/com/fourisland/fourpuzzle/util/Interval.java b/src/com/fourisland/fourpuzzle/util/Interval.java index 3383a39..1e5e8f3 100755 --- a/src/com/fourisland/fourpuzzle/util/Interval.java +++ b/src/com/fourisland/fourpuzzle/util/Interval.java @@ -22,12 +22,12 @@ public class Interval { public static Interval createTickInterval(float ticks) { - return createMillisInterval((int) (Game.FPS * ticks)); + return createMillisInterval(Game.FPS * ticks); } - public static Interval createMillisInterval(int millis) + public static Interval createMillisInterval(float millis) { - return new Interval(millis*1000000); + return new Interval((int) (millis*1000000)); } private long last = System.nanoTime(); diff --git a/test/com/fourisland/fourpuzzle/gamestate/mapview/event/precondition/SwitchPreconditionTest.java b/test/com/fourisland/fourpuzzle/gamestate/mapview/event/precondition/SwitchPreconditionTest.java new file mode 100644 index 0000000..bc86459 --- /dev/null +++ b/test/com/fourisland/fourpuzzle/gamestate/mapview/event/precondition/SwitchPreconditionTest.java @@ -0,0 +1,52 @@ +package com.fourisland.fourpuzzle.gamestate.mapview.event.precondition; + +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +import com.fourisland.fourpuzzle.Game; +import com.fourisland.fourpuzzle.SaveFile; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author hatkirby + */ +public class SwitchPreconditionTest { + + public SwitchPreconditionTest() { + } + + String switchName = "TestSwitch"; + SwitchPrecondition sp; + + @Before + public void setUp() + { + Game.setSaveFile(new SaveFile()); + sp = new SwitchPrecondition(switchName); + } + + @Test + public void testUnsetSwitch() + { + Game.getSaveFile().getSwitches().put(switchName, false); + assertFalse(sp.match()); + } + + @Test + public void testSetSwitch() + { + Game.getSaveFile().getSwitches().put(switchName, true); + assertTrue(sp.match()); + } + + @After + public void tearDown() { + } + +} \ No newline at end of file -- cgit 1.4.1