1 package simworldobjects;
2
3 import interfaces.*;
4
5 import java.util.*;
6 import java.awt.*;
7 import java.awt.geom.*;
8
9
18 public abstract class BasicSimWorld implements SimWorld
19 {
20 private int ticks;
21 private int ambientLight;
22 private LinkedList objectList;
23 private long width, length, height;
24
25
32 public BasicSimWorld(long x, long y, long z)
33 {
34 width=x;
36 height=y;
37 length=z;
38
39 ticks=0;
41
42 objectList=new LinkedList();
44
45 ambientLight=20;
47 }
48
49
52 public void tick()
53 {
54 updateObjects();
55 ticks++;
56 }
57
58
63 public long getTime()
64 {
65 return ticks;
66 }
67
68
73 public int getBrightness(double x, double y, double z)
74 {
75 double totalBrightness=0;
76
77 for(int i=0;i<objectList.size();i++)
79 {
80 SimObject o=(SimObject)objectList.get(i);
81
82 if(o instanceof SimLight)
83 {
84 SimLight light=(SimLight)o;
85
86 int X=Math.abs((int)(light.getXCoord()-x));
87 int Z=Math.abs((int)(light.getZCoord()-z));
88
89 double distance=Math.sqrt((X*X)+(Z*Z));
91
92 double coeff=(((double)light.getBrightness()-(distance/8))/(double)light.getBrightness());
94
95 double brightness=light.getBrightness()*coeff;
97
98 totalBrightness+=brightness;
100 }
101 }
102
103 if(totalBrightness < ambientLight)
105 {
106 totalBrightness=ambientLight;
107 }
108
109 if(totalBrightness > 100)
111 {
112 totalBrightness=100;
113 }
114
115 Random r=new Random(System.currentTimeMillis());
117 totalBrightness=totalBrightness+(2*r.nextFloat());
118
119 return (int) totalBrightness;
121 }
122
123
132 public boolean hasObstacle(double x, double y, double z)
133 {
134 for (int i=0;i<objectList.size();i++)
136 {
137 SimObject o=(SimObject)objectList.get(i);
138
139 Shape s=createShape(o.getXCoord(),o.getZCoord(),o.getWidth(),o.getLength(),o.getActualBearingXZ());
141
142 if(s.contains(x,z) && !(o instanceof SimSensor))
144 {
145 return true;
147 }
148 }
149 return false;
151 }
152
153
163 private Shape rotateShape(Shape shape, double angle, double X, double Z)
164 {
165 double theta=Math.toRadians(angle);
167
168 AffineTransform atx = AffineTransform.getRotateInstance(theta,X,Z);
170
171 shape = atx.createTransformedShape(shape);
173
174 return shape;
176 }
177
178
189 private Shape createShape(double x, double z, double width, double length, double angle)
190 {
191 double X=(x-(width/2));
193 double Z=(z-(length/2));
194
195 Shape s=new Rectangle2D.Double(X,Z,width,length);
197
198 s=rotateShape(s,angle,x,z);
200
201 return s;
202 }
203
204
212 public boolean colliding(SimObject o, SimObject p)
213 {
214 Shape shapeO=createShape(o.getXCoord(),o.getZCoord(),o.getWidth(),o.getLength(),o.getActualBearingXZ());
216
217 Shape shapeP=createShape(p.getXCoord(),p.getZCoord(),p.getWidth(),p.getLength(),p.getActualBearingXZ());
219
220 Rectangle2D rectP=shapeP.getBounds2D();
222
223 if (shapeO.intersects(rectP))
225 {
226 return true;
228 }
229 else
230 {
231 return false;
233 }
234 }
235
236
241 public void addObject(SimObject s)
242 {
243 objectList.add(s);
245 }
246
247
252 public LinkedList getObjectList()
253 {
254 return objectList;
255 }
256
257
260 public void updateObjects()
261 {
262 boolean collided;
264
265 for (int i=0;i<objectList.size();i++)
266 {
267 SimObject o=(SimObject)objectList.get(i);
269
270 collided=false;
272
273
275 if(!(o instanceof SimSensor))
277 {
278 for (int j=0;j<objectList.size();j++)
280 {
281 SimObject p=(SimObject)objectList.get(j);
283
284 if(!(p instanceof SimSensor))
286 {
287 if(!(o.getXCoord()==p.getXCoord() && o.getZCoord()==p.getZCoord()))
290 {
291 if(colliding(o,p))
292 {
293 collided=true;
295 }
296 }
297 }
298 }
299 }
300
301 if(!collided)
302 {
303
305 if(o.getDesiredVelocity()>0)
306 {
307 moveForward(o);
308 }
309 else if(o.getDesiredVelocity()<0)
310 {
311 moveBackward(o);
312 }
313
314 if(o.getDesiredBearingVelocityXZ()>0)
315 {
316 moveRight(o);
317 }
318 else if(o.getDesiredBearingVelocityXZ()<0)
319 {
320 moveLeft(o);
321 }
322 }
323 else {
325
327 if(o.getActualVelocity()>0 && o.getActualVelocity()!=o.getDesiredVelocity())
328 {
329 moveBackward(o);
330 }
331 else if(o.getActualVelocity()<0 && o.getActualVelocity()!=o.getDesiredVelocity())
332 {
333 moveForward(o);
334 }
335
336 if(o.getActualBearingVelocityXZ()>0 && o.getActualBearingVelocityXZ()!=o.getDesiredBearingVelocityXZ())
337 {
338 moveLeft(o);
339 }
340 else if(o.getActualBearingVelocityXZ()<0 && o.getActualBearingVelocityXZ()!=o.getDesiredBearingVelocityXZ())
341 {
342 moveRight(o);
343 }
344 }
345 }
346 }
347
348
353 private void moveForward(SimObject o)
354 {
355 o.setActualVelocity(1);
356 o.setXCoord(o.getXCoord() + Math.sin(Math.toRadians(o.getActualBearingXZ())));
357 o.setZCoord(o.getZCoord() - Math.cos(Math.toRadians(o.getActualBearingXZ())));
358 }
359
360
365 private void moveBackward(SimObject o)
366 {
367 o.setActualVelocity(-1);
368 o.setXCoord(o.getXCoord() - Math.sin( Math.toRadians(o.getActualBearingXZ())));
369 o.setZCoord(o.getZCoord() + Math.cos( Math.toRadians(o.getActualBearingXZ())));
370 }
371
372
377 private void moveRight(SimObject o)
378 {
379 o.setActualBearingVelocityXZ(1);
380 o.setActualBearingXZ((o.getActualBearingXZ()+1));
381 }
382
383
388 private void moveLeft(SimObject o)
389 {
390 o.setActualBearingVelocityXZ(-1);
391 o.setActualBearingXZ((o.getActualBearingXZ()-1));
392 }
393 }
394