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