Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5635 was 5635, checked in by bensch, 18 years ago

orxonox/trunk: more about the executor.

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