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 private Color worldColor=Color.white;
25
26
33 public BasicSimWorld(long x, long y, long z)
34 {
35 width=x;
37 height=y;
38 length=z;
39
40 ticks=0;
42
43 objectList=new LinkedList();
45
46 ambientLight=20;
48
49 buildWallBoundaries();
51 }
52
61 public BasicSimWorld(long x, long y, long z, Color c)
62 {
63 width=x;
65 height=y;
66 length=z;
67
68 worldColor=c;
70
71 ticks=0;
73
74 objectList=new LinkedList();
76
77 ambientLight=20;
79
80 }
81
82
85 private void buildWallBoundaries(){
86 SimWall wall1=new SimWall(this.length/2,0.0,6.0,0.0,this.length-2,10.0);
89 addObject(wall1);
90
91 SimWall wall2=new SimWall(6.0,0.0,this.width/2,0.0,10.0,this.length-2);
93 addObject(wall2);
95
96
97 SimWall wall3=new SimWall(this.length-6.0,0.0,this.width/2,0.0,10.0,this.length-2);
99 addObject(wall3);
101
102 SimWall wall4=new SimWall(this.length/2,0.0,this.width-6.0,0.0,this.length-2,10.0);
104 addObject(wall4);
105
106 }
107
108
111 public void tick()
112 {
113 updateObjects();
114 ticks++;
115 }
116
117
122 public long getTime()
123 {
124 return ticks;
125 }
126
127
132 public int getBrightness(double x, double y, double z)
133 {
134 double totalBrightness=0;
135
136 for(int i=0;i<objectList.size();i++)
138 {
139 SimObject o=(SimObject)objectList.get(i);
140
141 if(o instanceof SimLight)
142 {
143 SimLight light=(SimLight)o;
144
145 int X=Math.abs((int)(light.getXCoord()-x));
146 int Z=Math.abs((int)(light.getZCoord()-z));
147
148 double distance=Math.sqrt((X*X)+(Z*Z));
150
151 double coeff=(((double)light.getBrightness()-(distance/8))/(double)light.getBrightness());
153
154 double brightness=light.getBrightness()*coeff;
156
157 totalBrightness+=brightness;
159 }
160 }
161
162 if(totalBrightness < ambientLight)
164 {
165 totalBrightness=ambientLight;
166 }
167
168 if(totalBrightness > 100)
170 {
171 totalBrightness=100;
172 }
173
174 Random r=new Random(System.currentTimeMillis());
176 totalBrightness=totalBrightness+(2*r.nextFloat());
177
178 return (int) totalBrightness;
180 }
181
182
191 public boolean hasObstacle(double x, double y, double z)
192 {
193 for (int i=0;i<objectList.size();i++)
195 {
196 SimObject o=(SimObject)objectList.get(i);
197
198 Shape s=createShape(o.getXCoord(),o.getZCoord(),o.getWidth(),o.getLength(),o.getActualBearingXZ());
200
201 if(s.contains(x,z) && !(o instanceof SimSensor) && !(o instanceof SimGround) && !(o instanceof SimRoad))
203 {
204 return true;
206 }
207 }
208 return false;
210 }
211
212
222 private Shape rotateShape(Shape shape, double angle, double X, double Z)
223 {
224 double theta=Math.toRadians(angle);
226
227 AffineTransform atx = AffineTransform.getRotateInstance(theta,X,Z);
229
230 shape = atx.createTransformedShape(shape);
232
233 return shape;
235 }
236
237
248 private Shape createShape(double x, double z, double width, double length, double angle)
249 {
250 double X=(x-(width/2));
252 double Z=(z-(length/2));
253
254 Shape s=new Rectangle2D.Double(X,Z,width,length);
256
257 s=rotateShape(s,angle,x,z);
259
260 return s;
261 }
262
263
271 public boolean colliding(SimObject o, SimObject p)
272 {
273 Shape shapeO=createShape(o.getXCoord(),o.getZCoord(),o.getWidth(),o.getLength(),o.getActualBearingXZ());
275
276 Shape shapeP=createShape(p.getXCoord(),p.getZCoord(),p.getWidth(),p.getLength(),p.getActualBearingXZ());
278
279 Rectangle2D rectP=shapeP.getBounds2D();
281
282 if (shapeO.intersects(rectP))
284 {
285 return true;
287 }
288 else
289 {
290 return false;
292 }
293 }
294
295
300 public void addObject(SimObject s)
301 {
302 objectList.add(s);
304 }
305
306
312 public void addObjecttoFront(SimObject s)
313 {
314 int b=this.objectList.size();
315 for(int i=0;i<this.objectList.size();i++){
316 SimObject sim = (SimObject)objectList.get(i);
317 if(sim instanceof SimSensor || sim instanceof SimRCX){
318 b=i;
319 break;
320 }
321 }
322 this.objectList.add(b,s);
323 }
324
325
330 public LinkedList getObjectList()
331 {
332 return objectList;
333 }
334
335
338 public void updateObjects()
339 {
340 boolean collided;
342
343 for (int i=0;i<objectList.size();i++)
344 {
345 SimObject o=(SimObject)objectList.get(i);
347
348 collided=false;
350
351
353 if(o instanceof SimRCX || o instanceof SimLightSensor)
355 {
356 for (int j=0;j<objectList.size();j++)
358 {
359 SimObject p=(SimObject)objectList.get(j);
361
362 if(p instanceof SimWall)
364 {
365 if(!(o.getXCoord()==p.getXCoord() && o.getZCoord()==p.getZCoord()))
368 {
369 if(colliding(o,p))
370 {
371 collided=true;
373 }
374 }
375 }
376 }
377 }
378
379 if(!collided)
380 {
381
383 if(o.getDesiredVelocity()>0)
384 {
385 moveForward(o);
386 }
387 else if(o.getDesiredVelocity()<0)
388 {
389 moveBackward(o);
390 }
391
392 if(o.getDesiredBearingVelocityXZ()>0)
393 {
394 moveRight(o);
395 }
396 else if(o.getDesiredBearingVelocityXZ()<0)
397 {
398 moveLeft(o);
399 }
400 }
401 else {
403 if(o instanceof SimRCX){
404 SimRCX robot = (SimRCX)o;
405 robot.stopMoving();
406
407 }
408 else if(o instanceof SimLightSensor){
409 SimRCX robot = (SimRCX)((SimSensor)o).getOwner();
410 robot.stopMoving();
411 }
412 }
413 if(o instanceof SimSensor){
415 SimSensor sensor = (SimSensor)o;
416 int index=0;
417
418 switch(sensor.getPosition()){
419 case 'L': index=1; break;
420 case 'F': index=2; break;
421 case 'R': index=3; break;
422 }
423
424 if(o instanceof SimTouchSensor){
426 SimTouchSensor touch = (SimTouchSensor)o;
427 if(touch.getCurrentValue() != touch.getPreviousValue()){
428 ((SimRCX)touch.getOwner()).touchEvent(index);
429 }
430 }
431
432 else if(o instanceof SimLightSensor){
434
435 SimLightSensor light = (SimLightSensor)o;
436
437 if(light.getCurrentValue() != light.getPreviousValue()){
438 ((SimRCX)light.getOwner()).lightEvent(index);
439 }
440 }
441 }
442
443 }
444
445 }
446
447
453 private void moveForward(SimObject o)
454 {
455 if(o.getXCoord() >= 0 && o.getZCoord() >=0 && o.getXCoord() <= this.getWorldDimensions()[0] && o.getZCoord() <= this.getWorldDimensions()[2]){
456 o.setActualVelocity(o.getDesiredVelocity());
457 o.setXCoord(o.getXCoord()+ o.getDesiredVelocity()*(Math.sin(Math.toRadians(o.getActualBearingXZ()))));
458 o.setZCoord(o.getZCoord() - o.getDesiredVelocity()*(Math.cos(Math.toRadians(o.getActualBearingXZ()))));
459 }
460 }
461
462
467 private void moveBackward(SimObject o)
468 {
469 if(o.getXCoord() >= 0 && o.getZCoord() >=0 && o.getXCoord() <= this.getWorldDimensions()[0] && o.getZCoord() <= this.getWorldDimensions()[2]){
470 o.setActualVelocity(o.getDesiredVelocity());
471 o.setXCoord(o.getXCoord() + o.getDesiredVelocity()*(Math.sin(Math.toRadians(o.getActualBearingXZ()))));
472 o.setZCoord(o.getZCoord() - o.getDesiredVelocity()*(Math.cos(Math.toRadians(o.getActualBearingXZ()))));
473 }
474 }
475
476
481 private void moveRight(SimObject o)
482 {
483 o.setActualBearingVelocityXZ(o.getDesiredBearingVelocityXY());
484 o.setActualBearingXZ((o.getActualBearingXZ()+o.getDesiredBearingVelocityXZ()));
485
486 if(o instanceof SimRCX){
487 ((SimRCX)o).pivotRotation();
489 }
490 }
491
492
497 private void moveLeft(SimObject o)
498 {
499 if(o instanceof SimWall)
500 o.setActualBearingVelocityXZ(o.getDesiredBearingVelocityXY());
501 o.setActualBearingXZ((o.getActualBearingXZ()+o.getDesiredBearingVelocityXZ()));
502
503 if(o instanceof SimRCX){
504 ((SimRCX)o).pivotRotation();
506 }
507 }
508
509
514 public Color getColorUnderLightSensor(SimLightSensor lightSensor){
515
517 int b=9999;
518 for(int i=0;i<objectList.size();i++)
519 {
520 SimObject o=(SimObject)objectList.get(i);
521
522 if(o instanceof SimGround)
523 {
524 SimGround ground=(SimGround)o;
525 if(this.colliding(lightSensor, ground))b=i;
527 }
528 if(o instanceof SimRoad)
529 {
530 SimRoad road=(SimRoad)o;
531 if(this.colliding(lightSensor, road))b=i;
533 }
534 }
535 if(b!=9999) {
537 SimObject o=(SimObject)objectList.get(b);
538 if(o instanceof SimGround) return ((SimGround)(objectList.get(b))).getColor();
539 else return ((SimRoad)(objectList.get(b))).getColor();
540 }
541 else return worldColor;
542 }
543
544
550 public Color getWorldColor(){
551 return worldColor;
552 }
553
554
560 public void setWorldColor(Color color){
561 worldColor = color;
562 }
563
564
570 public int[] getWorldDimensions(){
571 int[] dimensions = {(int)this.width,(int)this.height,(int)this.length};
572 return dimensions;
573 }
574
575 public void updateObjectList(LinkedList newList){
576 objectList=newList;
577 }
578 }