Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: adapting ShellCommand to use Executor.
On the go

File size: 8.9 KB
RevLine 
[4592]1/*
[3471]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
[5357]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
[3590]17
[5635]18
19#include "executor/executor.h"
[3471]20#include "player.h"
[3596]21
[3620]22#include "track_manager.h"
[3484]23#include "objModel.h"
[3655]24#include "resource_manager.h"
[5355]25#include "factory.h"
[4826]26
[5556]27#include "weapons/weapon_manager.h"
28#include "weapons/test_gun.h"
29#include "weapons/turret.h"
[3471]30
[3620]31#include "list.h"
32
[4404]33#include "event_handler.h"
[4389]34
[4404]35#include "event.h"
[4287]36
[3471]37using namespace std;
38
[4010]39CREATE_FACTORY(Player);
40
[3471]41/**
[4885]42 * creates a new Player
[4836]43 * @param isFree if the player is free
[3471]44*/
[4762]45Player::Player()
[3471]46{
[4780]47  this->init();
[4597]48
[3620]49  //weapons:
[4955]50  Weapon* wpRight = new TestGun(this->weaponMan, 0);
51  Weapon* wpLeft = new TestGun(this->weaponMan, 1);
[4592]52
[4951]53  this->weaponMan->addWeapon(wpRight);
[4910]54//  this->weaponMan->addWeapon(wpLeft, WM_CONFIG1, WM_SLOT1);
55//  this->weaponMan->addWeapon(wpRight, WM_CONFIG2);
56//  this->weaponMan->addWeapon(wpLeft, WM_CONFIG2);
[3471]57}
58
59/**
[4975]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)
[3471]64{
[4975]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());
[3583]75}
[3566]76
[4010]77/**
[4836]78 *  creates a new Player from Xml Data
79 * @param root the xml element containing player data
[4592]80
[4836]81   @todo add more parameters to load
[4010]82*/
[4780]83Player::Player(const TiXmlElement* root)
[4010]84{
[4780]85  this->init();
[5155]86  if (root != NULL)
87    this->loadParams(root);
[4597]88
[4010]89  //weapons:
[4955]90  Weapon* wpRight = new TestGun(this->weaponMan, 0);
[4953]91  wpRight->setName("testGun Right");
[4955]92  Weapon* wpLeft = new TestGun(this->weaponMan, 1);
[4953]93  wpLeft->setName("testGun Left");
[4592]94
[4953]95  this->weaponMan->addWeapon(wpLeft, 1, 0);
96  this->weaponMan->addWeapon(wpRight,1 ,1);
[5041]97  //this->weaponMan->addWeapon(turret, 3, 0);
[4953]98
[5435]99  this->weaponMan->changeWeaponConfig(1);
[4010]100}
[3583]101
[4780]102/**
[4975]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
[5453]114//#include "glgui_pushbutton.h"
[5395]115
[4975]116/**
[4780]117 * initializes a Player
118 */
119void Player::init()
120{
[5037]121//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
[4780]122  this->setClassID(CL_PLAYER, "Player");
[5144]123
[5300]124  PRINTF(4)("PLAYER INIT\n");
[4780]125  travelSpeed = 15.0;
126  bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
127  bFire = false;
128  acceleration = 10.0;
[4834]129
[5453]130//   GLGuiButton* button = new GLGuiPushButton();
131//   button->show();
132//   button->setLabel("orxonox");
133//   button->setBindNode(this);
[4834]134
[4953]135  this->weaponMan = new WeaponManager(this);
[5435]136  this->weaponMan->setSlotCount(10);
[5064]137
[4953]138  this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0));
[5441]139  this->weaponMan->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[5064]140
[4953]141  this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0));
[5441]142  this->weaponMan->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[5064]143
[4969]144  this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5));
145  this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
[5064]146
[4969]147  this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5));
148  this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
[5064]149
150  this->weaponMan->setSlotPosition(4, Vector(-1.5, -.5, .5));
151  this->weaponMan->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
152
153  this->weaponMan->setSlotPosition(5, Vector(-1.5, -.5, -.5));
154  this->weaponMan->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
155
[5435]156  this->weaponMan->setSlotPosition(6, Vector(-2.0, 0.1, -2.0));
157  this->weaponMan->setSlotPosition(7, Vector(-2.0, 0.1, 2.0));
158
159  this->weaponMan->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
160  this->weaponMan->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
161
162  this->weaponMan->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
163  this->weaponMan->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));
[4780]164}
165
166
[3583]167/**
[4885]168 * loads the Settings of a Player from an XML-element.
[4780]169 * @param root the XML-element to load the Player's properties from
170 */
171void Player::loadParams(const TiXmlElement* root)
172{
173  static_cast<WorldEntity*>(this)->loadParams(root);
174
175
176
177}
178
179/**
[4885]180 * adds a weapon to the weapon list of player
[4836]181 * @param weapon to add
[3583]182*/
183void Player::addWeapon(Weapon* weapon)
184{
[3878]185  this->weaponMan->addWeapon(weapon);
[3471]186}
187
[3583]188
[3471]189/**
[4836]190 *  removes a weapon from the player
191 * @param weapon to remove
[3583]192*/
193void Player::removeWeapon(Weapon* weapon)
194{
[3878]195  this->weaponMan->removeWeapon(weapon);
[3583]196}
197
198
199/**
[4836]200 *  effect that occurs after the player is spawned
[3471]201*/
202void Player::postSpawn ()
203{
[3474]204  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
[3471]205}
206
[3584]207
[3471]208/**
[4836]209 *  the action occuring if the player left the game
[3471]210*/
[3584]211void Player::leftWorld ()
212{}
[3471]213
[3584]214
[5435]215WorldEntity* ref = NULL;
[3471]216/**
[5435]217 *  this function is called, when two entities collide
218 * @param entity: the world entity with whom it collides
219 *
220 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
221 */
222void Player::collidesWith(WorldEntity* entity, const Vector& location)
[3471]223{
[5435]224  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
225  {
226    this->ADDWEAPON();
227    ref = entity;
228    }
229//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
[3471]230}
231
[4592]232/**
[4836]233 *  draws the player after transforming him.
[3471]234*/
[5500]235void Player::draw () const
[4592]236{
[3471]237  glMatrixMode(GL_MODELVIEW);
[3526]238  glPushMatrix();
[3471]239  /* translate */
[4592]240  glTranslatef (this->getAbsCoor ().x,
241                this->getAbsCoor ().y,
242                this->getAbsCoor ().z);
[3471]243  /* rotate */
[4998]244  Vector tmpRot = this->getAbsDir().getSpacialAxis();
245  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[3471]246  this->model->draw();
[3526]247  glPopMatrix();
[3750]248
[3877]249  this->weaponMan->draw();
[4994]250
251  //this->debug(0);
[3471]252}
253
254
255/**
[4836]256 *  the function called for each passing timeSnap
257 * @param time The timespan passed since last update
[3471]258*/
[3584]259void Player::tick (float time)
[3471]260{
[4885]261  // player controlled movement
262  this->move(time);
[4592]263
[3877]264  this->weaponMan->tick(time);
[3584]265  // weapon system manipulation
[4885]266  this->weaponAction();
[3471]267}
268
[3584]269
[3471]270/**
[4836]271 *  action if player moves
272 * @param time the timeslice since the last frame
[3471]273*/
274void Player::move (float time)
275{
276  Vector accel(0.0, 0.0, 0.0);
277  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
278  /* calculate the direction in which the craft is heading  */
279  Vector direction (1.0, 0.0, 0.0);
280  //direction = this->absDirection.apply (direction);
281  Vector orthDirection (0.0, 0.0, 1.0);
282  //orthDirection = orthDirection.cross (direction);
283
[3966]284  if( this->bUp && this->getRelCoor().x < 20)
[3596]285    accel = accel+(direction*acceleration);
[3966]286  if( this->bDown && this->getRelCoor().x > -5)
[4412]287    accel = accel -(direction*acceleration);
[5483]288  if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2)
[5482]289      accel = accel - (orthDirection*acceleration);
[5483]290  if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2)
291     accel = accel + (orthDirection*acceleration);
[4885]292  if( this->bAscend ) { /* FIXME */ }
[4836]293  if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */
[3471]294
295  Vector move = accel * time;
[5483]296
297  if (accel.z < 0)
298    this->setRelDirSoft(Quaternion(-.4, Vector(1,0,0)), 5);
299  else if (accel.z > 0)
300    this->setRelDirSoft(Quaternion(.4, Vector(1,0,0)), 5);
301  else
302    this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 5);
[3811]303  this->shiftCoor (move);
[3471]304}
[3584]305
306
307/**
[4885]308 * weapon manipulation by the player
[3584]309*/
[4885]310void Player::weaponAction()
[3584]311{
[3618]312  if( this->bFire)
[3584]313    {
[3875]314      this->weaponMan->fire();
[3584]315    }
316}
317
318/**
[4781]319 * @todo switch statement ??
320 */
[4404]321void Player::process(const Event &event)
322{
[4409]323  if( event.type == KeyMapper::PEV_UP)
324      this->bUp = event.bPressed;
325  else if( event.type == KeyMapper::PEV_DOWN)
326      this->bDown = event.bPressed;
327  else if( event.type == KeyMapper::PEV_RIGHT)
[4413]328      this->bRight= event.bPressed;
[4409]329  else if( event.type == KeyMapper::PEV_LEFT)
[4413]330      this->bLeft = event.bPressed;
[4412]331  else if( event.type == KeyMapper::PEV_FIRE1)
[4885]332      this->bFire = event.bPressed;
[4952]333  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
[4954]334    this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
[4952]335  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
336    this->weaponMan->previousWeaponConfig();
[4404]337}
[5435]338
339// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
340void Player::ADDWEAPON()
341{
342  Weapon* turret1 = new Turret(this->weaponMan);
343  turret1->setName("Turret");
344  turret1->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
345
[5440]346  this->weaponMan->addWeapon(turret1, 2);
[5435]347
348  this->weaponMan->changeWeaponConfig(2);
349}
Note: See TracBrowser for help on using the repository browser.