1   package simworldobjects;
2   
3   import interfaces.*;
4   import main.*;
5   
6   /**
7   * Simulates a light sensor
8   *
9   * @author Graham Ritchie modified by Simon Zienkiewicz
10  */
11  public class SimLightSensor extends SimSensor
12  {
13      private SimRCX robot;
14      private double xOffset, zOffset;
15          private int currentValue;
16          private int previousValue;
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           * @param loc the location of the sensor
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 SimLightSensor(SimRCX r, double xOffset, double zOffset, char loc)
26      {
27          robot=r;
28          initSimSensor(r,xOffset,zOffset,4.0,4.0,4.0,"sensorLight", loc);
29          this.xOffset=xOffset;
30          this.zOffset=zOffset;
31                  currentValue=0;
32                  previousValue=0;
33      }
34      
35      /**
36      * Returns the current brightness of the world at this sensor's position
37      * modified by: Simon Zienkiewicz
38      * @return the light level as an int
39      */
40      public int getValue()
41      {
42          BasicSimWorld world= (BasicSimWorld)robot.getWorld();
43          return LightSensorColorLibrary.getValue(world.getColorUnderLightSensor(this));
44      }
45      
46      /**
47      * Returns the offset from the owning SimRCX's x coordinate
48      *
49      * @return the x offset
50      */
51      public double getXOffset()
52      {
53          return xOffset;
54      }
55      
56      /**
57      * Returns the offset from the owning SimRCX's y coordinate
58      *
59      * @return the y offset
60      */
61      public double getZOffset()
62      {
63          return zOffset;
64      }
65          
66          /** Returns the previous value read by the sensor.
67           * @return the previous value of the sensor
68           */        
69          public int getPreviousValue(){
70              return this.previousValue;
71          }
72          
73          /** Returns the present value of the sensor.
74           * @return the present value of the sensor
75           */        
76          public int getCurrentValue(){
77              BasicSimWorld world= (BasicSimWorld)robot.getWorld();
78              previousValue = currentValue;
79              currentValue = LightSensorColorLibrary.getValue(world.getColorUnderLightSensor(this));
80              return this.currentValue;
81          }
82  }
83