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 "Weapon.h" |
---|
32 | |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/XMLPort.h" |
---|
35 | |
---|
36 | #include "WeaponMode.h" |
---|
37 | #include "WeaponPack.h" |
---|
38 | #include "WeaponSystem.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | CreateFactory(Weapon); |
---|
43 | |
---|
44 | Weapon::Weapon(BaseObject* creator) : StaticEntity(creator) |
---|
45 | { |
---|
46 | RegisterObject(Weapon); |
---|
47 | |
---|
48 | this->weaponPack_ = 0; |
---|
49 | this->weaponSlot_ = 0; |
---|
50 | this->bReloading_ = false; |
---|
51 | this->reloadingWeaponmode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; |
---|
52 | |
---|
53 | this->reloadTimer_.setTimer(0.0f, false, this, createExecutor(createFunctor(&Weapon::reloaded))); |
---|
54 | this->reloadTimer_.stopTimer(); |
---|
55 | } |
---|
56 | |
---|
57 | Weapon::~Weapon() |
---|
58 | { |
---|
59 | if (this->isInitialized()) |
---|
60 | { |
---|
61 | if (this->weaponPack_) |
---|
62 | this->weaponPack_->removeWeapon(this); |
---|
63 | |
---|
64 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) |
---|
65 | delete it->second; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | void Weapon::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
70 | { |
---|
71 | SUPER(Weapon, XMLPort, xmlelement, mode); |
---|
72 | |
---|
73 | XMLPortObject(Weapon, WeaponMode, "", addWeaponmode, getWeaponmode, xmlelement, mode); |
---|
74 | } |
---|
75 | |
---|
76 | void Weapon::addWeaponmode(WeaponMode* weaponmode) |
---|
77 | { |
---|
78 | if (!weaponmode) |
---|
79 | return; |
---|
80 | |
---|
81 | this->weaponmodes_.insert(std::pair<unsigned int, WeaponMode*>(weaponmode->getMode(), weaponmode)); |
---|
82 | weaponmode->setWeapon(this); |
---|
83 | } |
---|
84 | |
---|
85 | WeaponMode* Weapon::getWeaponmode(unsigned int index) const |
---|
86 | { |
---|
87 | unsigned int i = 0; |
---|
88 | for (std::multimap<unsigned int, WeaponMode*>::const_iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) |
---|
89 | { |
---|
90 | if (i == index) |
---|
91 | return it->second; |
---|
92 | |
---|
93 | ++i; |
---|
94 | } |
---|
95 | return 0; |
---|
96 | } |
---|
97 | |
---|
98 | void Weapon::fire(unsigned int mode) |
---|
99 | { |
---|
100 | // To avoid firing with more than one mode at the same time, we lock the weapon (reloading) for |
---|
101 | // all modes except the one which is currently reloading. |
---|
102 | // |
---|
103 | // Example: |
---|
104 | // WeaponMode A -> mode 0 |
---|
105 | // WeaponMode B -> mode 0 |
---|
106 | // WeaponMode C -> mode 1 |
---|
107 | // |
---|
108 | // -> A and B can fire at the same time, but C has to wait until both (A and B) have reloaded |
---|
109 | // -> If C fires, A and B have to wait until C has reloaded |
---|
110 | // |
---|
111 | // Note: The reloading of each WeaponMode is internally handled by each A, B and C. |
---|
112 | // The reloading of the weapon is only performed to avoid firing with different modes at the same time. |
---|
113 | if (this->bReloading_ && this->reloadingWeaponmode_ != mode) |
---|
114 | return; |
---|
115 | |
---|
116 | std::multimap<unsigned int, WeaponMode*>::iterator start = this->weaponmodes_.lower_bound(mode); |
---|
117 | std::multimap<unsigned int, WeaponMode*>::iterator end = this->weaponmodes_.upper_bound(mode); |
---|
118 | |
---|
119 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = start; it != end; ++it) |
---|
120 | { |
---|
121 | float reloading_time = 0; |
---|
122 | if (it->second->fire(&reloading_time)) |
---|
123 | { |
---|
124 | this->bReloading_ = true; |
---|
125 | this->reloadingWeaponmode_ = mode; |
---|
126 | |
---|
127 | this->reloadTimer_.setInterval(reloading_time); |
---|
128 | this->reloadTimer_.startTimer(); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | void Weapon::reload() |
---|
134 | { |
---|
135 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) |
---|
136 | it->second->reload(); |
---|
137 | } |
---|
138 | |
---|
139 | void Weapon::reloaded() |
---|
140 | { |
---|
141 | this->bReloading_ = false; |
---|
142 | this->reloadingWeaponmode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; |
---|
143 | } |
---|
144 | |
---|
145 | void Weapon::notifyWeaponModes() |
---|
146 | { |
---|
147 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) |
---|
148 | it->second->setWeapon(this); |
---|
149 | } |
---|
150 | } |
---|