View Javadoc

1   package cz.cuni.amis.pogamut.emohawk.agent.module.sensomotoric;
2   
3   import cz.cuni.amis.pogamut.base.agent.module.SensomotoricModule;
4   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
5   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectListener;
6   import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
7   import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
8   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
9   import java.util.Arrays;
10  import java.util.Collection;
11  import java.util.HashSet;
12  
13  /**
14   * Annotates map EmohawkVille.
15   * @author knight
16   */
17  public class Places extends SensomotoricModule<UT2004Bot>{
18  
19      /** for fast retrieval - list of all places */
20      HashSet<Place> placesSet = new HashSet();
21      /** self object */
22      Self self;
23  
24      public Place getCurrentPlace() {
25          if (self == null) return null;
26          Location location = self.getLocation();
27          for (Place place : placesSet) {
28              if (place.placeReached(location))
29                  return place;
30          }
31          return null;
32      }
33  
34      public Place getCurrentPlace(Location location) {
35          for (Place place : placesSet) {
36              if (place.placeReached(location))
37                  return place;
38          }
39          return null;
40      }
41  
42      public Collection<Place> getPlaces() {
43          return placesSet;
44      }
45      
46      /**
47       * Listener for Self object - stores Self object into our link.
48       */
49      IWorldObjectListener<Self> storeSelfListener = new IWorldObjectListener<Self>() {
50          @Override
51          public void notify(IWorldObjectEvent<Self> event) {
52              if (self == null) {
53                  self = event.getObject();
54                  worldView.removeObjectListener(Self.class, this);
55                  storeSelfListener = null;
56              }
57          }
58      };
59      
60      /**
61       * Default constructor.
62       *
63       * @param agent
64       */
65      public Places(UT2004Bot agent) {
66          super(agent);
67          placesSet.addAll(Arrays.asList(Place.values()));
68          worldView.addObjectListener(Self.class, storeSelfListener);
69      }
70  }