Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/main_reto_vs05/src/orxonox_ship.cc @ 194

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