summary refs log tree commit diff stats
path: root/src/com/fourisland
diff options
context:
space:
mode:
authorStarla Insigna <hatkirby@fourisland.com>2009-03-10 16:33:01 -0400
committerStarla Insigna <hatkirby@fourisland.com>2009-03-10 16:33:01 -0400
commit0a5ace745f171cad0150dbed11ae8febc0e17f27 (patch)
tree184feabfb008e77ddc4eb45931103b50f031d6a3 /src/com/fourisland
parentaa575d82717f50c3724be72ca7ab4bcb8de725ee (diff)
downloadfourpuzzle-0a5ace745f171cad0150dbed11ae8febc0e17f27.tar.gz
fourpuzzle-0a5ace745f171cad0150dbed11ae8febc0e17f27.tar.bz2
fourpuzzle-0a5ace745f171cad0150dbed11ae8febc0e17f27.zip
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.
Diffstat (limited to 'src/com/fourisland')
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/event/ImmutableEvent.java77
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java18
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/CustomMovementType.java5
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/FollowMovementType.java142
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/MovementType.java3
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/RandomMovementType.java40
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/StayStillMovementType.java3
7 files changed, 245 insertions, 43 deletions
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 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package com.fourisland.fourpuzzle.gamestate.mapview.event;
7
8import com.fourisland.fourpuzzle.Direction;
9import com.fourisland.fourpuzzle.Layer;
10import com.fourisland.fourpuzzle.gamestate.mapview.Map;
11import java.awt.Point;
12import java.util.Collections;
13import java.util.List;
14
15/**
16 *
17 * @author hatkirby
18 */
19public final class ImmutableEvent
20{
21 final Event ev;
22 public ImmutableEvent(Event ev)
23 {
24 this.ev = ev;
25 }
26
27 public String getLabel()
28 {
29 return new String(ev.getLabel());
30 }
31
32 public Point getLocation()
33 {
34 return new Point(ev.getLocation());
35 }
36
37 public Direction getDirection()
38 {
39 return ev.getDirection();
40 }
41
42 public boolean isMoving()
43 {
44 return ev.isMoving();
45 }
46
47 public Layer getLayer()
48 {
49 return ev.getLayer();
50 }
51
52 public boolean isOccupyingSpace(int x, int y)
53 {
54 return ev.isOccupyingSpace(x, y);
55 }
56
57 public List<Direction> getLegalMoves()
58 {
59 return Collections.unmodifiableList(ev.getLegalMoves());
60 }
61
62 public int getAnimationStep()
63 {
64 return ev.getAnimationStep();
65 }
66
67 public Map getParentMap()
68 {
69 return ev.getParentMap();
70 }
71
72 public MoveSpeed getMoveSpeed()
73 {
74 return ev.getMoveSpeed();
75 }
76
77}
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 {
99 99
100 public void startMoving() 100 public void startMoving()
101 { 101 {
102 Direction toMove = getPossibleEvent().getMovement().nextMovement(); 102 Direction toMove = getPossibleEvent().getMovement().nextMovement(new ImmutableEvent(this));
103 103
104 if (toMove != null) 104 if (toMove != null)
105 { 105 {
106 startMoving(toMove); 106 if (getPossibleEvent().getGraphic() instanceof MoveableEventGraphic)
107 } 107 {
108 } 108 startMoving(toMove);
109 109 }
110 @Override
111 public boolean startMoving(Direction toMove)
112 {
113 if (!(getPossibleEvent().getGraphic() instanceof MoveableEventGraphic))
114 {
115 return false;
116 } 110 }
117
118 return super.startMoving(toMove);
119 } 111 }
120 112
121 public Direction getDirection() 113 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 @@
6package com.fourisland.fourpuzzle.gamestate.mapview.event.movement; 6package com.fourisland.fourpuzzle.gamestate.mapview.event.movement;
7 7
8import com.fourisland.fourpuzzle.Direction; 8import com.fourisland.fourpuzzle.Direction;
9import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent;
9 10
10/** 11/**
11 * CustomMovementEvent takes an array of Directions and directions the event 12 * CustomMovementEvent takes an array of Directions and directions the event
@@ -24,8 +25,8 @@ public class CustomMovementType implements MovementType {
24 this.moves = moves; 25 this.moves = moves;
25 } 26 }
26 27
27 public Direction nextMovement() 28 public Direction nextMovement(ImmutableEvent ev)
28 { 29 {
29 if (step >= moves.length) 30 if (step >= moves.length)
30 { 31 {
31 step = 0; 32 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 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package com.fourisland.fourpuzzle.gamestate.mapview.event.movement;
7
8import com.fourisland.fourpuzzle.Direction;
9import com.fourisland.fourpuzzle.Game;
10import com.fourisland.fourpuzzle.gamestate.mapview.event.Event;
11import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent;
12import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent;
13import java.awt.Point;
14import java.util.ArrayDeque;
15import java.util.ArrayList;
16import java.util.Collections;
17import java.util.Deque;
18import java.util.List;
19
20/**
21 * FollowMovementType allows an Event to continually walk toward a specified
22 * Event.
23 *
24 * @author hatkirby
25 */
26public class FollowMovementType implements MovementType {
27
28 String name = null;
29 Event event = null;
30 Point lastLoc = new Point();
31 Deque<Direction> moves = new ArrayDeque<Direction>();
32 List<Point> attempts = new ArrayList<Point>();
33
34 public FollowMovementType() {}
35
36 public FollowMovementType(String name)
37 {
38 this.name = name;
39 }
40
41 private boolean search(ImmutableEvent ev)
42 {
43 for (Direction d : Direction.values())
44 {
45 Point loc = d.to(ev.getLocation());
46 if (lastLoc.equals(loc))
47 {
48 return true;
49 }
50 }
51
52 List<Direction> ds = ev.getLegalMoves();
53 List<Direction> tempd = new ArrayList<Direction>();
54
55 if (lastLoc.x < ev.getLocation().x)
56 {
57 tempd.add(Direction.West);
58 } else if (lastLoc.x > ev.getLocation().x)
59 {
60 tempd.add(Direction.East);
61 } else {
62 if (!ds.contains(Direction.North) || !ds.contains(Direction.South))
63 {
64 tempd.add(Direction.West);
65 tempd.add(Direction.East);
66 }
67 }
68
69 if (lastLoc.y < ev.getLocation().y)
70 {
71 tempd.add(Direction.North);
72 } else if (lastLoc.y > ev.getLocation().y)
73 {
74 tempd.add(Direction.South);
75 } else {
76 if (!ds.contains(Direction.West) || !ds.contains(Direction.East))
77 {
78 tempd.add(Direction.North);
79 tempd.add(Direction.South);
80 }
81 }
82
83 tempd.retainAll(ds);
84 Collections.shuffle(tempd);
85
86 for (Direction d : tempd)
87 {
88 Point loc = d.to(ev.getLocation());
89
90 if (attempts.contains(loc))
91 {
92 continue;
93 }
94
95 Event temp = new LayerEvent(loc.x, loc.y);
96 temp.setParentMap(ev.getParentMap());
97 attempts.add(loc);
98
99 if (search(new ImmutableEvent(temp)))
100 {
101 moves.push(d);
102 return true;
103 }
104 }
105
106 return false;
107 }
108
109 public Direction nextMovement(ImmutableEvent ev)
110 {
111 if (event == null)
112 {
113 if (name == null)
114 {
115 event = Game.getHeroEvent();
116 } else {
117 event = ev.getParentMap().getEvent(name);
118 }
119 } else if ((name != null) && !(event.getParentMap().equals(ev.getParentMap())))
120 {
121 event = ev.getParentMap().getEvent(name);
122 }
123
124 if (!event.getLocation().equals(lastLoc))
125 {
126 lastLoc.setLocation(event.getLocation());
127
128 moves.clear();
129 attempts.clear();
130
131 search(ev);
132 }
133
134 if (!moves.isEmpty())
135 {
136 return moves.pop();
137 }
138
139 return null;
140 }
141
142} \ 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 @@
6package com.fourisland.fourpuzzle.gamestate.mapview.event.movement; 6package com.fourisland.fourpuzzle.gamestate.mapview.event.movement;
7 7
8import com.fourisland.fourpuzzle.Direction; 8import com.fourisland.fourpuzzle.Direction;
9import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent;
9 10
10/** 11/**
11 * A MovementType is an object that specifies the type of AI a non-hero event 12 * A MovementType is an object that specifies the type of AI a non-hero event
@@ -17,6 +18,6 @@ import com.fourisland.fourpuzzle.Direction;
17 */ 18 */
18public interface MovementType { 19public interface MovementType {
19 20
20 public Direction nextMovement(); 21 public Direction nextMovement(ImmutableEvent ev);
21 22
22} \ No newline at end of file 23} \ 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 @@
6package com.fourisland.fourpuzzle.gamestate.mapview.event.movement; 6package com.fourisland.fourpuzzle.gamestate.mapview.event.movement;
7 7
8import com.fourisland.fourpuzzle.Direction; 8import com.fourisland.fourpuzzle.Direction;
9import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent;
10import com.fourisland.fourpuzzle.util.Interval;
11import java.util.List;
9import java.util.Random; 12import java.util.Random;
10 13
11/** 14/**
@@ -15,34 +18,19 @@ import java.util.Random;
15 */ 18 */
16public class RandomMovementType implements MovementType { 19public class RandomMovementType implements MovementType {
17 20
18 public Direction nextMovement() 21 Interval in = Interval.createTickInterval(10);
22 public Direction nextMovement(ImmutableEvent ev)
19 { 23 {
20 Random r = new Random(); 24 if (in.isElapsed())
21 int ra = r.nextInt(1000);
22 Direction toMove = null;
23 boolean letsMove = false;
24
25 if (ra < 25)
26 {
27 toMove = Direction.North;
28 letsMove = true;
29 } else if (ra < 50)
30 {
31 toMove = Direction.West;
32 letsMove = true;
33 } else if (ra < 75)
34 {
35 toMove = Direction.South;
36 letsMove = true;
37 } else if (ra < 100)
38 {
39 toMove = Direction.East;
40 letsMove = true;
41 }
42
43 if (letsMove)
44 { 25 {
45 return toMove; 26 List<Direction> moves = ev.getLegalMoves();
27 Random r = new Random();
28 int ra = r.nextInt(moves.size());
29
30 if (ra != moves.size())
31 {
32 return moves.get(ra);
33 }
46 } 34 }
47 35
48 return null; 36 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 @@
6package com.fourisland.fourpuzzle.gamestate.mapview.event.movement; 6package com.fourisland.fourpuzzle.gamestate.mapview.event.movement;
7 7
8import com.fourisland.fourpuzzle.Direction; 8import com.fourisland.fourpuzzle.Direction;
9import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent;
9 10
10/** 11/**
11 * StayStillMovementType keeps the event stationary. 12 * StayStillMovementType keeps the event stationary.
@@ -14,7 +15,7 @@ import com.fourisland.fourpuzzle.Direction;
14 */ 15 */
15public class StayStillMovementType implements MovementType { 16public class StayStillMovementType implements MovementType {
16 17
17 public Direction nextMovement() 18 public Direction nextMovement(ImmutableEvent ev)
18 { 19 {
19 return null; // Do nothing, stay still 20 return null; // Do nothing, stay still
20 } 21 }