1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software: you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation, either version 3 of the License, or |
---|
10 | * (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | * |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Reto Grieder |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include "OgreMath.h" |
---|
29 | #include "OgreVector3.h" |
---|
30 | #include "OgreStringConverter.h" |
---|
31 | #include "OgreSceneNode.h" |
---|
32 | #include "OgreEntity.h" |
---|
33 | #include "OgreSceneManager.h" |
---|
34 | |
---|
35 | #include "bullet.h" |
---|
36 | #include "bullet_manager.h" |
---|
37 | #include "inertial_node.h" |
---|
38 | #include "ammunition_dump.h" |
---|
39 | #include "run_manager.h" |
---|
40 | |
---|
41 | #include "base_weapon.h" |
---|
42 | |
---|
43 | |
---|
44 | namespace orxonox { |
---|
45 | namespace weapon { |
---|
46 | using namespace Ogre; |
---|
47 | |
---|
48 | BaseWeapon::BaseWeapon(InertialNode *node, AmmunitionDump *ammoDump) |
---|
49 | : sceneMgr_(RunManager::getSingletonPtr()->getSceneManagerPtr()), node_(node), |
---|
50 | bulletCounter_(0), primaryFireRequest_(false), currentState_(IDLE), |
---|
51 | secondaryFireRequest_(false), |
---|
52 | bulletManager_(RunManager::getSingletonPtr()->getBulletManagerPtr()), |
---|
53 | secondaryFired_(false), |
---|
54 | timeSinceNextActionAdded_(0), actionAdded_(false), nextAction_(NOTHING), |
---|
55 | ammoDump_(ammoDump) |
---|
56 | { |
---|
57 | leftAmmo_ = 0; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | BaseWeapon::~BaseWeapon() |
---|
62 | { |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | bool BaseWeapon::addAction(const Action act) |
---|
67 | { |
---|
68 | if (nextAction_ == NOTHING) |
---|
69 | { |
---|
70 | nextAction_ = act; |
---|
71 | actionAdded_ = true; |
---|
72 | return true; |
---|
73 | } |
---|
74 | else |
---|
75 | return false; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void BaseWeapon::primaryFireRequest() |
---|
80 | { |
---|
81 | primaryFireRequest_ = true; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | void BaseWeapon::secondaryFireRequest() |
---|
86 | { |
---|
87 | secondaryFireRequest_ = true; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | bool BaseWeapon::tick(unsigned long time, Real deltaTime) |
---|
92 | { |
---|
93 | // process action adder |
---|
94 | if (actionAdded_) |
---|
95 | { |
---|
96 | timeSinceNextActionAdded_ = time; |
---|
97 | actionAdded_ = false; |
---|
98 | } |
---|
99 | |
---|
100 | if (currentState_ != IDLE) |
---|
101 | { |
---|
102 | switch (currentState_) |
---|
103 | { |
---|
104 | case PRIMARY_FIRE: |
---|
105 | primaryFiring((unsigned int)(time - actionStartTime_)); |
---|
106 | break; |
---|
107 | |
---|
108 | case SECONDARY_FIRE: |
---|
109 | secondaryFiring((unsigned int)(time - actionStartTime_)); |
---|
110 | break; |
---|
111 | |
---|
112 | case RELOADING: |
---|
113 | break; |
---|
114 | |
---|
115 | case CHANGING_AMMO: |
---|
116 | break; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | if (currentState_ == IDLE) |
---|
121 | { |
---|
122 | // first, process next action |
---|
123 | if (nextAction_ != NOTHING) |
---|
124 | { |
---|
125 | actionStartTime_ = time; |
---|
126 | switch (nextAction_) |
---|
127 | { |
---|
128 | case RELOAD: |
---|
129 | leftAmmo_ += ammoDump_->getAmmunition("Barrel", magazineSize_ - leftAmmo_); |
---|
130 | break; |
---|
131 | |
---|
132 | case CHANGE_AMMO: |
---|
133 | break; |
---|
134 | |
---|
135 | case SPECIAL: |
---|
136 | break; |
---|
137 | |
---|
138 | default: |
---|
139 | break; |
---|
140 | } |
---|
141 | |
---|
142 | // pay attention when multithreaded! |
---|
143 | nextAction_ = NOTHING; |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | // secondly, execute firing |
---|
148 | if (primaryFireRequest_ && !(secondaryFired_ && secondaryFireRequest_)) |
---|
149 | { |
---|
150 | actionStartTime_ = time; |
---|
151 | currentState_ = PRIMARY_FIRE; |
---|
152 | secondaryFired_ = false; |
---|
153 | primaryFire(); |
---|
154 | } |
---|
155 | else if (secondaryFireRequest_) |
---|
156 | { |
---|
157 | actionStartTime_ = time; |
---|
158 | currentState_ = SECONDARY_FIRE; |
---|
159 | secondaryFired_ = true; |
---|
160 | secondaryFire(); |
---|
161 | } |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | primaryFireRequest_ = false; |
---|
166 | secondaryFireRequest_ = false; |
---|
167 | |
---|
168 | if (time - timeSinceNextActionAdded_ > nextActionValidityPeriod_) |
---|
169 | nextAction_ = NOTHING; |
---|
170 | |
---|
171 | return true; |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | int BaseWeapon::getAmmoState() |
---|
176 | { |
---|
177 | return leftAmmo_; |
---|
178 | } |
---|
179 | } |
---|
180 | } |
---|