1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Martin Polak |
---|
24 | * Fabian 'x3n' Landau |
---|
25 | * Co-authors: |
---|
26 | * ... |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | #include "WeaponMode.h" |
---|
31 | |
---|
32 | #include "util/StringUtils.h" |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/XMLPort.h" |
---|
35 | #include "controllers/Controller.h" |
---|
36 | #include "worldentities/pawns/Pawn.h" |
---|
37 | |
---|
38 | #include "Munition.h" |
---|
39 | #include "Weapon.h" |
---|
40 | #include "WeaponPack.h" |
---|
41 | #include "WeaponSystem.h" |
---|
42 | #include "WeaponSlot.h" |
---|
43 | |
---|
44 | #include "sound/WorldSound.h" |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | WeaponMode::WeaponMode(BaseObject* creator) : BaseObject(creator) |
---|
49 | { |
---|
50 | RegisterObject(WeaponMode); |
---|
51 | |
---|
52 | this->weapon_ = 0; |
---|
53 | this->mode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; |
---|
54 | |
---|
55 | this->munition_ = 0; |
---|
56 | this->initialMunition_ = 0; |
---|
57 | this->initialMagazines_ = 0; |
---|
58 | this->munitionPerShot_ = 1; |
---|
59 | |
---|
60 | this->reloadTime_ = 0.25; |
---|
61 | this->bReloading_ = false; |
---|
62 | this->bAutoReload_ = true; |
---|
63 | this->bParallelReload_ = true; |
---|
64 | |
---|
65 | this->reloadTimer_.setTimer(0.0f, false, createExecutor(createFunctor(&WeaponMode::reloaded, this))); |
---|
66 | this->reloadTimer_.stopTimer(); |
---|
67 | |
---|
68 | this->damage_ = 0; |
---|
69 | |
---|
70 | this->muzzleOffset_ = Vector3::ZERO; |
---|
71 | this->muzzlePosition_ = Vector3::ZERO; |
---|
72 | this->muzzleOrientation_ = Quaternion::IDENTITY; |
---|
73 | |
---|
74 | if( GameMode::isMaster() ) |
---|
75 | { |
---|
76 | this->defSndWpnFire_ = new WorldSound(this); |
---|
77 | this->defSndWpnFire_->setLooping(false); |
---|
78 | this->bSoundAttached_ = false; |
---|
79 | } |
---|
80 | else |
---|
81 | this->defSndWpnFire_ = 0; |
---|
82 | } |
---|
83 | |
---|
84 | WeaponMode::~WeaponMode() |
---|
85 | { |
---|
86 | if(this->isInitialized()) |
---|
87 | { |
---|
88 | if( this->defSndWpnFire_ ) |
---|
89 | delete this->defSndWpnFire_; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | void WeaponMode::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
94 | { |
---|
95 | SUPER(WeaponMode, XMLPort, xmlelement, mode); |
---|
96 | |
---|
97 | XMLPortParam(WeaponMode, "mode", setMode, getMode, xmlelement, mode); |
---|
98 | |
---|
99 | XMLPortParam(WeaponMode, "munitiontype", setMunitionName, getMunitionName, xmlelement, mode); |
---|
100 | XMLPortParam(WeaponMode, "initialmunition", setInitialMunition, getInitialMunition, xmlelement, mode); |
---|
101 | XMLPortParam(WeaponMode, "initialmagazines", setInitialMagazines, getInitialMagazines, xmlelement, mode); |
---|
102 | XMLPortParam(WeaponMode, "munitionpershot", setMunitionPerShot, getMunitionPerShot, xmlelement, mode); |
---|
103 | |
---|
104 | XMLPortParam(WeaponMode, "reloadtime", setReloadTime, getReloadTime, xmlelement, mode); |
---|
105 | XMLPortParam(WeaponMode, "autoreload", setAutoReload, getAutoReload, xmlelement, mode).description("If true, the weapon reloads the magazine automatically"); |
---|
106 | XMLPortParam(WeaponMode, "parallelreload", setParallelReload, getParallelReload, xmlelement, mode).description("If true, the weapon reloads in parallel to the magazine reloading"); |
---|
107 | |
---|
108 | XMLPortParam(WeaponMode, "damage", setDamage, getDamage, xmlelement, mode); |
---|
109 | XMLPortParam(WeaponMode, "muzzleoffset", setMuzzleOffset, getMuzzleOffset, xmlelement, mode); |
---|
110 | } |
---|
111 | |
---|
112 | bool WeaponMode::fire(float* reloadTime) |
---|
113 | { |
---|
114 | (*reloadTime) = this->reloadTime_; |
---|
115 | if( !this->bSoundAttached_ && GameMode::isMaster() ) |
---|
116 | { |
---|
117 | assert(this->getWeapon() && this->getWeapon()->getWeaponSlot()); |
---|
118 | this->getWeapon()->getWeaponSlot()->attach(this->defSndWpnFire_); |
---|
119 | this->bSoundAttached_ = true; |
---|
120 | } |
---|
121 | |
---|
122 | if (!this->bReloading_ && this->munition_ && this->munition_->takeMunition(this->munitionPerShot_, this)) |
---|
123 | { |
---|
124 | float reloadtime = this->reloadTime_; |
---|
125 | |
---|
126 | if (this->bAutoReload_ && this->munition_->needReload(this)) |
---|
127 | { |
---|
128 | if (this->munition_->reload(this)) |
---|
129 | { |
---|
130 | if (!this->bParallelReload_) |
---|
131 | reloadtime += this->munition_->getReloadTime(); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | this->bReloading_ = true; |
---|
136 | this->reloadTimer_.setInterval(reloadtime); |
---|
137 | this->reloadTimer_.startTimer(); |
---|
138 | |
---|
139 | if( this->defSndWpnFire_ && !(this->defSndWpnFire_->isPlaying())) |
---|
140 | { |
---|
141 | this->defSndWpnFire_->play(); |
---|
142 | } |
---|
143 | |
---|
144 | this->fire(); |
---|
145 | |
---|
146 | return true; |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | return false; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | bool WeaponMode::reload() |
---|
155 | { |
---|
156 | if (this->munition_ && this->munition_->reload(this)) |
---|
157 | { |
---|
158 | if (!this->bParallelReload_) |
---|
159 | { |
---|
160 | this->bReloading_ = true; |
---|
161 | this->reloadTimer_.setInterval(this->reloadTime_ + this->munition_->getReloadTime()); |
---|
162 | this->reloadTimer_.startTimer(); |
---|
163 | } |
---|
164 | |
---|
165 | return true; |
---|
166 | } |
---|
167 | |
---|
168 | return false; |
---|
169 | } |
---|
170 | |
---|
171 | void WeaponMode::setMunitionType(Identifier* identifier) |
---|
172 | { |
---|
173 | this->munitionname_ = identifier->getName(); |
---|
174 | this->munitiontype_ = identifier; |
---|
175 | this->updateMunition(); |
---|
176 | } |
---|
177 | |
---|
178 | void WeaponMode::setMunitionName(const std::string& munitionname) |
---|
179 | { |
---|
180 | this->munitionname_ = munitionname; |
---|
181 | Identifier* identifier = ClassByString(this->munitionname_); |
---|
182 | if (identifier) |
---|
183 | this->munitiontype_ = identifier; |
---|
184 | else |
---|
185 | COUT(2) << "Warning: No munition class defined in WeaponMode " << this->getName() << std::endl; |
---|
186 | this->updateMunition(); |
---|
187 | } |
---|
188 | |
---|
189 | void WeaponMode::updateMunition() |
---|
190 | { |
---|
191 | if (this->munitiontype_ && this->weapon_ && this->weapon_->getWeaponPack() && this->weapon_->getWeaponPack()->getWeaponSystem()) |
---|
192 | { |
---|
193 | this->munition_ = this->weapon_->getWeaponPack()->getWeaponSystem()->getMunition(&this->munitiontype_); |
---|
194 | |
---|
195 | if (this->munition_) |
---|
196 | { |
---|
197 | // Add the initial magazines |
---|
198 | this->munition_->addMagazines(this->initialMagazines_); |
---|
199 | |
---|
200 | // Maybe we have to reload (if this munition is used the first time or if there weren't any magazines available before) |
---|
201 | if (this->munition_->needReload(this)) |
---|
202 | this->munition_->reload(this, false); |
---|
203 | |
---|
204 | // Add the initial munition |
---|
205 | if (this->initialMunition_ > 0 && this->munition_->getNumMunitionInCurrentMagazine(this) == this->munition_->getMaxMunitionPerMagazine()) |
---|
206 | { |
---|
207 | // The current magazine is still full, so let's just add another magazine to |
---|
208 | // the stack and reduce the current magazine to the given amount of munition |
---|
209 | |
---|
210 | unsigned int initialmunition = this->initialMunition_; |
---|
211 | if (initialmunition > this->munition_->getMaxMunitionPerMagazine()) |
---|
212 | initialmunition = this->munition_->getMaxMunitionPerMagazine(); |
---|
213 | |
---|
214 | this->munition_->takeMunition(this->munition_->getMaxMunitionPerMagazine() - initialmunition, this); |
---|
215 | this->munition_->addMagazines(1); |
---|
216 | } |
---|
217 | else |
---|
218 | { |
---|
219 | // The current magazine isn't full, add the munition directly |
---|
220 | |
---|
221 | this->munition_->addMunition(this->initialMunition_); |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | else |
---|
226 | this->munition_ = 0; |
---|
227 | } |
---|
228 | |
---|
229 | void WeaponMode::reloaded() |
---|
230 | { |
---|
231 | if( this->defSndWpnFire_ && this->defSndWpnFire_->isPlaying()) |
---|
232 | { |
---|
233 | this->defSndWpnFire_->stop(); |
---|
234 | } |
---|
235 | this->bReloading_ = false; |
---|
236 | } |
---|
237 | |
---|
238 | void WeaponMode::computeMuzzleParameters(const Vector3& target) |
---|
239 | { |
---|
240 | if (this->weapon_) |
---|
241 | { |
---|
242 | this->muzzlePosition_ = this->weapon_->getWorldPosition() + this->weapon_->getWorldOrientation() * this->muzzleOffset_; |
---|
243 | |
---|
244 | Vector3 muzzleDirection; |
---|
245 | muzzleDirection = target - this->muzzlePosition_; |
---|
246 | // COUT(0) << "muzzleDirection " << muzzleDirection << endl; |
---|
247 | this->muzzleOrientation_ = (this->weapon_->getWorldOrientation() * WorldEntity::FRONT).getRotationTo(muzzleDirection) * this->weapon_->getWorldOrientation(); |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | this->muzzlePosition_ = this->muzzleOffset_; |
---|
252 | this->muzzleOrientation_ = Quaternion::IDENTITY; |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | Vector3 WeaponMode::getMuzzleDirection() const |
---|
257 | { |
---|
258 | if (this->weapon_) |
---|
259 | return (this->getMuzzleOrientation() * WorldEntity::FRONT); |
---|
260 | else |
---|
261 | return WorldEntity::FRONT; |
---|
262 | } |
---|
263 | |
---|
264 | void WeaponMode::setDefaultSound(const std::string& soundPath) |
---|
265 | { |
---|
266 | if( this->defSndWpnFire_ ) |
---|
267 | this->defSndWpnFire_->setSource(soundPath); |
---|
268 | } |
---|
269 | |
---|
270 | const std::string& WeaponMode::getDefaultSound() |
---|
271 | { |
---|
272 | if( this->defSndWpnFire_ ) |
---|
273 | return this->defSndWpnFire_->getSource(); |
---|
274 | else |
---|
275 | return BLANKSTRING; |
---|
276 | } |
---|
277 | } |
---|