1   package simworldobjects;
2   
3   import interfaces.*;
4   
5   /**
6   * Simulates a light sensor
7   *
8   * @author Graham Ritchie
9   */
10  public class SimTouchSensor extends SimSensor
11  {
12      private SimRCX robot;
13      private double xOffset, zOffset;
14      
15      /**
16      * Sets up a light sensor on the given SimRCX, and positions it at the
17      * the appropriate offset from the robot's position.
18      *
19      * @param r the SimRCX owner of this sensor
20      * @param xOffset the offest from the SimRCX's x position
21      * @param zOffset the offest from the SimRCX's z position
22      */
23      public SimTouchSensor(SimRCX r, double xOffset, double zOffset)
24      {
25          robot=r;
26          initSimSensor(r,xOffset,zOffset,10.0,10.0,10.0,"sensor");
27          this.xOffset=xOffset;
28          this.zOffset=zOffset;
29      }
30      
31      /**
32      * Returns the current value of this sensor, a 1 if it is touching something
33      * or 0 if it is not.
34      *
35      * @return 1 or 0 accordingly
36      */
37      public int getValue()
38      {
39          SimWorld world=robot.getWorld();
40          
41          if (world.hasObstacle(this.getXCoord(),this.getYCoord(),this.getZCoord()))
42          {
43              return 1;
44          }
45          else
46          {
47              return 0;
48          }
49      }
50      
51      public double getXOffset()
52      {
53          return xOffset;
54      }
55      
56      public double getZOffset()
57      {
58          return zOffset;
59      }
60  }
61