Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/audio/src/orxonox_ship.cc @ 349

Last change on this file since 349 was 300, checked in by nicolasc, 17 years ago

initial merge of main_reto

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