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