Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/player.cc @ 5153

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

orxonox/trunk: one parameter-functions should be executable…. dubugging

File size: 8.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_PLAYER
17
18#include "player.h"
19
20#include "track_manager.h"
21#include "objModel.h"
22#include "resource_manager.h"
23
24#include "weapon_manager.h"
25#include "test_gun.h"
26#include "turret.h"
27#include "world.h"
28
29#include "list.h"
30
31#include "event_handler.h"
32
33#include "event.h"
34
35
36using namespace std;
37
38CREATE_FACTORY(Player);
39
40/**
41 * creates a new Player
42 * @param isFree if the player is free
43*/
44Player::Player()
45{
46  this->init();
47
48  //weapons:
49  Weapon* wpRight = new TestGun(this->weaponMan, 0);
50  Weapon* wpLeft = new TestGun(this->weaponMan, 1);
51
52  this->weaponMan->addWeapon(wpRight);
53//  this->weaponMan->addWeapon(wpLeft, WM_CONFIG1, WM_SLOT1);
54//  this->weaponMan->addWeapon(wpRight, WM_CONFIG2);
55//  this->weaponMan->addWeapon(wpLeft, WM_CONFIG2);
56}
57
58/**
59 * loads a Players information from a specified file.
60 * @param fileName the name of the File to load the player from (absolute path)
61 */
62Player::Player(const char* fileName)
63{
64  this->init();
65  TiXmlDocument doc(fileName);
66
67  if(!doc.LoadFile())
68  {
69    PRINTF(2)("Loading file %s failed for player.\n", fileName);
70    return;
71  }
72
73  this->loadParams(doc.RootElement());
74}
75
76/**
77 *  creates a new Player from Xml Data
78 * @param root the xml element containing player data
79
80   @todo add more parameters to load
81*/
82Player::Player(const TiXmlElement* root)
83{
84  this->init();
85  this->loadParams(root);
86
87  //weapons:
88  Weapon* wpRight = new TestGun(this->weaponMan, 0);
89  wpRight->setName("testGun Right");
90  Weapon* wpLeft = new TestGun(this->weaponMan, 1);
91  wpLeft->setName("testGun Left");
92
93  Weapon* turret1 = new Turret(this->weaponMan);
94  turret1->setName("Turret1");
95  turret1->setStateDuration(WS_SHOOTING, .2);
96  Weapon* turret2 = new Turret(this->weaponMan);
97  turret2->setName("Turret2");
98  turret2->setStateDuration(WS_SHOOTING, .3);
99  Weapon* turret3 = new Turret(this->weaponMan);
100  turret3->setName("Turret3");
101  turret3->setStateDuration(WS_SHOOTING, .17);
102
103  Weapon* turret4 = new Turret(this->weaponMan);
104  turret4->setName("Turret4");
105  turret4->setStateDuration(WS_SHOOTING, .3);
106
107
108  this->weaponMan->addWeapon(wpLeft, 1, 0);
109  this->weaponMan->addWeapon(wpRight,1 ,1);
110  this->weaponMan->addWeapon(turret1, 2, 2);
111  this->weaponMan->addWeapon(turret2, 2, 3);
112  this->weaponMan->addWeapon(turret3, 2, 4);
113  this->weaponMan->addWeapon(turret4, 2, 5);
114
115
116//  ShellCommand<Player>::registerCommand("init", CL_PLAYER, &Player::init);
117  //this->weaponMan->addWeapon(turret, 3, 0);
118
119  this->weaponMan->changeWeaponConfig(0);
120}
121
122/**
123 *  destructs the player, deletes alocated memory
124 */
125Player::~Player ()
126{
127  /* do not delete the weapons, they are contained in the pnode tree
128  and will be deleted there.
129  this only frees the memory allocated to save the list.
130  */
131  delete this->weaponMan;
132}
133
134/**
135 * initializes a Player
136 */
137void Player::init()
138{
139//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
140  this->setClassID(CL_PLAYER, "Player");
141
142  PRINTF(1)("PLAYER INIT\n");
143  travelSpeed = 15.0;
144  velocity = new Vector();
145  bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
146  bFire = false;
147  acceleration = 10.0;
148
149
150  this->weaponMan = new WeaponManager(this);
151  this->weaponMan->setSlotCount(6);
152
153  this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0));
154
155  this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0));
156
157  this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5));
158  this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
159
160  this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5));
161  this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
162
163  this->weaponMan->setSlotPosition(4, Vector(-1.5, -.5, .5));
164  this->weaponMan->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
165
166  this->weaponMan->setSlotPosition(5, Vector(-1.5, -.5, -.5));
167  this->weaponMan->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
168
169}
170
171
172/**
173 * loads the Settings of a Player from an XML-element.
174 * @param root the XML-element to load the Player's properties from
175 */
176void Player::loadParams(const TiXmlElement* root)
177{
178  static_cast<WorldEntity*>(this)->loadParams(root);
179
180
181
182}
183
184/**
185 * adds a weapon to the weapon list of player
186 * @param weapon to add
187*/
188void Player::addWeapon(Weapon* weapon)
189{
190  this->weaponMan->addWeapon(weapon);
191}
192
193
194/**
195 *  removes a weapon from the player
196 * @param weapon to remove
197*/
198void Player::removeWeapon(Weapon* weapon)
199{
200  this->weaponMan->removeWeapon(weapon);
201}
202
203
204/**
205 *  effect that occurs after the player is spawned
206*/
207void Player::postSpawn ()
208{
209  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
210}
211
212
213/**
214 *  the action occuring if the player left the game
215*/
216void Player::leftWorld ()
217{}
218
219
220
221/**
222 *  if the player is hit, call this function
223 * @param weapon hit by this weapon
224 * @param loc ??
225*/
226void Player::hit (WorldEntity* weapon, Vector* loc)
227{
228}
229
230
231/**
232  *  Collision with another Entity has this effect
233  * @param other the other colider
234  * @param ownhitflags ??
235  * @param otherhitflags ??
236*/
237void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags)
238{
239}
240
241
242/**
243 *  draws the player after transforming him.
244*/
245void Player::draw ()
246{
247  glMatrixMode(GL_MODELVIEW);
248  glPushMatrix();
249  /* translate */
250  glTranslatef (this->getAbsCoor ().x,
251                this->getAbsCoor ().y,
252                this->getAbsCoor ().z);
253  /* rotate */
254  Vector tmpRot = this->getAbsDir().getSpacialAxis();
255  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
256  this->model->draw();
257  glPopMatrix();
258
259  this->weaponMan->draw();
260
261  //this->debug(0);
262}
263
264
265/**
266 *  the function called for each passing timeSnap
267 * @param time The timespan passed since last update
268*/
269void Player::tick (float time)
270{
271  // player controlled movement
272  this->move(time);
273
274  this->weaponMan->tick(time);
275  // weapon system manipulation
276  this->weaponAction();
277}
278
279
280/**
281 *  action if player moves
282 * @param time the timeslice since the last frame
283*/
284void Player::move (float time)
285{
286  Vector accel(0.0, 0.0, 0.0);
287  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
288  /* calculate the direction in which the craft is heading  */
289  Vector direction (1.0, 0.0, 0.0);
290  //direction = this->absDirection.apply (direction);
291  Vector orthDirection (0.0, 0.0, 1.0);
292  //orthDirection = orthDirection.cross (direction);
293
294  if( this->bUp && this->getRelCoor().x < 20)
295    accel = accel+(direction*acceleration);
296  if( this->bDown && this->getRelCoor().x > -5)
297    accel = accel -(direction*acceleration);
298  if( this->bLeft &&  TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2)
299    accel = accel - (orthDirection*acceleration);
300  if( this->bRight &&  TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2)
301    accel = accel + (orthDirection*acceleration);
302  if( this->bAscend ) { /* FIXME */ }
303  if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */
304
305  Vector move = accel * time;
306  this->shiftCoor (move);
307}
308
309
310/**
311 * weapon manipulation by the player
312*/
313void Player::weaponAction()
314{
315  if( this->bFire)
316    {
317      this->weaponMan->fire();
318    }
319}
320
321/**
322 * @todo switch statement ??
323 */
324void Player::process(const Event &event)
325{
326  if( event.type == KeyMapper::PEV_UP)
327      this->bUp = event.bPressed;
328  else if( event.type == KeyMapper::PEV_DOWN)
329      this->bDown = event.bPressed;
330  else if( event.type == KeyMapper::PEV_RIGHT)
331      this->bRight= event.bPressed;
332  else if( event.type == KeyMapper::PEV_LEFT)
333      this->bLeft = event.bPressed;
334  else if( event.type == KeyMapper::PEV_FIRE1)
335      this->bFire = event.bPressed;
336  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
337    this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
338  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
339    this->weaponMan->previousWeaponConfig();
340}
Note: See TracBrowser for help on using the repository browser.