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 "OrxonoxStableHeaders.h" |
---|
31 | #include "WeaponMode.h" |
---|
32 | |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/XMLPort.h" |
---|
35 | |
---|
36 | #include "Munition.h" |
---|
37 | #include "Weapon.h" |
---|
38 | #include "WeaponPack.h" |
---|
39 | #include "WeaponSystem.h" |
---|
40 | |
---|
41 | namespace orxonox |
---|
42 | { |
---|
43 | WeaponMode::WeaponMode(BaseObject* creator) : BaseObject(creator) |
---|
44 | { |
---|
45 | RegisterObject(WeaponMode); |
---|
46 | |
---|
47 | this->weapon_ = 0; |
---|
48 | this->mode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; |
---|
49 | |
---|
50 | this->munition_ = 0; |
---|
51 | this->initialMunition_ = 0; |
---|
52 | this->initialMagazines_ = 0; |
---|
53 | this->munitionPerShot_ = 1; |
---|
54 | |
---|
55 | this->reloadTime_ = 0.25; |
---|
56 | this->bReloading_ = false; |
---|
57 | this->bAutoReload_ = true; |
---|
58 | this->bParallelReload_ = true; |
---|
59 | |
---|
60 | this->reloadTimer_.setTimer(0.0f, false, this, createExecutor(createFunctor(&WeaponMode::reloaded))); |
---|
61 | this->reloadTimer_.stopTimer(); |
---|
62 | |
---|
63 | this->damage_ = 0; |
---|
64 | this->muzzleOffset_ = Vector3::ZERO; |
---|
65 | } |
---|
66 | |
---|
67 | WeaponMode::~WeaponMode() |
---|
68 | { |
---|
69 | } |
---|
70 | |
---|
71 | void WeaponMode::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
72 | { |
---|
73 | SUPER(WeaponMode, XMLPort, xmlelement, mode); |
---|
74 | |
---|
75 | XMLPortParam(WeaponMode, "mode", setMode, getMode, xmlelement, mode); |
---|
76 | |
---|
77 | XMLPortParam(WeaponMode, "munitiontype", setMunitionName, getMunitionName, xmlelement, mode); |
---|
78 | XMLPortParam(WeaponMode, "initialmunition", setInitialMunition, getInitialMunition, xmlelement, mode); |
---|
79 | XMLPortParam(WeaponMode, "initialmagazines", setInitialMagazines, getInitialMagazines, xmlelement, mode); |
---|
80 | XMLPortParam(WeaponMode, "munitionpershot", setMunitionPerShot, getMunitionPerShot, xmlelement, mode); |
---|
81 | |
---|
82 | XMLPortParam(WeaponMode, "reloadtime", setReloadTime, getReloadTime, xmlelement, mode); |
---|
83 | XMLPortParam(WeaponMode, "autoreload", setAutoReload, getAutoReload, xmlelement, mode).description("If true, the weapon reloads the magazine automatically"); |
---|
84 | XMLPortParam(WeaponMode, "parallelreload", setParallelReload, getParallelReload, xmlelement, mode).description("If true, the weapon reloads in parallel to the magazine reloading"); |
---|
85 | |
---|
86 | XMLPortParam(WeaponMode, "damage", setDamage, getDamage, xmlelement, mode); |
---|
87 | XMLPortParam(WeaponMode, "muzzleoffset", setMuzzleOffset, getMuzzleOffset, xmlelement, mode); |
---|
88 | } |
---|
89 | |
---|
90 | bool WeaponMode::fire(float* reloadTime) |
---|
91 | { |
---|
92 | (*reloadTime) = this->reloadTime_; |
---|
93 | |
---|
94 | if (!this->bReloading_ && this->munition_ && this->munition_->takeMunition(this->munitionPerShot_, this)) |
---|
95 | { |
---|
96 | float reloadtime = this->reloadTime_; |
---|
97 | |
---|
98 | if (this->bAutoReload_ && this->munition_->needReload(this)) |
---|
99 | { |
---|
100 | if (this->munition_->reload(this)) |
---|
101 | { |
---|
102 | if (!this->bParallelReload_) |
---|
103 | reloadtime += this->munition_->getReloadTime(); |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | this->bReloading_ = true; |
---|
108 | this->reloadTimer_.setInterval(reloadtime); |
---|
109 | this->reloadTimer_.startTimer(); |
---|
110 | |
---|
111 | this->fire(); |
---|
112 | |
---|
113 | return true; |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | return false; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | bool WeaponMode::reload() |
---|
122 | { |
---|
123 | if (this->munition_ && this->munition_->reload(this)) |
---|
124 | { |
---|
125 | if (!this->bParallelReload_) |
---|
126 | { |
---|
127 | this->bReloading_ = true; |
---|
128 | this->reloadTimer_.setInterval(this->reloadTime_ + this->munition_->getReloadTime()); |
---|
129 | this->reloadTimer_.startTimer(); |
---|
130 | } |
---|
131 | |
---|
132 | return true; |
---|
133 | } |
---|
134 | |
---|
135 | return false; |
---|
136 | } |
---|
137 | |
---|
138 | void WeaponMode::setMunitionType(Identifier* identifier) |
---|
139 | { |
---|
140 | this->munitionname_ = identifier->getName(); |
---|
141 | this->munitiontype_ = identifier; |
---|
142 | this->updateMunition(); |
---|
143 | } |
---|
144 | |
---|
145 | void WeaponMode::setMunitionName(const std::string& munitionname) |
---|
146 | { |
---|
147 | this->munitionname_ = munitionname; |
---|
148 | this->munitiontype_ = ClassByString(this->munitionname_); |
---|
149 | this->updateMunition(); |
---|
150 | } |
---|
151 | |
---|
152 | void WeaponMode::updateMunition() |
---|
153 | { |
---|
154 | if (this->munitiontype_ && this->weapon_ && this->weapon_->getWeaponPack() && this->weapon_->getWeaponPack()->getWeaponSystem()) |
---|
155 | { |
---|
156 | this->munition_ = this->weapon_->getWeaponPack()->getWeaponSystem()->getMunition(&this->munitiontype_); |
---|
157 | |
---|
158 | if (this->munition_) |
---|
159 | { |
---|
160 | // Add the initial magazines |
---|
161 | this->munition_->addMagazines(this->initialMagazines_); |
---|
162 | |
---|
163 | // Maybe we have to reload (if this munition is used the first time or if there weren't any magazines available before) |
---|
164 | if (this->munition_->needReload(this)) |
---|
165 | this->munition_->reload(this, false); |
---|
166 | |
---|
167 | // Add the initial munition |
---|
168 | if (this->initialMunition_ > 0 && this->munition_->getNumMunitionInCurrentMagazine(this) == this->munition_->getMaxMunitionPerMagazine()) |
---|
169 | { |
---|
170 | // The current magazine is still full, so let's just add another magazine to |
---|
171 | // the stack and reduce the current magazine to the given amount of munition |
---|
172 | |
---|
173 | unsigned int initialmunition = this->initialMunition_; |
---|
174 | if (initialmunition > this->munition_->getMaxMunitionPerMagazine()) |
---|
175 | initialmunition = this->munition_->getMaxMunitionPerMagazine(); |
---|
176 | |
---|
177 | this->munition_->takeMunition(this->munition_->getMaxMunitionPerMagazine() - initialmunition, this); |
---|
178 | this->munition_->addMagazines(1); |
---|
179 | } |
---|
180 | else |
---|
181 | { |
---|
182 | // The current magazine isn't full, add the munition directly |
---|
183 | |
---|
184 | this->munition_->addMunition(this->initialMunition_); |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | else |
---|
189 | this->munition_ = 0; |
---|
190 | } |
---|
191 | |
---|
192 | void WeaponMode::reloaded() |
---|
193 | { |
---|
194 | this->bReloading_ = false; |
---|
195 | } |
---|
196 | |
---|
197 | Vector3 WeaponMode::getMuzzlePosition() const |
---|
198 | { |
---|
199 | if (this->weapon_) |
---|
200 | return (this->weapon_->getWorldPosition() + this->muzzleOffset_); |
---|
201 | else |
---|
202 | return this->muzzleOffset_; |
---|
203 | } |
---|
204 | |
---|
205 | const Quaternion& WeaponMode::getMuzzleOrientation() const |
---|
206 | { |
---|
207 | if (this->weapon_) |
---|
208 | return this->weapon_->getWorldOrientation(); |
---|
209 | else |
---|
210 | return Quaternion::IDENTITY; |
---|
211 | } |
---|
212 | |
---|
213 | Vector3 WeaponMode::getMuzzleDirection() const |
---|
214 | { |
---|
215 | if (this->weapon_) |
---|
216 | return (this->weapon_->getWorldOrientation() * WorldEntity::FRONT); |
---|
217 | else |
---|
218 | return WorldEntity::FRONT; |
---|
219 | } |
---|
220 | } |
---|