Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/old.we/src/world_entities/player.cc @ 5839

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

orxonox/trunk: removed WeaponManager from all the Weapons, because weapon should not know where it is connected to

File size: 9.1 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, CL_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(0);
82  wpRight->setName("testGun Right");
83  Weapon* wpLeft = new TestGun(1);
84  wpLeft->setName("testGun Left");
85  Weapon* cannon = dynamic_cast<Weapon*>(Factory::getFirst()->fabricate(CL_CANNON));
86
87  cannon->setName("BFG");
88
89  this->weaponMan->addWeapon(wpLeft, 1, 0);
90  this->weaponMan->addWeapon(wpRight,1 ,1);
91  this->weaponMan->addWeapon(cannon, 0, 6);
92
93  //this->weaponMan->addWeapon(turret, 3, 0);
94
95  this->weaponMan->changeWeaponConfig(1);
96}
97
98/**
99 *  destructs the player, deletes alocated memory
100 */
101Player::~Player ()
102{
103  /* do not delete the weapons, they are contained in the pnode tree
104  and will be deleted there.
105  this only frees the memory allocated to save the list.
106  */
107  delete this->weaponMan;
108}
109
110//#include "glgui_pushbutton.h"
111
112/**
113 * initializes a Player
114 */
115void Player::init()
116{
117//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
118  this->setClassID(CL_PLAYER, "Player");
119
120  PRINTF(4)("PLAYER INIT\n");
121  travelSpeed = 15.0;
122  bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
123  bFire = false;
124  acceleration = 10.0;
125
126//   GLGuiButton* button = new GLGuiPushButton();
127//   button->show();
128//   button->setLabel("orxonox");
129//   button->setBindNode(this);
130
131  this->weaponMan = new WeaponManager(this);
132  this->weaponMan->setSlotCount(7);
133
134  this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0));
135  this->weaponMan->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
136
137  this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0));
138  this->weaponMan->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
139
140  this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5));
141  this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
142
143  this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5));
144  this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
145
146  this->weaponMan->setSlotPosition(4, Vector(-1.5, -.5, .5));
147  this->weaponMan->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
148
149  this->weaponMan->setSlotPosition(5, Vector(-1.5, -.5, -.5));
150  this->weaponMan->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
151//
152   this->weaponMan->setSlotPosition(6, Vector(-1, 0.0, 0));
153   this->weaponMan->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
154   //
155//   this->weaponMan->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
156//   this->weaponMan->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
157//
158//   this->weaponMan->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
159//   this->weaponMan->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
160
161}
162
163
164/**
165 * loads the Settings of a Player from an XML-element.
166 * @param root the XML-element to load the Player's properties from
167 */
168void Player::loadParams(const TiXmlElement* root)
169{
170  static_cast<WorldEntity*>(this)->loadParams(root);
171
172
173
174}
175
176/**
177 * adds a weapon to the weapon list of player
178 * @param weapon to add
179*/
180void Player::addWeapon(Weapon* weapon)
181{
182  this->weaponMan->addWeapon(weapon);
183}
184
185
186/**
187 *  removes a weapon from the player
188 * @param weapon to remove
189*/
190void Player::removeWeapon(Weapon* weapon)
191{
192  this->weaponMan->removeWeapon(weapon);
193}
194
195
196/**
197 *  effect that occurs after the player is spawned
198*/
199void Player::postSpawn ()
200{
201  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
202}
203
204
205/**
206 *  the action occuring if the player left the game
207*/
208void Player::leftWorld ()
209{}
210
211
212WorldEntity* ref = NULL;
213/**
214 *  this function is called, when two entities collide
215 * @param entity: the world entity with whom it collides
216 *
217 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
218 */
219void Player::collidesWith(WorldEntity* entity, const Vector& location)
220{
221  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
222  {
223    this->ADDWEAPON();
224    ref = entity;
225    }
226//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
227}
228
229/**
230 *  draws the player after transforming him.
231*/
232void Player::draw () const
233{
234  glMatrixMode(GL_MODELVIEW);
235  glPushMatrix();
236  /* translate */
237  glTranslatef (this->getAbsCoor ().x,
238                this->getAbsCoor ().y,
239                this->getAbsCoor ().z);
240  /* rotate */
241  Vector tmpRot = this->getAbsDir().getSpacialAxis();
242  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
243  this->model->draw();
244  glPopMatrix();
245
246  this->weaponMan->draw();
247
248  //this->debug(0);
249}
250
251
252/**
253 *  the function called for each passing timeSnap
254 * @param time The timespan passed since last update
255*/
256void Player::tick (float time)
257{
258  // player controlled movement
259  this->move(time);
260
261  this->weaponMan->tick(time);
262  // weapon system manipulation
263  this->weaponAction();
264}
265
266
267/**
268 *  action if player moves
269 * @param time the timeslice since the last frame
270*/
271void Player::move (float time)
272{
273  Vector accel(0.0, 0.0, 0.0);
274  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
275  /* calculate the direction in which the craft is heading  */
276  Vector direction (1.0, 0.0, 0.0);
277  //direction = this->absDirection.apply (direction);
278  Vector orthDirection (0.0, 0.0, 1.0);
279  //orthDirection = orthDirection.cross (direction);
280
281  if( this->bUp && this->getRelCoor().x < 20)
282    accel = accel+(direction*acceleration);
283  if( this->bDown && this->getRelCoor().x > -5)
284    accel = accel -(direction*acceleration);
285  if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2)
286      accel = accel - (orthDirection*acceleration);
287  if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2)
288     accel = accel + (orthDirection*acceleration);
289  if( this->bAscend ) { /* FIXME */ }
290  if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */
291
292  Vector move = accel * time;
293
294  if (accel.z < 0)
295    this->setRelDirSoft(Quaternion(-.4, Vector(1,0,0)), 5);
296  else if (accel.z > 0)
297    this->setRelDirSoft(Quaternion(.4, Vector(1,0,0)), 5);
298  else
299    this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 5);
300  this->shiftCoor (move);
301}
302
303
304/**
305 * weapon manipulation by the player
306*/
307void Player::weaponAction()
308{
309  if( this->bFire)
310    {
311      this->weaponMan->fire();
312    }
313}
314
315/**
316 * @todo switch statement ??
317 */
318void Player::process(const Event &event)
319{
320  if( event.type == KeyMapper::PEV_UP)
321      this->bUp = event.bPressed;
322  else if( event.type == KeyMapper::PEV_DOWN)
323      this->bDown = event.bPressed;
324  else if( event.type == KeyMapper::PEV_RIGHT)
325      this->bRight= event.bPressed;
326  else if( event.type == KeyMapper::PEV_LEFT)
327      this->bLeft = event.bPressed;
328  else if( event.type == KeyMapper::PEV_FIRE1)
329      this->bFire = event.bPressed;
330  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
331    this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
332  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
333    this->weaponMan->previousWeaponConfig();
334}
335
336#include "weapons/aiming_turret.h"
337// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
338void Player::ADDWEAPON()
339{
340  Weapon* turret = NULL;
341
342  if ((float)rand()/RAND_MAX < .1)
343  {
344    //if (this->weaponMan->hasFreeSlot(2, WTYPE_TURRET))
345    {
346      turret = new Turret();
347      this->weaponMan->addWeapon(turret, 2);
348      this->weaponMan->changeWeaponConfig(2);
349    }
350  }
351  else
352  {
353    //if (this->weaponMan->hasFreeSlot(3))
354    {
355      turret = new AimingTurret();
356      this->weaponMan->addWeapon(turret, 3);
357
358      this->weaponMan->changeWeaponConfig(3);
359    }
360  }
361
362  if(turret != NULL)
363  {
364    turret->setName("Turret");
365    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
366  }
367}
Note: See TracBrowser for help on using the repository browser.