Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5750 was 5750, checked in by bensch, 20 years ago

orxonox/trunk: merged the WorldEntities into the Trunk.
Merged with command:
svn merge branches/world_entities/ trunk/ -r5516:HEAD

conflics from world_entities changed in favor of branches/world_entity
all other conflict in favor of the trunk

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