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