Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/world_entities/src/world_entities/player.cc @ 5621

Last change on this file since 5621 was 5621, checked in by bensch, 19 years ago

orxonox/branches/world_entities: added new Class Weapon, that will once fire BOMBS

File size: 9.2 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
13   co-programmer: Christian Meyer
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
18#include "player.h"
19
20#include "track_manager.h"
21#include "objModel.h"
22#include "resource_manager.h"
23#include "factory.h"
24
25#include "weapons/weapon_manager.h"
26#include "weapons/test_gun.h"
27#include "weapons/turret.h"
28#include "weapons/cannon.h"
29
30#include "list.h"
31
32#include "event_handler.h"
33
34#include "event.h"
35
36
37using namespace std;
38
39CREATE_FACTORY(Player);
40
41/**
42 * creates a new Player
43 * @param isFree if the player is free
44*/
45Player::Player()
46{
47  this->init();
48}
49
50/**
51 * loads a Players information from a specified file.
52 * @param fileName the name of the File to load the player from (absolute path)
53 */
54Player::Player(const char* fileName)
55{
56  this->init();
57  TiXmlDocument doc(fileName);
58
59  if(!doc.LoadFile())
60  {
61    PRINTF(2)("Loading file %s failed for player.\n", fileName);
62    return;
63  }
64
65  this->loadParams(doc.RootElement());
66}
67
68/**
69 *  creates a new Player from Xml Data
70 * @param root the xml element containing player data
71
72   @todo add more parameters to load
73*/
74Player::Player(const TiXmlElement* root)
75{
76  this->init();
77  if (root != NULL)
78    this->loadParams(root);
79
80  //weapons:
81  Weapon* wpRight = new TestGun(this->weaponMan, 0);
82  wpRight->setName("testGun Right");
83  Weapon* wpLeft = new TestGun(this->weaponMan, 1);
84  wpLeft->setName("testGun Left");
85  Weapon* cannon = new Cannon(this->weaponMan);
86  cannon->setName("BFG");
87
88  this->weaponMan->addWeapon(wpLeft, 1, 0);
89  this->weaponMan->addWeapon(wpRight,1 ,1);
90  this->weaponMan->addWeapon(cannon, 0, 6);
91
92  //this->weaponMan->addWeapon(turret, 3, 0);
93
94  this->weaponMan->changeWeaponConfig(1);
95}
96
97/**
98 *  destructs the player, deletes alocated memory
99 */
100Player::~Player ()
101{
102  /* do not delete the weapons, they are contained in the pnode tree
103  and will be deleted there.
104  this only frees the memory allocated to save the list.
105  */
106  delete this->weaponMan;
107}
108
109//#include "glgui_pushbutton.h"
110
111/**
112 * initializes a Player
113 */
114void Player::init()
115{
116//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
117  this->setClassID(CL_PLAYER, "Player");
118
119  PRINTF(4)("PLAYER INIT\n");
120  travelSpeed = 15.0;
121  bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
122  bFire = false;
123  acceleration = 10.0;
124
125//   GLGuiButton* button = new GLGuiPushButton();
126//   button->show();
127//   button->setLabel("orxonox");
128//   button->setBindNode(this);
129
130  this->weaponMan = new WeaponManager(this);
131  this->weaponMan->setSlotCount(7);
132
133  this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0));
134  this->weaponMan->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
135
136  this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0));
137  this->weaponMan->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
138
139  this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5));
140  this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
141
142  this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5));
143  this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
144
145  this->weaponMan->setSlotPosition(4, Vector(-1.5, -.5, .5));
146  this->weaponMan->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
147
148  this->weaponMan->setSlotPosition(5, Vector(-1.5, -.5, -.5));
149  this->weaponMan->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
150//
151   this->weaponMan->setSlotPosition(6, Vector(-1, 0.0, 0));
152   this->weaponMan->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
153   //
154//   this->weaponMan->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
155//   this->weaponMan->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
156//
157//   this->weaponMan->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
158//   this->weaponMan->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
159
160}
161
162
163/**
164 * loads the Settings of a Player from an XML-element.
165 * @param root the XML-element to load the Player's properties from
166 */
167void Player::loadParams(const TiXmlElement* root)
168{
169  static_cast<WorldEntity*>(this)->loadParams(root);
170
171
172
173}
174
175/**
176 * adds a weapon to the weapon list of player
177 * @param weapon to add
178*/
179void Player::addWeapon(Weapon* weapon)
180{
181  this->weaponMan->addWeapon(weapon);
182}
183
184
185/**
186 *  removes a weapon from the player
187 * @param weapon to remove
188*/
189void Player::removeWeapon(Weapon* weapon)
190{
191  this->weaponMan->removeWeapon(weapon);
192}
193
194
195/**
196 *  effect that occurs after the player is spawned
197*/
198void Player::postSpawn ()
199{
200  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
201}
202
203
204/**
205 *  the action occuring if the player left the game
206*/
207void Player::leftWorld ()
208{}
209
210
211WorldEntity* ref = NULL;
212/**
213 *  this function is called, when two entities collide
214 * @param entity: the world entity with whom it collides
215 *
216 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
217 */
218void Player::collidesWith(WorldEntity* entity, const Vector& location)
219{
220  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
221  {
222    this->ADDWEAPON();
223    ref = entity;
224    }
225//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
226}
227
228/**
229 *  draws the player after transforming him.
230*/
231void Player::draw () const
232{
233  glMatrixMode(GL_MODELVIEW);
234  glPushMatrix();
235  /* translate */
236  glTranslatef (this->getAbsCoor ().x,
237                this->getAbsCoor ().y,
238                this->getAbsCoor ().z);
239  /* rotate */
240  Vector tmpRot = this->getAbsDir().getSpacialAxis();
241  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
242  this->model->draw();
243  glPopMatrix();
244
245  this->weaponMan->draw();
246
247  //this->debug(0);
248}
249
250
251/**
252 *  the function called for each passing timeSnap
253 * @param time The timespan passed since last update
254*/
255void Player::tick (float time)
256{
257  // player controlled movement
258  this->move(time);
259
260  this->weaponMan->tick(time);
261  // weapon system manipulation
262  this->weaponAction();
263}
264
265
266/**
267 *  action if player moves
268 * @param time the timeslice since the last frame
269*/
270void Player::move (float time)
271{
272  Vector accel(0.0, 0.0, 0.0);
273  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
274  /* calculate the direction in which the craft is heading  */
275  Vector direction (1.0, 0.0, 0.0);
276  //direction = this->absDirection.apply (direction);
277  Vector orthDirection (0.0, 0.0, 1.0);
278  //orthDirection = orthDirection.cross (direction);
279
280  if( this->bUp && this->getRelCoor().x < 20)
281    accel = accel+(direction*acceleration);
282  if( this->bDown && this->getRelCoor().x > -5)
283    accel = accel -(direction*acceleration);
284  if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2)
285      accel = accel - (orthDirection*acceleration);
286  if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2)
287     accel = accel + (orthDirection*acceleration);
288  if( this->bAscend ) { /* FIXME */ }
289  if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */
290
291  Vector move = accel * time;
292
293  if (accel.z < 0)
294    this->setRelDirSoft(Quaternion(-.4, Vector(1,0,0)), 5);
295  else if (accel.z > 0)
296    this->setRelDirSoft(Quaternion(.4, Vector(1,0,0)), 5);
297  else
298    this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 5);
299  this->shiftCoor (move);
300}
301
302
303/**
304 * weapon manipulation by the player
305*/
306void Player::weaponAction()
307{
308  if( this->bFire)
309    {
310      this->weaponMan->fire();
311    }
312}
313
314/**
315 * @todo switch statement ??
316 */
317void Player::process(const Event &event)
318{
319  if( event.type == KeyMapper::PEV_UP)
320      this->bUp = event.bPressed;
321  else if( event.type == KeyMapper::PEV_DOWN)
322      this->bDown = event.bPressed;
323  else if( event.type == KeyMapper::PEV_RIGHT)
324      this->bRight= event.bPressed;
325  else if( event.type == KeyMapper::PEV_LEFT)
326      this->bLeft = event.bPressed;
327  else if( event.type == KeyMapper::PEV_FIRE1)
328      this->bFire = event.bPressed;
329  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
330    this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
331  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
332    this->weaponMan->previousWeaponConfig();
333}
334
335#include "weapons/aiming_turret.h"
336// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
337void Player::ADDWEAPON()
338{
339  Weapon* turret = NULL;
340
341  if ((float)rand()/RAND_MAX < .1)
342  {
343    //if (this->weaponMan->hasFreeSlot(2, WTYPE_TURRET))
344    {
345      turret = new Turret(this->weaponMan);
346      this->weaponMan->addWeapon(turret, 2);
347      this->weaponMan->changeWeaponConfig(2);
348    }
349  }
350  else
351  {
352    //if (this->weaponMan->hasFreeSlot(3))
353    {
354      turret = new AimingTurret(this->weaponMan);
355      this->weaponMan->addWeapon(turret, 3);
356
357      this->weaponMan->changeWeaponConfig(3);
358    }
359  }
360
361  if(turret != NULL)
362  {
363    turret->setName("Turret");
364    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
365  }
366}
Note: See TracBrowser for help on using the repository browser.