Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: first element of the GLGui is visible… there is a long way to go…

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