-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathGameSimulator.java
232 lines (189 loc) · 5.85 KB
/
GameSimulator.java
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import processing.core.*;
import processing.*;
import java.util.*;
public class GameSimulator implements Drawable{
private static float FIELD_W = 2.44f;
private static float FIELD_H = 1.82f;
private static final float WALL_TICK = 0.10f;
public Vector<Simulatable> simulatables = new Vector<Simulatable>();
private Vector<Drawable> drawables = new Vector<Drawable>();
private Block[] walls = new Block[4];
public Field field;
public Ball ball;
// Blocks used to define neutral spots and see if something is hitting it
public Block[] neutralSpots = new Block[4];
// This block is not simulated, but exists to define the inner field area
public Block fieldArea;
public Goal goalLeft, goalRight;
public GoalWall[] goalWalls = new GoalWall[4];
public PVector fieldCenter;
GameSimulator(){
reset();
}
public void setFieldSize(float w, float h){
FIELD_W = w;
FIELD_H = h;
reset();
}
public float getFieldWidth(){
return FIELD_W - field.space*2;
}
public float getFieldHeight(){
return FIELD_H - field.space*2;
}
public void reset(){
// Erase everything
simulatables.clear();
drawables.clear();
// Create Field
field = new Field(FIELD_W, FIELD_H);
drawables.add(field);
// Create inner field area
fieldArea = new Block(FIELD_W/2, FIELD_H/2, FIELD_W - field.space*2, FIELD_H - field.space * 2);
// drawables.add(fieldArea);
// Save field center
fieldCenter = new PVector(field.width / 2, field.height / 2);
// Create Ball
ball = new Ball();
ball.position = fieldCenter.get();
simulatables.add(ball);
drawables.add(ball);
//
// Make Walls
walls[0] = new Block(FIELD_W / 2, 0, FIELD_W, WALL_TICK); // TOP
walls[1] = new Block(FIELD_W / 2, FIELD_H, FIELD_W, WALL_TICK); // BOTTOM
walls[2] = new Block(0, FIELD_H / 2, WALL_TICK, FIELD_H + WALL_TICK); // LEFT
walls[3] = new Block(FIELD_W, FIELD_H / 2, WALL_TICK, FIELD_H + WALL_TICK); // RIGHT
for(int i = 0; i < 4; i ++){
simulatables.add(walls[i]);
drawables.add(walls[i]);
}
// Make Neutral Spots
float xZero = FIELD_W/2;
float yZero = FIELD_H/2;
float xDelta = (FIELD_W - field.space * 2 - field.neutral_spot_dist * 2) / 2;
float yDelta = field.y_goal/2;
neutralSpots[0] = new Block(xZero - xDelta, yZero + yDelta, 0.05f, 0.05f);
neutralSpots[1] = new Block(xZero - xDelta, yZero - yDelta, 0.05f, 0.05f);
neutralSpots[2] = new Block(xZero + xDelta, yZero + yDelta, 0.05f, 0.05f);
neutralSpots[3] = new Block(xZero + xDelta, yZero - yDelta, 0.05f, 0.05f);
// for(int i = 0; i < 4; i ++){
// drawables.add(neutralSpots[i]);
// }
// Make Goals
xDelta = (FIELD_W - field.space*2 - field.line_width*2 + field.x_goal) / 2;
goalLeft = new Goal(xZero - xDelta, yZero, field.x_goal, field.y_goal);
goalRight = new Goal(xZero + xDelta, yZero, field.x_goal, field.y_goal);
simulatables.add(goalLeft);
// drawables.add(goalLeft);
simulatables.add(goalRight);
// drawables.add(goalRight);
// Make Goal Walls
float tickness = field.line_width * 2;
yDelta = (field.y_goal + tickness)/2;
xDelta = (FIELD_W - field.space*2 - field.line_width*2 + field.space) / 2;
goalWalls[0] = new GoalWall(xZero - xDelta, yZero - yDelta, field.space, tickness);
goalWalls[1] = new GoalWall(xZero - xDelta, yZero + yDelta, field.space, tickness);
goalWalls[2] = new GoalWall(xZero + xDelta, yZero - yDelta, field.space, tickness);
goalWalls[3] = new GoalWall(xZero + xDelta, yZero + yDelta, field.space, tickness);
for(int i = 0; i < 4; i ++){
// drawables.add(goalWalls[i]);
simulatables.add(goalWalls[i]);
}
}
float lastT = 0;
public float getTime(){
return lastT;
}
public void simulate(float t){
float dt = t - lastT;
lastT = t;
if(dt > 0.5 || dt <= 0f){
return;
}
// Simulate Physics
for(Simulatable s:simulatables){
s.simulate(dt);
// Simulate Collisions
for(Simulatable b:simulatables){
if(b == s || !s.canCollide(b))
continue;
if(s.colliding(b))
s.resolveCollision(b);
}
}
}
/*
Returns a list of NeutralSpots for the Side given
*/
public PVector[] getNeutralSpots(TeamSide side){
if(side == TeamSide.LEFT){
return new PVector[]{
neutralSpots[0].getRealPosition(),
neutralSpots[1].getRealPosition()
};
}else{
return new PVector[]{
neutralSpots[2].getRealPosition(),
neutralSpots[3].getRealPosition()
};
}
}
/*
Return neares neutral spot to this position
*/
public PVector getNearestNeutralSpot(PVector point){
PVector nearest = null;
float nearesDist = 0;
for(int i = 0; i < neutralSpots.length; i++){
PVector thisPos = neutralSpots[i].getRealPosition();
float thisDist = PVector.sub(point, thisPos).mag();
if(nearest == null || thisDist < nearesDist){
nearest = thisPos;
nearesDist = thisDist;
}
}
return nearest;
}
/*
Returns if an Simulatable will hit another simulatable
*/
public boolean isColliding(Simulatable test){
for(Simulatable s:simulatables){
if(test == s) continue;
if(s.colliding(test))
return true;
}
return false;
}
public float closestSimulatableInRay(Robot robot, PVector origin, float direction) {
float dist = Float.POSITIVE_INFINITY;
synchronized(simulatables) {
Iterator i = simulatables.iterator();
while(i.hasNext()){
Simulatable sim = (Simulatable)i.next();
if (sim == robot || sim instanceof Ball || sim instanceof GoalWall)
continue;
dist = Math.min(dist, MathUtil.rayDistance(origin, direction, sim));
}
}
return dist;
}
public void addToSimulation(Robot s){
if(simulatables.contains(s))
return;
simulatables.add(s);
drawables.add(s);
}
public boolean inSimulation(Simulatable s){
return simulatables.contains(s);
}
public void removeFromSimulation(Robot s){
simulatables.remove(s);
drawables.remove(s);
}
public void draw(PApplet canvas, float scale){
for(Drawable d:drawables)
d.draw(canvas, scale);
}
}