From 0a5ace745f171cad0150dbed11ae8febc0e17f27 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 10 Mar 2009 16:33:01 -0400 Subject: Engine: Added stalking movement type Also added a class (ImmutableEvent) that provides an immutable view to an event: It is like Collections.unmodifiableMap(), the event data may change in the mean time, and the object will reflect that, but the class cannot be modified. MovementType now provides an ImmutableEvent to each of its subclasses when nextMovement() is called. --- .../gamestate/mapview/event/ImmutableEvent.java | 77 +++++++++++ .../gamestate/mapview/event/LayerEvent.java | 18 +-- .../mapview/event/movement/CustomMovementType.java | 5 +- .../mapview/event/movement/FollowMovementType.java | 142 +++++++++++++++++++++ .../mapview/event/movement/MovementType.java | 3 +- .../mapview/event/movement/RandomMovementType.java | 40 ++---- .../event/movement/StayStillMovementType.java | 3 +- 7 files changed, 245 insertions(+), 43 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/event/ImmutableEvent.java create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/FollowMovementType.java (limited to 'src') diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/ImmutableEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/ImmutableEvent.java new file mode 100644 index 0000000..e2eea05 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/ImmutableEvent.java @@ -0,0 +1,77 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview.event; + +import com.fourisland.fourpuzzle.Direction; +import com.fourisland.fourpuzzle.Layer; +import com.fourisland.fourpuzzle.gamestate.mapview.Map; +import java.awt.Point; +import java.util.Collections; +import java.util.List; + +/** + * + * @author hatkirby + */ +public final class ImmutableEvent +{ + final Event ev; + public ImmutableEvent(Event ev) + { + this.ev = ev; + } + + public String getLabel() + { + return new String(ev.getLabel()); + } + + public Point getLocation() + { + return new Point(ev.getLocation()); + } + + public Direction getDirection() + { + return ev.getDirection(); + } + + public boolean isMoving() + { + return ev.isMoving(); + } + + public Layer getLayer() + { + return ev.getLayer(); + } + + public boolean isOccupyingSpace(int x, int y) + { + return ev.isOccupyingSpace(x, y); + } + + public List getLegalMoves() + { + return Collections.unmodifiableList(ev.getLegalMoves()); + } + + public int getAnimationStep() + { + return ev.getAnimationStep(); + } + + public Map getParentMap() + { + return ev.getParentMap(); + } + + public MoveSpeed getMoveSpeed() + { + return ev.getMoveSpeed(); + } + +} diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java index a95cad1..9788b2e 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java @@ -99,23 +99,15 @@ public class LayerEvent extends AbstractEvent implements Event { public void startMoving() { - Direction toMove = getPossibleEvent().getMovement().nextMovement(); + Direction toMove = getPossibleEvent().getMovement().nextMovement(new ImmutableEvent(this)); if (toMove != null) { - startMoving(toMove); - } - } - - @Override - public boolean startMoving(Direction toMove) - { - if (!(getPossibleEvent().getGraphic() instanceof MoveableEventGraphic)) - { - return false; + if (getPossibleEvent().getGraphic() instanceof MoveableEventGraphic) + { + startMoving(toMove); + } } - - return super.startMoving(toMove); } public Direction getDirection() diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/CustomMovementType.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/CustomMovementType.java index 0002c03..78dd991 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/CustomMovementType.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/CustomMovementType.java @@ -6,6 +6,7 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event.movement; import com.fourisland.fourpuzzle.Direction; +import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent; /** * CustomMovementEvent takes an array of Directions and directions the event @@ -24,8 +25,8 @@ public class CustomMovementType implements MovementType { this.moves = moves; } - public Direction nextMovement() - { + public Direction nextMovement(ImmutableEvent ev) + { if (step >= moves.length) { step = 0; diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/FollowMovementType.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/FollowMovementType.java new file mode 100644 index 0000000..b84fc4a --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/FollowMovementType.java @@ -0,0 +1,142 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview.event.movement; + +import com.fourisland.fourpuzzle.Direction; +import com.fourisland.fourpuzzle.Game; +import com.fourisland.fourpuzzle.gamestate.mapview.event.Event; +import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent; +import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent; +import java.awt.Point; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Deque; +import java.util.List; + +/** + * FollowMovementType allows an Event to continually walk toward a specified + * Event. + * + * @author hatkirby + */ +public class FollowMovementType implements MovementType { + + String name = null; + Event event = null; + Point lastLoc = new Point(); + Deque moves = new ArrayDeque(); + List attempts = new ArrayList(); + + public FollowMovementType() {} + + public FollowMovementType(String name) + { + this.name = name; + } + + private boolean search(ImmutableEvent ev) + { + for (Direction d : Direction.values()) + { + Point loc = d.to(ev.getLocation()); + if (lastLoc.equals(loc)) + { + return true; + } + } + + List ds = ev.getLegalMoves(); + List tempd = new ArrayList(); + + if (lastLoc.x < ev.getLocation().x) + { + tempd.add(Direction.West); + } else if (lastLoc.x > ev.getLocation().x) + { + tempd.add(Direction.East); + } else { + if (!ds.contains(Direction.North) || !ds.contains(Direction.South)) + { + tempd.add(Direction.West); + tempd.add(Direction.East); + } + } + + if (lastLoc.y < ev.getLocation().y) + { + tempd.add(Direction.North); + } else if (lastLoc.y > ev.getLocation().y) + { + tempd.add(Direction.South); + } else { + if (!ds.contains(Direction.West) || !ds.contains(Direction.East)) + { + tempd.add(Direction.North); + tempd.add(Direction.South); + } + } + + tempd.retainAll(ds); + Collections.shuffle(tempd); + + for (Direction d : tempd) + { + Point loc = d.to(ev.getLocation()); + + if (attempts.contains(loc)) + { + continue; + } + + Event temp = new LayerEvent(loc.x, loc.y); + temp.setParentMap(ev.getParentMap()); + attempts.add(loc); + + if (search(new ImmutableEvent(temp))) + { + moves.push(d); + return true; + } + } + + return false; + } + + public Direction nextMovement(ImmutableEvent ev) + { + if (event == null) + { + if (name == null) + { + event = Game.getHeroEvent(); + } else { + event = ev.getParentMap().getEvent(name); + } + } else if ((name != null) && !(event.getParentMap().equals(ev.getParentMap()))) + { + event = ev.getParentMap().getEvent(name); + } + + if (!event.getLocation().equals(lastLoc)) + { + lastLoc.setLocation(event.getLocation()); + + moves.clear(); + attempts.clear(); + + search(ev); + } + + if (!moves.isEmpty()) + { + return moves.pop(); + } + + return null; + } + +} \ No newline at end of file diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/MovementType.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/MovementType.java index f304ec0..bd47793 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/MovementType.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/MovementType.java @@ -6,6 +6,7 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event.movement; import com.fourisland.fourpuzzle.Direction; +import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent; /** * A MovementType is an object that specifies the type of AI a non-hero event @@ -17,6 +18,6 @@ import com.fourisland.fourpuzzle.Direction; */ public interface MovementType { - public Direction nextMovement(); + public Direction nextMovement(ImmutableEvent ev); } \ No newline at end of file diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/RandomMovementType.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/RandomMovementType.java index fd65a41..1b1628e 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/RandomMovementType.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/RandomMovementType.java @@ -6,6 +6,9 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event.movement; import com.fourisland.fourpuzzle.Direction; +import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent; +import com.fourisland.fourpuzzle.util.Interval; +import java.util.List; import java.util.Random; /** @@ -15,34 +18,19 @@ import java.util.Random; */ public class RandomMovementType implements MovementType { - public Direction nextMovement() + Interval in = Interval.createTickInterval(10); + public Direction nextMovement(ImmutableEvent ev) { - Random r = new Random(); - int ra = r.nextInt(1000); - Direction toMove = null; - boolean letsMove = false; - - if (ra < 25) - { - toMove = Direction.North; - letsMove = true; - } else if (ra < 50) - { - toMove = Direction.West; - letsMove = true; - } else if (ra < 75) - { - toMove = Direction.South; - letsMove = true; - } else if (ra < 100) - { - toMove = Direction.East; - letsMove = true; - } - - if (letsMove) + if (in.isElapsed()) { - return toMove; + List moves = ev.getLegalMoves(); + Random r = new Random(); + int ra = r.nextInt(moves.size()); + + if (ra != moves.size()) + { + return moves.get(ra); + } } return null; diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/StayStillMovementType.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/StayStillMovementType.java index e373de9..6fb376f 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/StayStillMovementType.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/StayStillMovementType.java @@ -6,6 +6,7 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event.movement; import com.fourisland.fourpuzzle.Direction; +import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent; /** * StayStillMovementType keeps the event stationary. @@ -14,7 +15,7 @@ import com.fourisland.fourpuzzle.Direction; */ public class StayStillMovementType implements MovementType { - public Direction nextMovement() + public Direction nextMovement(ImmutableEvent ev) { return null; // Do nothing, stay still } -- cgit 1.4.1