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 | /** |
---|
29 | * Base class for any kind of flyable ship in Orxonox. |
---|
30 | * |
---|
31 | * The ship offers steering methods (like left, right, etc.) and translates |
---|
32 | * them into movement. A ship can also hold more than one weapons (where each |
---|
33 | * of these can be replaced during the game). This means that a ship can have |
---|
34 | * many WeaponManagers but only one MunitionManager (independant object that |
---|
35 | * is referenced in each WeaponManager). |
---|
36 | * Furthermore a ship in Orxonox is responsible for its visualization, which is |
---|
37 | * why it receives a pointer to the SceneManager. |
---|
38 | */ |
---|
39 | |
---|
40 | |
---|
41 | #include "orxonox_ship.h" |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | * Standard constructor, that only initalizes a few variables. Some of them |
---|
46 | * could be made static, since any new ship would be derived from the BaseShip. |
---|
47 | * Or even better: write config files for each ship so that manipulating |
---|
48 | * its properties would be even easier. |
---|
49 | * @param mSceneMgr The current main SceneManager |
---|
50 | * @param mNode The scene node which the ship will be attached to later. |
---|
51 | */ |
---|
52 | OrxonoxShip::OrxonoxShip(SceneManager *mSceneMgr, SceneNode *mNode) |
---|
53 | : mSceneMgr(mSceneMgr), mRootNode(mNode), speed(Vector3(0, 0, 0)), |
---|
54 | baseThrust(1000), thrust(0), sideThrust(0), n(0), |
---|
55 | bulletSpeed(400) |
---|
56 | { |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | * Standard destructor. |
---|
62 | * Doesn't have any work to do yet. |
---|
63 | */ |
---|
64 | OrxonoxShip::~OrxonoxShip() |
---|
65 | { |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | * Initialises everything. |
---|
71 | * Once that ResourceGroups are organised, this method loads them. |
---|
72 | * It might be an idea to make this function static in order for the |
---|
73 | * SceneManger to call the initialise method of every needed class (macros..) |
---|
74 | */ |
---|
75 | bool OrxonoxShip::initialise() |
---|
76 | { |
---|
77 | // load all the resources needed (no resource groups yet, |
---|
78 | // so the allInit is not executed!) |
---|
79 | // ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
80 | |
---|
81 | // create the "space ship" (currently a fish..) |
---|
82 | // TODO: names must be unique! use static variables.. |
---|
83 | mShip = mSceneMgr->createEntity("Ship", "fish.mesh"); |
---|
84 | SceneNode *fishNode = mRootNode->createChildSceneNode("fishNode"); |
---|
85 | fishNode->yaw(Degree(-90)); |
---|
86 | fishNode->attachObject(mShip); |
---|
87 | fishNode->setScale(Vector3(10, 10, 10)); |
---|
88 | |
---|
89 | return true; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | /** |
---|
94 | * Gets the ship to accelerate in the current direction. |
---|
95 | * The value should be between 0 and 1, with one beeing full thrust and 0 none. |
---|
96 | * @param value Acceleration between 0 and 1 |
---|
97 | */ |
---|
98 | void OrxonoxShip::setThrust(const Real value) |
---|
99 | { |
---|
100 | thrust = value * baseThrust; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /** |
---|
105 | * Gets the ship to accelerate sideways regarding the current direction. |
---|
106 | * The value should be between 0 and 1, with one beeing full thrust and 0 none. |
---|
107 | * @param value Acceleration between 0 and 1 |
---|
108 | */ |
---|
109 | void OrxonoxShip::setSideThrust(const Real value) |
---|
110 | { |
---|
111 | sideThrust = value * baseThrust; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | /** |
---|
116 | * Rotate the ship along with the camera up and down. |
---|
117 | * @param angle Pitch value. |
---|
118 | */ |
---|
119 | void OrxonoxShip::turnUpAndDown(const Radian &angle) |
---|
120 | { |
---|
121 | RootNode_->pitch(angle, Ogre::Node::TransformSpace::TS_LOCAL); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | /** |
---|
126 | * Rotate the ship along with the camera left and right. |
---|
127 | * @param angle Yaw value. |
---|
128 | */ |
---|
129 | void OrxonoxShip::turnLeftAndRight(const Radian &angle) |
---|
130 | { |
---|
131 | RootNode_->yaw(angle, Ogre::Node::TransformSpace::TS_PARENT); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | /** |
---|
136 | * Fire a bullet (Entity with SceneNode). |
---|
137 | * This method creates a new Entity plus a SceneNode. But be sure not make |
---|
138 | * the new Node a child of RootNode_! |
---|
139 | */ |
---|
140 | Bullet* OrxonoxShip::fire() |
---|
141 | { |
---|
142 | // TODO: Names must be unique! |
---|
143 | SceneNode *temp = RootNode_->getParentSceneNode()->createChildSceneNode( |
---|
144 | "BulletNode" + StringConverter::toString(objectCounter_)); |
---|
145 | temp->setOrientation(RootNode_->getOrientation()); |
---|
146 | temp->setPosition(RootNode_->getPosition()); |
---|
147 | temp->setScale(Vector3(1, 1, 1) * 10); |
---|
148 | temp->yaw(Degree(-90)); |
---|
149 | return new Bullet(temp, mSceneMgr->createEntity("bullet" |
---|
150 | + StringConverter::toString(objectCounter_++), "Barrel.mesh"), speed |
---|
151 | + (RootNode_->getOrientation() * Vector3(0, 0, -1)).normalisedCopy() |
---|
152 | * bulletSpeed_); |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | /** |
---|
157 | * Standard tick() function. |
---|
158 | * Currently, only the speed is applied according to the thrust values. |
---|
159 | * @param time Absolute time. |
---|
160 | * @param deltaTime Relative time. |
---|
161 | */ |
---|
162 | bool OrxonoxShip::tick(unsigned long time, Real deltaTime) |
---|
163 | { |
---|
164 | Quaternion quad = mRootNode->getOrientation(); |
---|
165 | quad.normalise(); |
---|
166 | speed += quad * Vector3(0, 0, -1) * currentThrust_ * deltaTime; |
---|
167 | speed += quad * Vector3(-1, 0, 0) * current_SideThrust_ * deltaTime; |
---|
168 | |
---|
169 | RootNode_->translate(currentSpeed_ * deltaTime); |
---|
170 | |
---|
171 | return true; |
---|
172 | } |
---|