1 package nl.tudelft.pogamut.unreal.agent.module.shooting;
2
3 import nl.tudelft.pogamut.unreal.agent.module.shooting.util.FocusProvider;
4 import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
5 import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
6 import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
7 import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
8 import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
9 import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Weaponry;
10 import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.AgentInfo;
11 import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.WeaponPref;
12 import cz.cuni.amis.pogamut.ut2004.bot.command.ImprovedShooting;
13 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
14 import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
15 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.EndMessage;
16
17
18
19
20
21
22
23
24 @SuppressWarnings("rawtypes")
25 public abstract class AbstractWeaponShooting extends SensorModule<UT2004Bot> implements WeaponShooting {
26
27 protected static final double FACING_ANGLE = 30.0;
28
29
30
31 protected static final Location BELOW_PLAYER_OFFSET = new Location(0, 0, 44);
32
33
34
35 protected static final Location ABOVE_PLAYER_OFFSET = new Location(0, 0, 88);
36
37
38
39 protected ILocated target = null;
40
41
42
43 protected WeaponPref weaponPref;
44
45
46
47 protected ImprovedShooting shoot;
48
49
50
51
52 protected Weaponry weaponry;
53
54
55
56
57 protected AgentInfo info;
58
59
60
61 protected boolean active = false;
62
63
64
65
66 protected FocusProvider focus = new FocusProvider();
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public AbstractWeaponShooting(UT2004Bot<?, ?, ?> agent, AgentInfo info, ImprovedShooting shoot, Weaponry weaponry) {
81 super(agent);
82 this.shoot = shoot;
83 this.info = info;
84 this.weaponry = weaponry;
85 this.endMessageListener = new EndMessageListener(worldView);
86 }
87
88
89
90
91
92
93
94
95
96 @Override
97 public void shoot(WeaponPref weaponPref, ILocated target) {
98 if (weaponPref == null || !weaponPref.getWeapon().equals(getWeaponType())) {
99 throw new IllegalArgumentException(
100 "Weapon pref must be correct for the weapon associated with this shooting");
101 }
102
103 this.active = true;
104 this.target = target;
105 this.focus.setFocus(target);
106 this.weaponPref = weaponPref;
107 }
108
109
110
111
112 protected abstract WeaponPref getDefaultWeaponPref();
113
114
115
116
117 public void stopShoot() {
118 this.active = false;
119 this.target = null;
120 this.focus.clearFocus();
121 shoot.stopShooting();
122 }
123
124
125
126
127
128
129
130 protected void setFocus(ILocated focus) {
131 this.focus.setFocus(focus);
132 }
133
134
135
136
137 public ILocated getFocus() {
138 return focus;
139 }
140
141
142
143
144
145 public boolean hasTarget() {
146 return target != null;
147 }
148
149
150
151
152
153
154 public ItemType getWeaponType() {
155 return getDefaultWeaponPref().getWeapon();
156 }
157
158
159
160
161 @Override
162 public boolean isActive() {
163 return active;
164 }
165
166
167
168
169 protected boolean isWeaponReady() {
170
171 if (weaponry.getCurrentWeapon() == null || !weaponry.getCurrentWeapon().getType().equals(getWeaponType())) {
172 weaponry.changeWeapon(getWeaponType());
173 return false;
174 }
175
176 return true;
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 protected abstract void shoot();
193
194
195
196
197 private class EndMessageListener implements IWorldEventListener<EndMessage> {
198 @Override
199 public void notify(EndMessage event) {
200 if (isActive())
201 shoot();
202 }
203
204
205
206
207
208
209
210 public EndMessageListener(IWorldView worldView) {
211 worldView.addEventListener(EndMessage.class, this);
212 }
213 }
214
215
216 protected EndMessageListener endMessageListener;
217
218 }