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