[198] | 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 | |
---|
| 40 | #include "base_weapon.h" |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | namespace orxonox { |
---|
| 44 | namespace weapon { |
---|
| 45 | using namespace Ogre; |
---|
| 46 | |
---|
| 47 | BaseWeapon::BaseWeapon(SceneManager *sceneMgr, InertialNode *node, |
---|
| 48 | BulletManager *bulletManager, AmmunitionDump *ammoDump) |
---|
| 49 | : sceneMgr_(sceneMgr), node_(node), |
---|
| 50 | bulletCounter_(0), primaryFireRequest_(false), currentState_(IDLE), |
---|
| 51 | secondaryFireRequest_(false), |
---|
| 52 | bulletManager_(bulletManager), secondaryFired_(false), |
---|
| 53 | timeSinceNextActionAdded_(0), actionAdded_(false), nextAction_(NOTHING), |
---|
| 54 | name_("Base Weapon"), primaryFirePower_(100), secondaryFirePower_(500), |
---|
| 55 | primaryFiringRate_(10), secondaryFiringRate_(2), primaryBulletSpeed_(1000), |
---|
| 56 | secondaryBulletSpeed_(500), magazineSize_(25), ammoDump_(ammoDump) |
---|
| 57 | { |
---|
| 58 | leftAmmo_ = ammoDump_->getAmmunition(magazineSize_); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | BaseWeapon::~BaseWeapon() |
---|
| 63 | { |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | bool BaseWeapon::addAction(const Action act) |
---|
| 68 | { |
---|
| 69 | if (nextAction_ == NOTHING) |
---|
| 70 | { |
---|
| 71 | nextAction_ = act; |
---|
| 72 | actionAdded_ = true; |
---|
| 73 | return true; |
---|
| 74 | } |
---|
| 75 | else |
---|
| 76 | return false; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | void BaseWeapon::primaryFireRequest() |
---|
| 81 | { |
---|
| 82 | primaryFireRequest_ = true; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | void BaseWeapon::primaryFire() |
---|
| 87 | { |
---|
| 88 | if (leftAmmo_ < 1) |
---|
| 89 | { |
---|
| 90 | currentState_ = IDLE; |
---|
| 91 | return; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | SceneNode *temp = sceneMgr_->getRootSceneNode()->createChildSceneNode( |
---|
| 95 | node_->getSceneNode()->getWorldPosition(), |
---|
| 96 | node_->getSceneNode()->getWorldOrientation()); |
---|
| 97 | |
---|
| 98 | Entity* bulletEntity = sceneMgr_->createEntity("BulletEntity" |
---|
| 99 | + StringConverter::toString(bulletCounter_++), "Barrel.mesh"); |
---|
| 100 | |
---|
| 101 | Vector3 speed = (temp->getOrientation() * Vector3(0, 0, -1)) |
---|
| 102 | .normalisedCopy() * primaryBulletSpeed_; |
---|
| 103 | speed += node_->getWorldSpeed(); |
---|
| 104 | |
---|
| 105 | temp->setScale(Vector3(1, 1, 1) * 4); |
---|
| 106 | temp->yaw(Degree(-90)); |
---|
| 107 | |
---|
| 108 | bulletManager_->addBullet(new Bullet(temp, bulletEntity, speed)); |
---|
| 109 | |
---|
| 110 | --leftAmmo_; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | void BaseWeapon::primaryFiring(unsigned int time) |
---|
| 115 | { |
---|
| 116 | if (time > 100) |
---|
| 117 | { |
---|
| 118 | currentState_ = IDLE; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | void BaseWeapon::secondaryFireRequest() |
---|
| 124 | { |
---|
| 125 | secondaryFireRequest_ = true; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | void BaseWeapon::secondaryFire() |
---|
| 130 | { |
---|
| 131 | if (leftAmmo_ < 5) |
---|
| 132 | { |
---|
| 133 | currentState_ = IDLE; |
---|
| 134 | return; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | SceneNode *temp = sceneMgr_->getRootSceneNode()->createChildSceneNode( |
---|
| 138 | node_->getSceneNode()->getWorldPosition(), |
---|
| 139 | node_->getSceneNode()->getWorldOrientation()); |
---|
| 140 | |
---|
| 141 | Entity* bulletEntity = sceneMgr_->createEntity("BulletEntity" |
---|
| 142 | + StringConverter::toString(bulletCounter_++), "Barrel.mesh"); |
---|
| 143 | |
---|
| 144 | Vector3 speed = (temp->getOrientation() * Vector3(0, 0, -1)) |
---|
| 145 | .normalisedCopy() * secondaryBulletSpeed_*0.5; |
---|
| 146 | speed += node_->getWorldSpeed(); |
---|
| 147 | |
---|
| 148 | temp->setScale(Vector3(1, 1, 1) * 10); |
---|
| 149 | temp->yaw(Degree(-90)); |
---|
| 150 | |
---|
| 151 | bulletManager_->addBullet(new Bullet(temp, bulletEntity, speed)); |
---|
| 152 | |
---|
| 153 | leftAmmo_ -= 5; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | void BaseWeapon::secondaryFiring(unsigned int time) |
---|
| 158 | { |
---|
| 159 | if (time > 250) |
---|
| 160 | currentState_ = IDLE; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | bool BaseWeapon::tick(unsigned long time, Real deltaTime) |
---|
| 165 | { |
---|
| 166 | // process action adder |
---|
| 167 | if (actionAdded_) |
---|
| 168 | { |
---|
| 169 | timeSinceNextActionAdded_ = time; |
---|
| 170 | actionAdded_ = false; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | switch (currentState_) |
---|
| 174 | { |
---|
| 175 | case IDLE: |
---|
| 176 | // first, process next action |
---|
| 177 | if (nextAction_ != NOTHING) |
---|
| 178 | { |
---|
| 179 | actionStartTime_ = time; |
---|
| 180 | switch (nextAction_) |
---|
| 181 | { |
---|
| 182 | case RELOAD: |
---|
| 183 | leftAmmo_ += ammoDump_->getAmmunition(magazineSize_ - leftAmmo_); |
---|
| 184 | break; |
---|
| 185 | |
---|
| 186 | case CHANGE_AMMO: |
---|
| 187 | break; |
---|
| 188 | |
---|
| 189 | case SPECIAL: |
---|
| 190 | break; |
---|
| 191 | |
---|
| 192 | default: |
---|
| 193 | break; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | // pay attention when multithreaded! |
---|
| 197 | nextAction_ = NOTHING; |
---|
| 198 | } |
---|
| 199 | else |
---|
| 200 | { |
---|
| 201 | // secondly, execute firing |
---|
| 202 | if (primaryFireRequest_ && !(secondaryFired_ && secondaryFireRequest_)) |
---|
| 203 | { |
---|
| 204 | actionStartTime_ = time; |
---|
| 205 | currentState_ = PRIMARY_FIRE; |
---|
| 206 | secondaryFired_ = false; |
---|
| 207 | primaryFire(); |
---|
| 208 | } |
---|
| 209 | else if (secondaryFireRequest_) |
---|
| 210 | { |
---|
| 211 | actionStartTime_ = time; |
---|
| 212 | currentState_ = SECONDARY_FIRE; |
---|
| 213 | secondaryFired_ = true; |
---|
| 214 | secondaryFire(); |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | break; |
---|
| 219 | |
---|
| 220 | case PRIMARY_FIRE: |
---|
| 221 | primaryFiring((unsigned int)(time - actionStartTime_)); |
---|
| 222 | break; |
---|
| 223 | |
---|
| 224 | case SECONDARY_FIRE: |
---|
| 225 | secondaryFiring((unsigned int)(time - actionStartTime_)); |
---|
| 226 | break; |
---|
| 227 | |
---|
| 228 | case RELOADING: |
---|
| 229 | break; |
---|
| 230 | |
---|
| 231 | case CHANGING_AMMO: |
---|
| 232 | break; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | primaryFireRequest_ = false; |
---|
| 236 | secondaryFireRequest_ = false; |
---|
| 237 | |
---|
| 238 | if (time - timeSinceNextActionAdded_ > nextActionValidityPeriod_) |
---|
| 239 | nextAction_ = NOTHING; |
---|
| 240 | |
---|
| 241 | return true; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | int BaseWeapon::getAmmoState() |
---|
| 246 | { |
---|
| 247 | return leftAmmo_; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | } |
---|