1 package simworldobjects;
2
3 import interfaces.*;
4
5 import java.awt.*;
6 import java.lang.*;
7 import java.awt.event.*;
8 import javax.swing.*;
9 import java.io.*;
10 import java.util.*;
11 import java.awt.geom.*;
12
13
18 public class SimRCX extends BasicSimObject implements AbstractRobot
19 {
20 public static final int STATE_STOPPED=1;
21 public static final int STATE_FORWARD=2;
22 public static final int STATE_BACKWARD=3;
23 public static final int STATE_RIGHT=4;
24 public static final int STATE_LEFT=5;
25
26 private int state;
27 private SimWorld world;
28
29 private SimSensor S1;
30 private SimSensor S2;
31 private SimSensor S3;
32
33 private Controller controller;
34
35
41 public SimRCX(SimWorld w, Controller c)
42 {
43 super(30.0,40.0,60.0,"robot",600.0,0.0,200.0,0.0,160.0);
45
46 this.world=w;
48
49 this.controller=c;
51
52 setupRobot();
53 }
54
55
58 public SimRCX(SimWorld w, Controller c, double height, double width, double length, String type, double x, double y, double z, double bearingXY, double bearingXZ)
59 {
60 super(height,width,length,type,x,y,z,bearingXY,bearingXZ);
62
63 world=w;
65
66 this.controller=c;
68
69 setupRobot();
70 }
71
72
75 private void setupRobot()
76 {
77 this.state=SimRCX.STATE_STOPPED;
79
80 int[] sensors=controller.getSensors();
82
83 if(sensors[0]==Controller.SENSOR_TYPE_LIGHT)
84 {
85 S1=new SimLightSensor(this,-this.getWidth(),-this.getLength()/4);
86 }
87 else
88 {
89 S1=new SimTouchSensor(this,-this.getWidth(),-this.getLength()/4);
90 }
91
92 if(sensors[1]==Controller.SENSOR_TYPE_LIGHT)
93 {
94 S2=new SimLightSensor(this,0.0,-(this.getLength()/2)-25);
95 }
96 else
97 {
98 S2=new SimTouchSensor(this,0.0,-(this.getLength()/2)-25);
99 }
100
101 if(sensors[2]==Controller.SENSOR_TYPE_LIGHT)
102 {
103 S3=new SimLightSensor(this,this.getWidth(),-this.getLength()/4);
104 }
105 else
106 {
107 S3=new SimTouchSensor(this,this.getWidth(),-this.getLength()/4);
108 }
109
110 world.addObject(S1);
112 world.addObject(S2);
113 world.addObject(S3);
114 }
115
116 public SimWorld getWorld()
117 {
118 return this.world;
119 }
120
121
124
125
129 public void forward()
130 {
131 stopMoving();
132 this.state=SimRCX.STATE_FORWARD;
133 this.setDesiredVelocity(1);
134 }
135
136
141 public void forward(int time)
142 {
143 stopMoving();
144 this.state=SimRCX.STATE_FORWARD;
145 this.setDesiredVelocity(1);
146 try{Thread.sleep(time);}catch(Exception e){}
147 stopMoving();
148 }
149
150
154 public void backward()
155 {
156 stopMoving();
157 this.state=SimRCX.STATE_BACKWARD;
158 this.setDesiredVelocity(-1);
159 }
160
161
166 public void backward(int time)
167 {
168 stopMoving();
169 this.state=SimRCX.STATE_BACKWARD;
170 this.setDesiredVelocity(-1);
171 try{Thread.sleep(time);}catch(Exception e){}
172 stopMoving();
173 }
174
175
179 public void right()
180 {
181 stopMoving();
182 this.state=SimRCX.STATE_RIGHT;
183 this.setDesiredBearingVelocityXZ(1);
184 }
185
186
191 public void right(int time)
192 {
193 stopMoving();
194 this.state=SimRCX.STATE_RIGHT;
195 this.setDesiredBearingVelocityXZ(1);
196 try{Thread.sleep(time);}catch(Exception e){}
197 this.state=SimRCX.STATE_STOPPED;
198 this.setDesiredBearingVelocityXZ(0);
199 }
200
201
205 public void left()
206 {
207 stopMoving();
208 this.state=SimRCX.STATE_LEFT;
209 this.setDesiredBearingVelocityXZ(-1);
210 }
211
212
217 public void left(int time)
218 {
219 stopMoving();
220 this.state=SimRCX.STATE_LEFT;
221 this.setDesiredBearingVelocityXZ(-1);
222 try{Thread.sleep(time);}catch(Exception e){}
223 stopMoving();
224 }
225
226
229 public void stopMoving()
230 {
231 this.state=SimRCX.STATE_STOPPED;
232 this.setDesiredVelocity(0);
233 this.setDesiredBearingVelocityXZ(0);
234 }
235
236
239 public void beep()
240 {
241 Toolkit.getDefaultToolkit().beep();
242 }
243
244
249 public int getSensor1()
250 {
251 return S1.getValue();
252 }
253
254
259 public int getSensor2()
260 {
261 return S2.getValue();
262 }
263
264
269 public int getSensor3()
270 {
271 return S3.getValue();
272 }
273 }
274