[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" |
---|
| 37 | |
---|
[161] | 38 | namespace Orxonox { |
---|
| 39 | using namespace Ogre; |
---|
[157] | 40 | |
---|
[161] | 41 | /** |
---|
| 42 | * Base class for any kind of flyable ship in Orxonox. |
---|
| 43 | * |
---|
| 44 | * The ship offers steering methods (like left, right, etc.) and translates |
---|
| 45 | * them into movement. A ship can also hold more than one weapons (where each |
---|
| 46 | * of these can be replaced during the game). This means that a ship can have |
---|
| 47 | * many WeaponManagers but only one MunitionManager (independant object that |
---|
| 48 | * is referenced in each WeaponManager). |
---|
| 49 | * Furthermore a ship in Orxonox is responsible for its visualization, which is |
---|
| 50 | * why it receives a pointer to the SceneManager. |
---|
| 51 | */ |
---|
[157] | 52 | |
---|
| 53 | |
---|
| 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 | |
---|
[161] | 102 | return true; |
---|
| 103 | } |
---|
[157] | 104 | |
---|
| 105 | |
---|
[161] | 106 | /** |
---|
| 107 | * Gets the ship to accelerate in the current direction. |
---|
| 108 | * The value should be between 0 and 1, with one beeing full thrust and 0 none. |
---|
| 109 | * @param value Acceleration between 0 and 1 |
---|
| 110 | */ |
---|
| 111 | void OrxonoxShip::setMainThrust(const Real value) |
---|
| 112 | { |
---|
| 113 | //currentThrust_ = value * baseThrust_; |
---|
| 114 | currentThrust_.z = value * baseThrust_; |
---|
| 115 | } |
---|
[159] | 116 | |
---|
| 117 | |
---|
[161] | 118 | /** |
---|
| 119 | * Gets the ship to accelerate sideways regarding the current direction. |
---|
| 120 | * The value should be between 0 and 1, with one beeing full thrust and 0 none. |
---|
| 121 | * @param value Acceleration between 0 and 1 |
---|
| 122 | */ |
---|
| 123 | void OrxonoxShip::setSideThrust(const Real value) |
---|
| 124 | { |
---|
| 125 | //currentSideThrust_ = value * baseThrust_; |
---|
| 126 | currentThrust_.x = value * baseThrust_; |
---|
| 127 | } |
---|
[157] | 128 | |
---|
| 129 | |
---|
[161] | 130 | /** |
---|
| 131 | * Gets the ship to accelerate up and down. |
---|
| 132 | * The value should be between 0 and 1, with one beeing full thrust and 0 none. |
---|
| 133 | * @param value Acceleration between 0 and 1 |
---|
| 134 | */ |
---|
| 135 | void OrxonoxShip::setYThrust(const Real value) |
---|
| 136 | { |
---|
| 137 | //currentYThrust_ = value * baseThrust_; |
---|
| 138 | currentThrust_.y = value * baseThrust_; |
---|
| 139 | } |
---|
[157] | 140 | |
---|
| 141 | |
---|
[161] | 142 | /** |
---|
| 143 | * Rotate the ship along with the camera up and down. |
---|
| 144 | * @param angle Pitch value. |
---|
| 145 | */ |
---|
| 146 | void OrxonoxShip::turnUpAndDown(const Radian &angle) |
---|
| 147 | { |
---|
| 148 | rootNode_->pitch(-angle, Node::TS_LOCAL); |
---|
| 149 | } |
---|
[159] | 150 | |
---|
| 151 | |
---|
[161] | 152 | /** |
---|
| 153 | * Rotate the ship along with the camera left and right. |
---|
| 154 | * @param angle Yaw value. |
---|
| 155 | */ |
---|
| 156 | void OrxonoxShip::turnLeftAndRight(const Radian &angle) |
---|
| 157 | { |
---|
| 158 | rootNode_->yaw(-angle, Node::TS_PARENT); |
---|
| 159 | } |
---|
[159] | 160 | |
---|
[157] | 161 | |
---|
[161] | 162 | /** |
---|
| 163 | * Returns the current speed of the ship according to its parent node. |
---|
| 164 | * @return The current speed. |
---|
| 165 | */ |
---|
| 166 | Vector3 OrxonoxShip::getSpeed() |
---|
| 167 | { |
---|
| 168 | return currentSpeed_; |
---|
| 169 | } |
---|
[157] | 170 | |
---|
[161] | 171 | /** |
---|
| 172 | * Returns the ship's root SceneNode. |
---|
| 173 | * @return The Root Node. |
---|
| 174 | */ |
---|
| 175 | SceneNode* OrxonoxShip::getRootNode() |
---|
| 176 | { |
---|
| 177 | return rootNode_; |
---|
| 178 | } |
---|
[157] | 179 | |
---|
| 180 | |
---|
[161] | 181 | /** |
---|
| 182 | * Fire a bullet (Entity with SceneNode). |
---|
| 183 | * This method creates a new Entity plus a SceneNode. But be sure not make |
---|
| 184 | * the new Node a child of RootNode_! |
---|
| 185 | * @return Bullet containing speed and entity. |
---|
| 186 | */ |
---|
| 187 | Bullet* OrxonoxShip::fire() |
---|
| 188 | { |
---|
| 189 | // TODO: Names must be unique! |
---|
| 190 | SceneNode *temp = rootNode_->getParentSceneNode()->createChildSceneNode( |
---|
| 191 | "BulletNode" + StringConverter::toString(objectCounter_)); |
---|
| 192 | temp->setOrientation(rootNode_->getOrientation()); |
---|
| 193 | temp->setPosition(rootNode_->getPosition()); |
---|
| 194 | temp->setScale(Vector3(1, 1, 1) * 10); |
---|
| 195 | temp->yaw(Degree(-90)); |
---|
| 196 | return new Bullet(temp, sceneMgr_->createEntity("bullet" |
---|
| 197 | + StringConverter::toString(objectCounter_++), "Barrel.mesh"), currentSpeed_ |
---|
| 198 | + (rootNode_->getOrientation() * Vector3(0, 0, -1)).normalisedCopy() |
---|
| 199 | * bulletSpeed_); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * Standard tick() function. |
---|
| 205 | * Currently, only the speed is applied according to the thrust values. |
---|
| 206 | * @param time Absolute time. |
---|
| 207 | * @param deltaTime Relative time. |
---|
| 208 | * @return Return true to continue render |
---|
| 209 | */ |
---|
| 210 | bool OrxonoxShip::tick(unsigned long time, Real deltaTime) |
---|
| 211 | { |
---|
| 212 | Quaternion quad = rootNode_->getOrientation(); |
---|
| 213 | quad.normalise(); |
---|
| 214 | currentSpeed_ += quad * (Vector3(-1, -1, -1) * currentThrust_) * deltaTime; |
---|
| 215 | //currentSpeed_ += quad * Vector3(0, 0, -1) * currentThrust_ * deltaTime; |
---|
| 216 | //currentSpeed_ += quad * Vector3(-1, 0, 0) * currentSideThrust_ * deltaTime; |
---|
| 217 | |
---|
| 218 | rootNode_->translate(currentSpeed_ * deltaTime); |
---|
| 219 | |
---|
| 220 | return true; |
---|
| 221 | } |
---|
| 222 | |
---|
[157] | 223 | } |
---|