View Javadoc

1   package cz.cuni.amis.pogamut.defcon.communication.worldview;
2   
3   import java.lang.reflect.Field;
4   
5   import com.google.inject.Inject;
6   import com.google.inject.name.Named;
7   
8   import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldChangeEvent;
9   import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
10  import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
11  import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
12  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
13  import cz.cuni.amis.pogamut.base3d.worldview.impl.LockableBatchAwareWorldView;
14  import cz.cuni.amis.pogamut.base3d.worldview.object.IViewable;
15  import cz.cuni.amis.pogamut.defcon.agent.module.sensor.GameInfo;
16  import cz.cuni.amis.pogamut.defcon.communication.messages.infos.DefConViewableObject;
17  import cz.cuni.amis.pogamut.defcon.communication.messages.infos.EventBatchBegin;
18  import cz.cuni.amis.pogamut.defcon.communication.messages.infos.EventBatchEnd;
19  import cz.cuni.amis.pogamut.defcon.communication.translator.DefConMessageProducer;
20  import cz.cuni.amis.utils.exception.PogamutException;
21  
22  /**
23   * Worldview implementation specific for defcon.
24   * 
25   * @author Radek 'Black_Hand' Pibil
26   * 
27   */
28  @AgentScoped
29  public class DefConWorldView extends LockableBatchAwareWorldView {
30  	public static final String WORLDVIEW_DEPENDENCY = "DefConWorldViewDependency";
31  
32  	private Field visibleField = null;
33  
34  	private DefConMessageProducer messageProducer;
35  
36  	private final static double maxX = 180d;
37  	private final static double minX = -180d;
38  	private final static double maxY = 100d;
39  	private final static double minY = -100d;
40  
41  	@Inject
42  	public DefConWorldView(
43  			@Named(WORLDVIEW_DEPENDENCY) ComponentDependencies dependencies,
44  			DefConMessageProducer messageProducer, IComponentBus bus,
45  			IAgentLogger log) {
46  		super(dependencies, bus, log);
47  		this.messageProducer = messageProducer;
48  	}
49  
50  	@Override
51  	protected boolean isBatchBeginEvent(IWorldChangeEvent evt) {
52  		return evt instanceof EventBatchBegin;
53  	}
54  
55  	@Override
56  	protected boolean isBatchEndEvent(IWorldChangeEvent evt) {
57  		return evt instanceof EventBatchEnd;
58  	}
59  
60  	@Override
61  	protected void setDisappearedFlag(IViewable obj) {
62  		if (obj instanceof DefConViewableObject) {
63  			try {
64  				getField((DefConViewableObject) obj).set(obj, false);
65  			} catch (Exception e) {
66  				throw new PogamutException("Can't set 'visible' field: "
67  						+ e.getMessage(), e, this);
68  			}
69  		}
70  	}
71  
72  	private Field getField(DefConViewableObject obj) {
73  		if (visibleField == null) {
74  			try {
75  				visibleField = DefConViewableObject.class
76  						.getDeclaredField("visible");
77  				visibleField.setAccessible(true);
78  			} catch (Exception e) {
79  				throw new PogamutException("DefConViewableObject " + obj
80  						+ " doesn't contain 'visible' field???", e, this);
81  			}
82  		}
83  		return visibleField;
84  	}
85  
86  	/**
87  	 * Updates message producer and reports the event.
88  	 */
89  	public void update() {
90  		if (eventBus.isRunning()) {
91  			lock();
92  			IWorldChangeEvent event;
93  			while ((event = messageProducer.getEvent()) != null) {
94  				notify(event);
95  			}
96  			unlock();
97  		}
98  	}
99  
100 	public float getStartTime() {
101 		return messageProducer.getStartTime();
102 	}
103 
104 	public float getCurrentTime() {
105 		return messageProducer.getCurrentTime();
106 	}
107 
108 	/**
109 	 * Returns game info object used to query defcon.
110 	 * 
111 	 * @return
112 	 */
113 	public GameInfo getGameInfo() {
114 		return messageProducer.getGameInfo();
115 	}
116 
117 	/**
118 	 * Returns minimal X value.
119 	 * 
120 	 * @return
121 	 */
122 	public double getMinX() {
123 		return minX;
124 	}
125 
126 	/**
127 	 * Returns maximal X value.
128 	 * 
129 	 * @return
130 	 */
131 	public double getMaxX() {
132 		return maxX;
133 	}
134 
135 	/**
136 	 * Returns minimal Y value.
137 	 * 
138 	 * @return
139 	 */
140 	public double getMinY() {
141 		return minY;
142 	}
143 
144 	/**
145 	 * Returns maximal Y value.
146 	 * 
147 	 * @return
148 	 */
149 	public double getMaxY() {
150 		return maxY;
151 	}
152 
153 	/**
154 	 * Returns width of the map.
155 	 * 
156 	 * @return
157 	 */
158 	public double getXSize() {
159 		return maxX - minX;
160 	}
161 
162 	/**
163 	 * Returns height of the map.
164 	 * 
165 	 * @return
166 	 */
167 	public double getYSize() {
168 		return maxY - minY;
169 	}
170 
171 	/**
172 	 * Updates the message producer.
173 	 */
174 	public void updateProducer() {
175 		messageProducer.update();
176 	}
177 }