| 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 |  *      Reto Grieder | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "OrxonoxStableHeaders.h" | 
|---|
| 30 | #include "BaseWeapon.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include <OgreStringConverter.h> | 
|---|
| 33 | #include <OgreSceneNode.h> | 
|---|
| 34 | #include <OgreEntity.h> | 
|---|
| 35 | #include <OgreSceneManager.h> | 
|---|
| 36 |  | 
|---|
| 37 | #include "util/Math.h" | 
|---|
| 38 | #include "Orxonox.h" | 
|---|
| 39 | #include "Bullet.h" | 
|---|
| 40 | #include "BulletManager.h" | 
|---|
| 41 | #include "AmmunitionDump.h" | 
|---|
| 42 | #include "core/CoreIncludes.h" | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | namespace orxonox { | 
|---|
| 46 |   float BaseWeapon::nextActionValidityPeriod_s = 0.5; | 
|---|
| 47 |  | 
|---|
| 48 |   BaseWeapon::BaseWeapon() | 
|---|
| 49 |     : sceneMgr_(GraphicsEngine::getSingleton().getSceneManager()), | 
|---|
| 50 |       bulletCounter_(0), | 
|---|
| 51 |       bulletManager_(Orxonox::getSingleton()->getBulletMgr()), | 
|---|
| 52 |       ammoDump_(NULL), | 
|---|
| 53 |       primaryFireRequest_(false), secondaryFireRequest_(false), | 
|---|
| 54 |       totalTime_(0.0f), actionStartTime_(0.0f), | 
|---|
| 55 |       currentState_(IDLE), secondaryFired_(false), | 
|---|
| 56 |       nextAction_(NOTHING), actionAdded_(false), | 
|---|
| 57 |       timeSinceNextActionAdded_(0.0f), | 
|---|
| 58 |       leftAmmo_(0) | 
|---|
| 59 |   { | 
|---|
| 60 |     RegisterObject(BaseWeapon); | 
|---|
| 61 |   } | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 |   BaseWeapon::~BaseWeapon() | 
|---|
| 65 |   { | 
|---|
| 66 |   } | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 |   bool BaseWeapon::addAction(const Action act) | 
|---|
| 70 |   { | 
|---|
| 71 |     if (nextAction_ == NOTHING) | 
|---|
| 72 |     { | 
|---|
| 73 |       nextAction_ = act; | 
|---|
| 74 |       actionAdded_ = true; | 
|---|
| 75 |       return true; | 
|---|
| 76 |     } | 
|---|
| 77 |     else | 
|---|
| 78 |       return false; | 
|---|
| 79 |   } | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 |   void BaseWeapon::primaryFireRequest() | 
|---|
| 83 |   { | 
|---|
| 84 |     primaryFireRequest_ = true; | 
|---|
| 85 |   } | 
|---|
| 86 |  | 
|---|
| 87 |  | 
|---|
| 88 |   void BaseWeapon::secondaryFireRequest() | 
|---|
| 89 |   { | 
|---|
| 90 |     secondaryFireRequest_ = true; | 
|---|
| 91 |   } | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 |   void BaseWeapon::tick(float dt) | 
|---|
| 95 |   { | 
|---|
| 96 |     totalTime_ += dt; | 
|---|
| 97 |     // process action adder | 
|---|
| 98 |     if (actionAdded_) | 
|---|
| 99 |     { | 
|---|
| 100 |       timeSinceNextActionAdded_ = totalTime_; | 
|---|
| 101 |       actionAdded_ = false; | 
|---|
| 102 |     } | 
|---|
| 103 |  | 
|---|
| 104 |     switch (currentState_) | 
|---|
| 105 |     { | 
|---|
| 106 |     case IDLE: | 
|---|
| 107 |       break; | 
|---|
| 108 |  | 
|---|
| 109 |     case PRIMARY_FIRE: | 
|---|
| 110 |       primaryFiring(totalTime_ - actionStartTime_); | 
|---|
| 111 |       break; | 
|---|
| 112 |  | 
|---|
| 113 |     case SECONDARY_FIRE: | 
|---|
| 114 |       secondaryFiring(totalTime_ - actionStartTime_); | 
|---|
| 115 |       break; | 
|---|
| 116 |  | 
|---|
| 117 |     case RELOADING: | 
|---|
| 118 |       break; | 
|---|
| 119 |  | 
|---|
| 120 |     case CHANGING_AMMO: | 
|---|
| 121 |       break; | 
|---|
| 122 |     } | 
|---|
| 123 |  | 
|---|
| 124 |     if (currentState_ == IDLE) | 
|---|
| 125 |     { | 
|---|
| 126 |       // first, process next action | 
|---|
| 127 |       if (nextAction_ != NOTHING) | 
|---|
| 128 |       { | 
|---|
| 129 |         actionStartTime_ = totalTime_; | 
|---|
| 130 |         switch (nextAction_) | 
|---|
| 131 |         { | 
|---|
| 132 |         case RELOAD: | 
|---|
| 133 |           leftAmmo_ += ammoDump_->getAmmunition("Barrel", magazineSize_ - leftAmmo_); | 
|---|
| 134 |           break; | 
|---|
| 135 |  | 
|---|
| 136 |         case CHANGE_AMMO: | 
|---|
| 137 |           break; | 
|---|
| 138 |  | 
|---|
| 139 |         case SPECIAL: | 
|---|
| 140 |           break; | 
|---|
| 141 |  | 
|---|
| 142 |         default: | 
|---|
| 143 |           break; | 
|---|
| 144 |         } | 
|---|
| 145 |  | 
|---|
| 146 |         // pay attention when multithreaded! | 
|---|
| 147 |         nextAction_ = NOTHING; | 
|---|
| 148 |       } | 
|---|
| 149 |       else | 
|---|
| 150 |       { | 
|---|
| 151 |         // secondly, execute firing | 
|---|
| 152 |         if (primaryFireRequest_ && !(secondaryFired_ && secondaryFireRequest_)) | 
|---|
| 153 |         { | 
|---|
| 154 |           actionStartTime_ = totalTime_; | 
|---|
| 155 |           currentState_ = PRIMARY_FIRE; | 
|---|
| 156 |           secondaryFired_ = false; | 
|---|
| 157 |           primaryFire(); | 
|---|
| 158 |         } | 
|---|
| 159 |         else if (secondaryFireRequest_) | 
|---|
| 160 |         { | 
|---|
| 161 |           actionStartTime_ = totalTime_; | 
|---|
| 162 |           currentState_ = SECONDARY_FIRE; | 
|---|
| 163 |           secondaryFired_ = true; | 
|---|
| 164 |           secondaryFire(); | 
|---|
| 165 |         } | 
|---|
| 166 |       } | 
|---|
| 167 |     } | 
|---|
| 168 |  | 
|---|
| 169 |     primaryFireRequest_ = false; | 
|---|
| 170 |     secondaryFireRequest_ = false; | 
|---|
| 171 |  | 
|---|
| 172 |     if (totalTime_ - timeSinceNextActionAdded_ > nextActionValidityPeriod_s) | 
|---|
| 173 |       nextAction_ = NOTHING; | 
|---|
| 174 |   } | 
|---|
| 175 |  | 
|---|
| 176 |  | 
|---|
| 177 |   int BaseWeapon::getAmmoState() | 
|---|
| 178 |   { | 
|---|
| 179 |     return leftAmmo_; | 
|---|
| 180 |   } | 
|---|
| 181 |  | 
|---|
| 182 |   void BaseWeapon::setAmmoDump(AmmunitionDump* dump) | 
|---|
| 183 |   { | 
|---|
| 184 |     this->ammoDump_ = dump; | 
|---|
| 185 |   } | 
|---|
| 186 | } | 
|---|