From eeee11b2ace3af986173bf7b6d2bc2a1eae97a1b Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sun, 8 Mar 2009 10:57:22 -0400 Subject: Engine: Added a to() method to Direction to() takes a Point and returns a Point one unit in the specified direction from the original point. --- src/com/fourisland/fourpuzzle/Direction.java | 44 ++++++++++++++++++++++ .../gamestate/mapview/event/AbstractEvent.java | 31 ++++++++------- .../fourpuzzle/gamestate/mapview/event/Event.java | 2 + 3 files changed, 63 insertions(+), 14 deletions(-) (limited to 'src/com') diff --git a/src/com/fourisland/fourpuzzle/Direction.java b/src/com/fourisland/fourpuzzle/Direction.java index 72ea595..658d3ff 100755 --- a/src/com/fourisland/fourpuzzle/Direction.java +++ b/src/com/fourisland/fourpuzzle/Direction.java @@ -5,6 +5,8 @@ package com.fourisland.fourpuzzle; +import java.awt.Point; + /** * * @author hatkirby @@ -26,6 +28,14 @@ public enum Direction { { return Direction.East; } + + public Point to(Point original) + { + Point temp = new Point(original); + temp.translate(0, -1); + + return temp; + } }, East { @@ -43,6 +53,14 @@ public enum Direction { { return Direction.South; } + + public Point to(Point original) + { + Point temp = new Point(original); + temp.translate(1, 0); + + return temp; + } }, South { @@ -60,6 +78,14 @@ public enum Direction { { return Direction.West; } + + public Point to(Point original) + { + Point temp = new Point(original); + temp.translate(0, 1); + + return temp; + } }, West { @@ -77,6 +103,14 @@ public enum Direction { { return Direction.North; } + + public Point to(Point original) + { + Point temp = new Point(original); + temp.translate(-1, 0); + + return temp; + } }; /** @@ -99,4 +133,14 @@ public enum Direction { * @return A Direction representing the wanted direction */ public abstract Direction right(); + + /** + * Returns a point one unit in the specified direction away from the + * specified point + * + * @param original The point to move away from + * @return A Point representing a space one unit away from the original + */ + public abstract Point to(Point original); + } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java index 2933fff..a2616e5 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java @@ -9,6 +9,8 @@ import com.fourisland.fourpuzzle.Direction; import com.fourisland.fourpuzzle.gamestate.mapview.Map; import com.fourisland.fourpuzzle.util.Functions; import java.awt.Point; +import java.util.ArrayList; +import java.util.List; /** * @@ -79,20 +81,7 @@ public abstract class AbstractEvent implements Event { { setAnimationStep(1); setMoving(false); - - if (getDirection() == Direction.North) - { - setLocation(getLocation().x,getLocation().y-1); - } else if (getDirection() == Direction.West) - { - setLocation(getLocation().x-1,getLocation().y); - } else if (getDirection() == Direction.South) - { - setLocation(getLocation().x,getLocation().y+1); - } else if (getDirection() == Direction.East) - { - setLocation(getLocation().x+1,getLocation().y); - } + setLocation(getDirection().to(getLocation())); } } } @@ -112,6 +101,20 @@ public abstract class AbstractEvent implements Event { return false; } + public List getLegalMoves() + { + List temp = new ArrayList(); + for (Direction d : Direction.values()) + { + if (!getParentMap().checkForCollision(this, d)) + { + temp.add(d); + } + } + + return temp; + } + private Map parentMap = null; public Map getParentMap() { diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java index 887e52b..e6443c6 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java @@ -10,6 +10,7 @@ import com.fourisland.fourpuzzle.Layer; import com.fourisland.fourpuzzle.gamestate.mapview.Map; import java.awt.Graphics; import java.awt.Point; +import java.util.List; /** * @@ -40,6 +41,7 @@ public interface Event { public Layer getLayer(); public boolean isOccupyingSpace(int x, int y); + public List getLegalMoves(); public void setAnimationStep(int animStep); public int getAnimationStep(); -- cgit 1.4.1