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          private boolean currentValue;
15          private boolean previousValue;
16      
17      /**
18      * Sets up a light sensor on the given SimRCX, and positions it at the
19      * the appropriate offset from the robot's position.
20      *
21      * @param r the SimRCX owner of this sensor
22      * @param xOffset the offest from the SimRCX's x position
23      * @param zOffset the offest from the SimRCX's z position
24      */
25      public SimTouchSensor(SimRCX r, double xOffset, double zOffset, char loc)
26      {
27          robot=r;
28          initSimSensor(r,xOffset,zOffset,10.0,10.0,10.0,"sensorTouch",loc);
29          this.xOffset=xOffset;
30          this.zOffset=zOffset;
31                  this.currentValue = false;
32                  this.previousValue = false;
33      }
34      
35      /**
36      * Returns the current value of this sensor, a 1 if it is touching something
37      * or 0 if it is not.
38      *
39      * @return 1 or 0 accordingly
40      */
41      public int getValue()
42      {
43          SimWorld world=robot.getWorld();
44          
45          if (world.hasObstacle(this.getXCoord(),this.getYCoord(),this.getZCoord()))
46          {
47              return 1;
48          }
49          else
50          {
51              return 0;
52          }
53      }
54          
55          /**
56      * Returns the current boolean value of this sensor, a 'true' if it is touching something
57      * or 'false' if it is not.
58      *
59      * @return 'true' or 'false' accordingly
60      */
61          public boolean getBooleanValue(){
62              BasicSimWorld world= (BasicSimWorld)robot.getWorld();
63             
64              return world.hasObstacle(this.getXCoord(),this.getYCoord(),this.getZCoord());
65             
66          }
67      
68      public double getXOffset()
69      {
70          return xOffset;
71      }
72      
73      public double getZOffset()
74      {
75          return zOffset;
76      }
77          
78          public boolean getPreviousValue(){
79              return this.previousValue;
80          }
81          
82          public boolean getCurrentValue(){
83              BasicSimWorld world= (BasicSimWorld)robot.getWorld();
84              previousValue = currentValue;
85              currentValue = world.hasObstacle(this.getXCoord(),this.getYCoord(),this.getZCoord());
86              return this.currentValue;
87          }
88  }
89