| 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 "OgreSceneManager.h" | 
|---|
| 29 | #include "OgreEntity.h" | 
|---|
| 30 | #include "OgreSceneNode.h" | 
|---|
| 31 | #include "OgreVector3.h" | 
|---|
| 32 | #include "OgreStringConverter.h" | 
|---|
| 33 |  | 
|---|
| 34 | #include "weapon.h" | 
|---|
| 35 | #include "bullet.h" | 
|---|
| 36 | #include "bullet_manager.h" | 
|---|
| 37 | #include "inertial_node.h" | 
|---|
| 38 | #include "weapon_manager.h" | 
|---|
| 39 |  | 
|---|
| 40 | #define ACTION_LIST_SIZE 4 | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | namespace orxonox { | 
|---|
| 44 | namespace weapon { | 
|---|
| 45 |   using namespace Ogre; | 
|---|
| 46 |  | 
|---|
| 47 |   Weapon** WeaponManager::weaponList_s = NULL; | 
|---|
| 48 |  | 
|---|
| 49 |   WeaponManager::WeaponManager(SceneManager *sceneMgr, InertialNode *node, | 
|---|
| 50 |         BulletManager *bulletManager, int slotSize) | 
|---|
| 51 |         : sceneMgr_(sceneMgr), node_(node), slotSize_(slotSize), slotIndex_(0), | 
|---|
| 52 |         bulletCounter_(0), primaryFireRequest_(false), currentState_(IDLE), | 
|---|
| 53 |         secondaryFireRequest_(false), selectedWeapon_(0), | 
|---|
| 54 |         bulletManager_(bulletManager), | 
|---|
| 55 |         actionListReadIndex_(0), actionListWriteIndex_(0) | 
|---|
| 56 |   { | 
|---|
| 57 |         slots_ = new Weapon*[slotSize]; | 
|---|
| 58 |     actionList_ = new Action[ACTION_LIST_SIZE]; | 
|---|
| 59 |     for (int i = 0; i < ACTION_LIST_SIZE; i++) | 
|---|
| 60 |       actionList_[i] = NOTHING; | 
|---|
| 61 |   } | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 |   WeaponManager::~WeaponManager() | 
|---|
| 65 |   { | 
|---|
| 66 |     if (slots_) | 
|---|
| 67 |       delete slots_; | 
|---|
| 68 |     if (actionList_) | 
|---|
| 69 |       delete actionList_; | 
|---|
| 70 |   } | 
|---|
| 71 |  | 
|---|
| 72 |  | 
|---|
| 73 |   bool WeaponManager::addWeapon(const Ogre::String &name) | 
|---|
| 74 |   { | 
|---|
| 75 |     if (!weaponList_s) | 
|---|
| 76 |       return false; | 
|---|
| 77 |  | 
|---|
| 78 |     if (name == weaponList_s[0]->name_) | 
|---|
| 79 |     { | 
|---|
| 80 |       // this is ugly, but for the time being, it has to fit. | 
|---|
| 81 |       selectedWeapon_ = slotIndex_; | 
|---|
| 82 |       slots_[slotIndex_++] = weaponList_s[0]; | 
|---|
| 83 |       return true; | 
|---|
| 84 |     } | 
|---|
| 85 |     else | 
|---|
| 86 |       return false; | 
|---|
| 87 |   } | 
|---|
| 88 |  | 
|---|
| 89 |  | 
|---|
| 90 |   bool WeaponManager::addAction(const Action act) | 
|---|
| 91 |   { | 
|---|
| 92 |     if (actionList_[actionListWriteIndex_] == NOTHING) | 
|---|
| 93 |     { | 
|---|
| 94 |       actionList_[actionListWriteIndex_] = act; | 
|---|
| 95 |       actionListWriteIndex_ = (actionListWriteIndex_ + 1) % ACTION_LIST_SIZE; | 
|---|
| 96 |       return true; | 
|---|
| 97 |     } | 
|---|
| 98 |     else | 
|---|
| 99 |       return false; | 
|---|
| 100 |   } | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 |   void WeaponManager::primaryFireRequest() | 
|---|
| 104 |   { | 
|---|
| 105 |     primaryFireRequest_ = true; | 
|---|
| 106 |   } | 
|---|
| 107 |  | 
|---|
| 108 |  | 
|---|
| 109 |   void WeaponManager::primaryFire() | 
|---|
| 110 |   { | 
|---|
| 111 |     currentState_ = PRIMARY_FIRE; | 
|---|
| 112 |  | 
|---|
| 113 |     // TODO: add the name of the weapon manager. but for that, | 
|---|
| 114 |     // the factory is required. | 
|---|
| 115 |     SceneNode *temp = sceneMgr_->getRootSceneNode()->createChildSceneNode( | 
|---|
| 116 |           node_->getSceneNode()->getWorldPosition(), | 
|---|
| 117 |           node_->getSceneNode()->getWorldOrientation()); | 
|---|
| 118 |  | 
|---|
| 119 |     Entity* bulletEntity = sceneMgr_->createEntity("BulletEntity" | 
|---|
| 120 |           + StringConverter::toString(bulletCounter_++), "Barrel.mesh"); | 
|---|
| 121 |  | 
|---|
| 122 |     Vector3 speed = (temp->getOrientation() * Vector3(0, 0, -1)) | 
|---|
| 123 |           .normalisedCopy() * slots_[selectedWeapon_]->bulletSpeed_; | 
|---|
| 124 |     speed += node_->getWorldSpeed(); | 
|---|
| 125 |  | 
|---|
| 126 |           temp->setScale(Vector3(1, 1, 1) * 10); | 
|---|
| 127 |           temp->yaw(Degree(-90)); | 
|---|
| 128 |  | 
|---|
| 129 |           bulletManager_->addBullet(new Bullet(temp, bulletEntity, speed)); | 
|---|
| 130 |    | 
|---|
| 131 |     currentState_ = IDLE; | 
|---|
| 132 |   } | 
|---|
| 133 |  | 
|---|
| 134 |  | 
|---|
| 135 |   void WeaponManager::secondaryFireRequest() | 
|---|
| 136 |   { | 
|---|
| 137 |     secondaryFireRequest_ = true; | 
|---|
| 138 |   } | 
|---|
| 139 |  | 
|---|
| 140 |   void WeaponManager::secondaryFire() | 
|---|
| 141 |   { | 
|---|
| 142 |   } | 
|---|
| 143 |  | 
|---|
| 144 |  | 
|---|
| 145 |   bool WeaponManager::tick(unsigned long time, Real deltaTime) | 
|---|
| 146 |   { | 
|---|
| 147 |     // return if no weapon has been added | 
|---|
| 148 |     if (!slots_[slotIndex_]) | 
|---|
| 149 |       return true; | 
|---|
| 150 |  | 
|---|
| 151 |     switch (currentState_) | 
|---|
| 152 |     { | 
|---|
| 153 |     case IDLE: | 
|---|
| 154 |       // first, process actions | 
|---|
| 155 |       if (actionList_[actionListReadIndex_] != NOTHING) | 
|---|
| 156 |       { | 
|---|
| 157 |         actionListReadIndex_ = (actionListReadIndex_ + 1) % ACTION_LIST_SIZE; | 
|---|
| 158 |         break; | 
|---|
| 159 |       } | 
|---|
| 160 |  | 
|---|
| 161 |       switch (actionList_[actionListReadIndex_]) | 
|---|
| 162 |       { | 
|---|
| 163 |       case RELOAD: | 
|---|
| 164 |         break; | 
|---|
| 165 |  | 
|---|
| 166 |       case ZOOM_IN: | 
|---|
| 167 |         break; | 
|---|
| 168 |  | 
|---|
| 169 |       case ZOOM_OUT: | 
|---|
| 170 |         break; | 
|---|
| 171 |  | 
|---|
| 172 |       default: | 
|---|
| 173 |         break; | 
|---|
| 174 |       } | 
|---|
| 175 |  | 
|---|
| 176 |       // secondly, execute firing | 
|---|
| 177 |       if (primaryFireRequest_) | 
|---|
| 178 |         primaryFire(); | 
|---|
| 179 |       else if (secondaryFireRequest_) | 
|---|
| 180 |         secondaryFire(); | 
|---|
| 181 |  | 
|---|
| 182 |       break; | 
|---|
| 183 |  | 
|---|
| 184 |     case PRIMARY_FIRE: | 
|---|
| 185 |       break; | 
|---|
| 186 |  | 
|---|
| 187 |     case SECONDARY_FIRE: | 
|---|
| 188 |       break; | 
|---|
| 189 |  | 
|---|
| 190 |     case RELOADING: | 
|---|
| 191 |       break; | 
|---|
| 192 |     } | 
|---|
| 193 |  | 
|---|
| 194 |     primaryFireRequest_ = false; | 
|---|
| 195 |     secondaryFireRequest_ = false; | 
|---|
| 196 |  | 
|---|
| 197 |     return true; | 
|---|
| 198 |   } | 
|---|
| 199 |  | 
|---|
| 200 |  | 
|---|
| 201 |   // static | 
|---|
| 202 |   bool WeaponManager::loadWeapons() | 
|---|
| 203 |   { | 
|---|
| 204 |     weaponList_s = new Weapon*[5]; | 
|---|
| 205 |     for (int i = 0; i < 5; i++) | 
|---|
| 206 |       weaponList_s[i] = NULL; | 
|---|
| 207 |     weaponList_s[0] = new Weapon("Barrel Gun", 10, 2, 500); | 
|---|
| 208 |     return true; | 
|---|
| 209 |   } | 
|---|
| 210 |  | 
|---|
| 211 |  | 
|---|
| 212 |   // static | 
|---|
| 213 |   void WeaponManager::destroyWeapons() | 
|---|
| 214 |   { | 
|---|
| 215 |     if (weaponList_s) | 
|---|
| 216 |     { | 
|---|
| 217 |       for (int i = 0; i < 5; i++) | 
|---|
| 218 |         if (weaponList_s[i]) | 
|---|
| 219 |           delete weaponList_s[i]; | 
|---|
| 220 |       delete weaponList_s; | 
|---|
| 221 |     } | 
|---|
| 222 |   } | 
|---|
| 223 |  | 
|---|
| 224 | } | 
|---|
| 225 | } | 
|---|