diff options
Diffstat (limited to 'src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/FollowMovementType.java')
-rw-r--r-- | src/com/fourisland/fourpuzzle/gamestate/mapview/event/movement/FollowMovementType.java | 142 |
1 files changed, 142 insertions, 0 deletions
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 | |||
6 | package com.fourisland.fourpuzzle.gamestate.mapview.event.movement; | ||
7 | |||
8 | import com.fourisland.fourpuzzle.Direction; | ||
9 | import com.fourisland.fourpuzzle.Game; | ||
10 | import com.fourisland.fourpuzzle.gamestate.mapview.event.Event; | ||
11 | import com.fourisland.fourpuzzle.gamestate.mapview.event.ImmutableEvent; | ||
12 | import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent; | ||
13 | import java.awt.Point; | ||
14 | import java.util.ArrayDeque; | ||
15 | import java.util.ArrayList; | ||
16 | import java.util.Collections; | ||
17 | import java.util.Deque; | ||
18 | import java.util.List; | ||
19 | |||
20 | /** | ||
21 | * FollowMovementType allows an Event to continually walk toward a specified | ||
22 | * Event. | ||
23 | * | ||
24 | * @author hatkirby | ||
25 | */ | ||
26 | public 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 | ||