-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMobileAnimatedActor.java
More file actions
157 lines (133 loc) · 4.26 KB
/
MobileAnimatedActor.java
File metadata and controls
157 lines (133 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import processing.core.PImage;
import java.util.LinkedList;
import java.util.List;
import static java.lang.Math.abs;
public abstract class MobileAnimatedActor
extends AnimatedActor
{
private List<Node> path = new LinkedList<>();
public MobileAnimatedActor(String name, Point position, int rate,
int animation_rate, List<PImage> imgs)
{
super(name, position, rate, animation_rate, imgs);
}
protected Point nextPosition(WorldModel world, Point dest_pt)
{
if(this.getPosition().x == -1)
{
return this.getPosition();
}
LinkedList<Node> list = A_star(this.getPosition(), dest_pt, world);
if(list != null && list.size() > 0)
{
return new Point(list.get(1).x, list.get(1).y);
}
return this.getPosition();
}
protected static boolean adjacent(Point p1, Point p2)
{
return (p1.x == p2.x && abs(p1.y - p2.y) == 1) ||
(p1.y == p2.y && abs(p1.x - p2.x) == 1);
}
protected abstract boolean canPassThrough(WorldModel world, Point new_pt);
public LinkedList A_star(Point start1, Point goal1, WorldModel world)
{
Node[][] nodeWorld = nodes_init(world);
Node start = nodeWorld[start1.y][start1.x];
Node goal = nodeWorld[goal1.y][goal1.x];
LinkedList<Node> closedset = new LinkedList<>();
OrderedList<Node> openset = new OrderedList<>();
openset.insert(start, start.get_f());
start.set_g(0);
start.set_f(start.get_g() + manhattan(start, goal));
while (openset.size() > 0)
{
Node current = openset.head().item;
if(current.x == goal.x && current.y == goal.y)
{
this.path = reconstruct_path(current);
return reconstruct_path(current);
}
openset.remove(current);
closedset.add(current);
for(Node neighbor : neighbor_nodes(current, world, goal, nodeWorld))
{
if(closedset.contains(neighbor))
{
continue;
}
int tentative_gscore = current.get_g() + 1;
if ( (!openset.contains(neighbor)) || tentative_gscore < neighbor.get_g())
{
neighbor.set_came_from(current);
neighbor.set_g(tentative_gscore);
neighbor.set_f(neighbor.get_g() + manhattan(neighbor, goal));
if (!openset.contains(neighbor))
{
openset.insert(neighbor, neighbor.get_f());
}
}
}
}
return null;
}
public static LinkedList reconstruct_path(Node current)
{
LinkedList<Node> path = new LinkedList<>();
while(current.get_came_from()!= null)
{
current = current.get_came_from();
path.add(0, current);
}
return path;
}
public static Node[][] nodes_init(WorldModel world)
{
Node[][] nodeWorld = new Node[world.getNumRows()][world.getNumCols()];
for(int i = 0; i < world.getNumCols(); i++)
{
for(int j = 0; j < world.getNumRows(); j++)
{
Node node = new Node(i, j);
nodeWorld[j][i] = node;
}
}
return nodeWorld;
}
public static LinkedList<Node> get_nodes(WorldModel world, Node[][] nodeWorld)
{
LinkedList<Node> returnlist = new LinkedList<>();
for(int i = 0; i < world.getNumRows(); i++)
{
for(int j = 0; j < world.getNumCols(); j++)
{
Node node = nodeWorld[i][j];
if (!(world.isOccupied(new Point(j, i))))
{
returnlist.add(node);
}
}
}
return returnlist;
}
public static int manhattan(Node start, Node goal)
{
return ((goal.y - start.y) + (goal.x - start.x));
}
public static LinkedList<Node> neighbor_nodes(Node current, WorldModel world, Node goal, Node[][] nodeWorld)
{
LinkedList<Node> returnlist= new LinkedList<>();
for (Node node : get_nodes(world, nodeWorld))
{
if(adjacent(new Point(node.x, node.y), new Point(current.x, current.y)))
{
returnlist.add(node);
}
}
if(adjacent(new Point(goal.x, goal.y), new Point(current.x, current.y)))
{
returnlist.add(goal);
}
return returnlist;
}
}