[157] | 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 | |
---|
[194] | 28 | #include "OgreMath.h" |
---|
| 29 | #include "OgreVector3.h" |
---|
| 30 | #include "OgreQuaternion.h" |
---|
[161] | 31 | #include "OgreSceneNode.h" |
---|
| 32 | #include "OgreEntity.h" |
---|
[194] | 33 | #include "OgreSceneManager.h" |
---|
[157] | 34 | |
---|
[189] | 35 | #include "inertial_node.h" |
---|
[232] | 36 | #include "run_manager.h" |
---|
[193] | 37 | #include "weapon/bullet.h" |
---|
| 38 | #include "weapon/bullet_manager.h" |
---|
[198] | 39 | #include "weapon/weapon_station.h" |
---|
| 40 | #include "weapon/base_weapon.h" |
---|
[232] | 41 | #include "weapon/barrel_gun.h" |
---|
[198] | 42 | #include "weapon/ammunition_dump.h" |
---|
[157] | 43 | |
---|
| 44 | #include "orxonox_ship.h" |
---|
| 45 | |
---|
[169] | 46 | |
---|
| 47 | namespace orxonox { |
---|
[161] | 48 | using namespace Ogre; |
---|
[189] | 49 | using namespace weapon; |
---|
[157] | 50 | |
---|
[161] | 51 | /** |
---|
| 52 | * Base class for any kind of flyable ship in Orxonox. |
---|
| 53 | * |
---|
| 54 | * The ship offers steering methods (like left, right, etc.) and translates |
---|
| 55 | * them into movement. A ship can also hold more than one weapons (where each |
---|
| 56 | * of these can be replaced during the game). This means that a ship can have |
---|
| 57 | * many WeaponManagers but only one MunitionManager (independant object that |
---|
| 58 | * is referenced in each WeaponManager). |
---|
| 59 | * Furthermore a ship in Orxonox is responsible for its visualization, which is |
---|
| 60 | * why it receives a pointer to the SceneManager. |
---|
| 61 | */ |
---|
[157] | 62 | |
---|
| 63 | |
---|
[161] | 64 | /** |
---|
| 65 | * Standard constructor, that only initalizes a few variables. Some of them |
---|
| 66 | * could be made static, since any new ship would be derived from the BaseShip. |
---|
| 67 | * Or even better: write config files for each ship so that manipulating |
---|
| 68 | * its properties would be even easier. |
---|
| 69 | * @param mSceneMgr The current main SceneManager |
---|
| 70 | * @param mNode The scene node which the ship will be attached to later. |
---|
| 71 | */ |
---|
[232] | 72 | OrxonoxShip::OrxonoxShip(SceneNode *node) |
---|
| 73 | : sceneMgr_(RunManager::getSingletonPtr()->getSceneManagerPtr()), |
---|
| 74 | bulletManager_(RunManager::getSingletonPtr()->getBulletManagerPtr()), |
---|
| 75 | baseThrust_(1000), currentThrust_(Vector3::ZERO), objectCounter_(0) |
---|
[161] | 76 | { |
---|
[189] | 77 | rootNode_ = new InertialNode(node, Vector3::ZERO); |
---|
[161] | 78 | } |
---|
[157] | 79 | |
---|
| 80 | |
---|
[161] | 81 | /** |
---|
| 82 | * Standard destructor. |
---|
| 83 | * Doesn't have any work to do yet. |
---|
| 84 | */ |
---|
| 85 | OrxonoxShip::~OrxonoxShip() |
---|
| 86 | { |
---|
[177] | 87 | if (mainWeapon_) |
---|
| 88 | delete mainWeapon_; |
---|
[198] | 89 | if (railGunStation_) |
---|
| 90 | delete railGunStation_; |
---|
[189] | 91 | if (rootNode_) |
---|
| 92 | delete rootNode_; |
---|
[161] | 93 | } |
---|
[157] | 94 | |
---|
| 95 | |
---|
[161] | 96 | /** |
---|
| 97 | * Initialises everything. |
---|
| 98 | * Once that ResourceGroups are organised, this method loads them. |
---|
| 99 | * It might be an idea to make this function static in order for the |
---|
| 100 | * SceneManger to call the initialise method of every needed class (macros..) |
---|
| 101 | * @return Returns false when failed. |
---|
| 102 | */ |
---|
| 103 | bool OrxonoxShip::initialise() |
---|
| 104 | { |
---|
| 105 | // load all the resources needed (no resource groups yet, |
---|
| 106 | // so the allInit is not executed!) |
---|
| 107 | // ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
[157] | 108 | |
---|
[161] | 109 | // create the "space ship" (currently a fish..) |
---|
| 110 | // TODO: names must be unique! use static variables.. |
---|
| 111 | shipEntity_ = sceneMgr_->createEntity("Ship", "fish.mesh"); |
---|
[189] | 112 | InertialNode *fishNode = rootNode_->createChildNode(); |
---|
| 113 | fishNode->getSceneNode()->yaw(Degree(-90)); |
---|
| 114 | fishNode->getSceneNode()->attachObject(shipEntity_); |
---|
| 115 | fishNode->getSceneNode()->setScale(Vector3(10, 10, 10)); |
---|
[157] | 116 | |
---|
[169] | 117 | // initialise weapon(s) |
---|
[232] | 118 | ammoDump_ = new AmmunitionDump(); |
---|
| 119 | ammoDump_->setDumpSize("Barrel", 1000); |
---|
| 120 | ammoDump_->store("Barrel", 420); |
---|
[198] | 121 | |
---|
[189] | 122 | InertialNode *mainWeaponNode = rootNode_->createChildNode(); |
---|
[232] | 123 | mainWeapon_ = new BarrelGun(mainWeaponNode, ammoDump_); |
---|
[169] | 124 | |
---|
[198] | 125 | railGunStation_ = new WeaponStation(4); |
---|
| 126 | railGunStation_->addWeapon(mainWeapon_); |
---|
| 127 | railGunStation_->selectWeapon(0); |
---|
| 128 | |
---|
[161] | 129 | return true; |
---|
| 130 | } |
---|
[157] | 131 | |
---|
| 132 | |
---|
[161] | 133 | /** |
---|
| 134 | * Gets the ship to accelerate in the current direction. |
---|
[177] | 135 | * The value should be between 0 and 1, with one beeing full thrust and 0 none |
---|
[161] | 136 | * @param value Acceleration between 0 and 1 |
---|
| 137 | */ |
---|
| 138 | void OrxonoxShip::setMainThrust(const Real value) |
---|
| 139 | { |
---|
| 140 | currentThrust_.z = value * baseThrust_; |
---|
| 141 | } |
---|
[159] | 142 | |
---|
| 143 | |
---|
[161] | 144 | /** |
---|
| 145 | * Gets the ship to accelerate sideways regarding the current direction. |
---|
[177] | 146 | * The value should be between 0 and 1, with one beeing full thrust and 0 none |
---|
[161] | 147 | * @param value Acceleration between 0 and 1 |
---|
| 148 | */ |
---|
| 149 | void OrxonoxShip::setSideThrust(const Real value) |
---|
| 150 | { |
---|
| 151 | currentThrust_.x = value * baseThrust_; |
---|
| 152 | } |
---|
[157] | 153 | |
---|
| 154 | |
---|
[161] | 155 | /** |
---|
| 156 | * Gets the ship to accelerate up and down. |
---|
[177] | 157 | * The value should be between 0 and 1, with one beeing full thrust and 0 none |
---|
[161] | 158 | * @param value Acceleration between 0 and 1 |
---|
| 159 | */ |
---|
| 160 | void OrxonoxShip::setYThrust(const Real value) |
---|
| 161 | { |
---|
| 162 | currentThrust_.y = value * baseThrust_; |
---|
| 163 | } |
---|
[157] | 164 | |
---|
| 165 | |
---|
[161] | 166 | /** |
---|
| 167 | * Rotate the ship along with the camera up and down. |
---|
| 168 | * @param angle Pitch value. |
---|
| 169 | */ |
---|
| 170 | void OrxonoxShip::turnUpAndDown(const Radian &angle) |
---|
| 171 | { |
---|
[189] | 172 | rootNode_->getSceneNode()->pitch(-angle, Node::TS_LOCAL); |
---|
[161] | 173 | } |
---|
[159] | 174 | |
---|
| 175 | |
---|
[161] | 176 | /** |
---|
| 177 | * Rotate the ship along with the camera left and right. |
---|
| 178 | * @param angle Yaw value. |
---|
| 179 | */ |
---|
| 180 | void OrxonoxShip::turnLeftAndRight(const Radian &angle) |
---|
| 181 | { |
---|
[189] | 182 | rootNode_->getSceneNode()->yaw(-angle, Node::TS_PARENT); |
---|
[161] | 183 | } |
---|
[159] | 184 | |
---|
[157] | 185 | |
---|
[161] | 186 | /** |
---|
| 187 | * Returns the current speed of the ship according to its parent node. |
---|
| 188 | * @return The current speed. |
---|
| 189 | */ |
---|
| 190 | Vector3 OrxonoxShip::getSpeed() |
---|
| 191 | { |
---|
[189] | 192 | return rootNode_->getSpeed(); |
---|
[161] | 193 | } |
---|
[157] | 194 | |
---|
[161] | 195 | /** |
---|
| 196 | * Returns the ship's root SceneNode. |
---|
| 197 | * @return The Root Node. |
---|
| 198 | */ |
---|
[189] | 199 | InertialNode* OrxonoxShip::getRootNode() |
---|
[161] | 200 | { |
---|
| 201 | return rootNode_; |
---|
| 202 | } |
---|
[157] | 203 | |
---|
| 204 | |
---|
[161] | 205 | /** |
---|
| 206 | * Fire a bullet (Entity with SceneNode). |
---|
| 207 | * This method creates a new Entity plus a SceneNode. But be sure not make |
---|
| 208 | * the new Node a child of RootNode_! |
---|
| 209 | * @return Bullet containing speed and entity. |
---|
| 210 | */ |
---|
[198] | 211 | BaseWeapon* OrxonoxShip::getMainWeapon() |
---|
[161] | 212 | { |
---|
[194] | 213 | return mainWeapon_; |
---|
[161] | 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
[198] | 217 | int OrxonoxShip::getAmmoStock() |
---|
| 218 | { |
---|
[232] | 219 | return ammoDump_->getStockSize("Barrel"); |
---|
[198] | 220 | } |
---|
| 221 | |
---|
| 222 | |
---|
[161] | 223 | /** |
---|
| 224 | * Standard tick() function. |
---|
| 225 | * Currently, only the speed is applied according to the thrust values. |
---|
| 226 | * @param time Absolute time. |
---|
| 227 | * @param deltaTime Relative time. |
---|
| 228 | * @return Return true to continue render |
---|
| 229 | */ |
---|
| 230 | bool OrxonoxShip::tick(unsigned long time, Real deltaTime) |
---|
| 231 | { |
---|
[177] | 232 | mainWeapon_->tick(time, deltaTime); |
---|
| 233 | |
---|
[189] | 234 | Quaternion quad = rootNode_->getSceneNode()->getOrientation(); |
---|
[161] | 235 | quad.normalise(); |
---|
[189] | 236 | rootNode_->addSpeed(quad * (Vector3(-1, -1, -1) * currentThrust_) * deltaTime); |
---|
[161] | 237 | |
---|
[189] | 238 | rootNode_->getSceneNode()->translate(rootNode_->getSpeed() * deltaTime); |
---|
[161] | 239 | |
---|
| 240 | return true; |
---|
| 241 | } |
---|
| 242 | |
---|
[157] | 243 | } |
---|