-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.pde
194 lines (161 loc) · 5.75 KB
/
Scene.pde
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
public class Scene
{
private CellGrid m_Grid;
private ArrayList<Animal> m_Animals = new ArrayList<Animal>();
private ArrayList<Animal> m_Animals2Destroy = new ArrayList<Animal>();
private float m_ScreenWrappingEpsilon = 1f;
private boolean m_SimStarted = false; // true after init() is called (the sim is started)
private int m_AnimalsInScene = 0;
private int m_MaxAnimalsInScene = 25;
private float m_UpdateTime = 0f;
private float m_DisplayTime = 0f;
public CellGrid getGrid() { return m_Grid; }
public int GetAnimalsInScene() { return m_AnimalsInScene; }
public int GetMaxAnimalsInScene() { return m_MaxAnimalsInScene; }
public float GetUpdateTime() { return m_UpdateTime; }
public float GetDisplayTime() { return m_DisplayTime; }
public Scene(CellGrid grid)
{
m_Grid = grid;
addAnimal(new Prey("Original Prey"), new ZVector(random(0, width), random(0, height)));
addAnimal(new Predator("Original Predator"), new ZVector(random(0, width), random(0, height)));
init();
}
public void init()
{
for (int i = 0; i < m_Animals.size(); i++)
{
m_Animals.get(i).SetGameScene(this);
m_Animals.get(i).setup();
}
m_SimStarted = true;
}
public void update()
{
float start = millis();
for (int i = 0; i < m_Animals.size(); i++)
{
Animal animal = m_Animals.get(i);
animal.SetCellStandingOn(m_Grid.getCellAt((int) animal.GetPosition().x, (int) animal.GetPosition().y));
animal.update();
}
keepAnimalsWithinBounds();
for (int i = 0; i < m_Animals2Destroy.size(); i++)
{
m_Animals.remove(m_Animals2Destroy.get(i));
}
m_Animals2Destroy.clear();
m_UpdateTime = millis() - start;
}
public void display()
{
float start = millis();
for (int i = 0; i < m_Animals.size(); i++)
{
m_Animals.get(i).display();
}
m_DisplayTime = millis() - start;
}
private void keepAnimalsWithinBounds()
{
// Check for each animal if they are within screen dimensions
for (int i = 0; i < m_Animals.size(); i++)
{
Animal animal = m_Animals.get(i);
ZVector center = animal.getCenter();
ZVector halfExtents = animal.getHalfExtents();
// top side
if (center.y - halfExtents.y < 0f)
{
animal.SetPosition(new ZVector(center.x, height - halfExtents.y - m_ScreenWrappingEpsilon));
}
// bottom side
else if (center.y + halfExtents.y > height)
{
animal.SetPosition(new ZVector(center.x, halfExtents.y + m_ScreenWrappingEpsilon));
}
// right side
if (center.x - halfExtents.x < 0f)
{
animal.SetPosition(new ZVector(width - halfExtents.x - m_ScreenWrappingEpsilon, center.y));
}
// left side
else if (center.x + halfExtents.x > width)
{
animal.SetPosition(new ZVector(halfExtents.x + m_ScreenWrappingEpsilon, center.y));
}
}
}
public void DestroyAnimal(Animal animal)
{
m_Animals2Destroy.add(animal); // add animal to destruction queue
m_AnimalsInScene--;
}
// Does a simple point overlap to see if any animals are at (xPos, yPos)
public Animal getAnimalAt(int xPos, int yPos)
{
for (int i = 0; i < m_Animals.size(); i++)
{
if (pointOverlap(xPos, yPos, (IObjectWithBounds) m_Animals.get(i)))
return m_Animals.get(i);
}
return null;
}
// Simple point vs AABB
public boolean pointOverlap(int xPos, int yPos, IObjectWithBounds bounds)
{
ZVector point = new ZVector(xPos, yPos);
ZVector center = bounds.getCenter();
ZVector halfExtents = bounds.getHalfExtents();
if (point.x < center.x - halfExtents.x)
return false;
if (point.x > center.x + halfExtents.x)
return false;
if (point.y < center.y - halfExtents.y)
return false;
if (point.y > center.y + halfExtents.y)
return false;
return true;
}
// AABB vs AABB
public boolean AABBvsAABB(IObjectWithBounds aabb1, IObjectWithBounds aabb2)
{
ZVector rangePos = aabb2.getCenter();
ZVector rangeSize = aabb2.getHalfExtents();
return !( aabb1.getCenter().x > rangePos.x + rangeSize.x ||
aabb1.getCenter().x + aabb1.getHalfExtents().x < rangePos.x ||
aabb1.getCenter().y > rangePos.y + rangeSize.y ||
aabb1.getCenter().y + aabb1.getHalfExtents().y < rangePos.y);
}
public void addAnimal(Animal animal, ZVector pos)
{
if (m_AnimalsInScene >= m_MaxAnimalsInScene)
return;
m_Animals.add(animal);
m_AnimalsInScene++;
animal.SetPosition(pos);
if (m_SimStarted)
{
animal.SetGameScene(this);
animal.setup();
}
}
public <T extends Animal> T getClosestAnimalOfType(Class<T> type, ZVector position)
{
float closestDist = Float.MAX_VALUE;
Animal closestAnimal = null;
for (int i = 0; i < m_Animals.size(); i++)
{
float dist = ZVector.sub(m_Animals.get(i).GetPosition(), position).mag();
if (dist >= closestDist)
continue;
if (!type.isAssignableFrom(m_Animals.get(i).getClass()))
continue;
closestDist = dist;
closestAnimal = m_Animals.get(i);
}
if (closestAnimal != null)
return type.cast(closestAnimal);
return null;
}
}