Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/player.cc @ 5833

Last change on this file since 5833 was 5833, checked in by patrick, 18 years ago

network: server/client orxonox now works

File size: 12.8 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
[5829]42
43
44#define UP    0
45#define DOWN  1
46#define RIGHT 2
47#define LEFT  3
48#define TIME  4
49
50
[3471]51/**
[4885]52 * creates a new Player
[4836]53 * @param isFree if the player is free
[3471]54*/
[4762]55Player::Player()
[3471]56{
[4780]57  this->init();
[3471]58}
59
60/**
[4975]61 * loads a Players information from a specified file.
62 * @param fileName the name of the File to load the player from (absolute path)
63 */
64Player::Player(const char* fileName)
[3471]65{
[4975]66  this->init();
67  TiXmlDocument doc(fileName);
68
69  if(!doc.LoadFile())
70  {
71    PRINTF(2)("Loading file %s failed for player.\n", fileName);
72    return;
73  }
74
75  this->loadParams(doc.RootElement());
[3583]76}
[3566]77
[4010]78/**
[4836]79 *  creates a new Player from Xml Data
80 * @param root the xml element containing player data
[4592]81
[4836]82   @todo add more parameters to load
[4010]83*/
[4780]84Player::Player(const TiXmlElement* root)
[4010]85{
[4780]86  this->init();
[5155]87  if (root != NULL)
88    this->loadParams(root);
[4597]89
[4010]90  //weapons:
[5750]91  Weapon* wpRight = new TestGun(0);
[4953]92  wpRight->setName("testGun Right");
[5750]93  Weapon* wpLeft = new TestGun(1);
[4953]94  wpLeft->setName("testGun Left");
[5750]95  Weapon* cannon = dynamic_cast<Weapon*>(Factory::getFirst()->fabricate(CL_CANNON));
[4592]96
[5750]97  cannon->setName("BFG");
98
[4953]99  this->weaponMan->addWeapon(wpLeft, 1, 0);
100  this->weaponMan->addWeapon(wpRight,1 ,1);
[5750]101  this->weaponMan->addWeapon(cannon, 0, 6);
102
[5041]103  //this->weaponMan->addWeapon(turret, 3, 0);
[4953]104
[5435]105  this->weaponMan->changeWeaponConfig(1);
[4010]106}
[3583]107
[4780]108/**
[4975]109 *  destructs the player, deletes alocated memory
110 */
111Player::~Player ()
112{
113  /* do not delete the weapons, they are contained in the pnode tree
114  and will be deleted there.
115  this only frees the memory allocated to save the list.
116  */
117  delete this->weaponMan;
[5829]118  if( this->outData)
119    delete[] this->outData;
120  if( this->inData)
121    delete[] this->inData;
[4975]122}
123
[5453]124//#include "glgui_pushbutton.h"
[5395]125
[4975]126/**
[4780]127 * initializes a Player
128 */
129void Player::init()
130{
[5037]131//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
[4780]132  this->setClassID(CL_PLAYER, "Player");
[5144]133
[5300]134  PRINTF(4)("PLAYER INIT\n");
[4780]135  travelSpeed = 15.0;
136  bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
137  bFire = false;
138  acceleration = 10.0;
[4834]139
[5453]140//   GLGuiButton* button = new GLGuiPushButton();
141//   button->show();
142//   button->setLabel("orxonox");
143//   button->setBindNode(this);
[4834]144
[4953]145  this->weaponMan = new WeaponManager(this);
[5750]146  this->weaponMan->setSlotCount(7);
[5064]147
[4953]148  this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0));
[5441]149  this->weaponMan->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[5064]150
[4953]151  this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0));
[5441]152  this->weaponMan->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[5064]153
[4969]154  this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5));
155  this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
[5064]156
[4969]157  this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5));
158  this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
[5064]159
160  this->weaponMan->setSlotPosition(4, Vector(-1.5, -.5, .5));
161  this->weaponMan->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
162
163  this->weaponMan->setSlotPosition(5, Vector(-1.5, -.5, -.5));
164  this->weaponMan->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
[5750]165//
166   this->weaponMan->setSlotPosition(6, Vector(-1, 0.0, 0));
167   this->weaponMan->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
168   //
169//   this->weaponMan->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
170//   this->weaponMan->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
171//
172//   this->weaponMan->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
173//   this->weaponMan->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
[5064]174
[5829]175  this->outBufferLength = 100;
176  this->outLength = 0;
177  this->recLength = 0;
178  this->inBufferLength = 100;
179  this->inLength = 0;
180  this->sentLength = 0;
181  this->outData = new byte[this->outBufferLength];
182  this->inData = new byte[this->inBufferLength];
[4780]183}
184
185
[3583]186/**
[4885]187 * loads the Settings of a Player from an XML-element.
[4780]188 * @param root the XML-element to load the Player's properties from
189 */
190void Player::loadParams(const TiXmlElement* root)
191{
192  static_cast<WorldEntity*>(this)->loadParams(root);
193
194
195
196}
197
198/**
[4885]199 * adds a weapon to the weapon list of player
[4836]200 * @param weapon to add
[3583]201*/
202void Player::addWeapon(Weapon* weapon)
203{
[3878]204  this->weaponMan->addWeapon(weapon);
[3471]205}
206
[3583]207
[3471]208/**
[4836]209 *  removes a weapon from the player
210 * @param weapon to remove
[3583]211*/
212void Player::removeWeapon(Weapon* weapon)
213{
[3878]214  this->weaponMan->removeWeapon(weapon);
[3583]215}
216
217
218/**
[4836]219 *  effect that occurs after the player is spawned
[3471]220*/
221void Player::postSpawn ()
222{
[3474]223  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
[3471]224}
225
[3584]226
[3471]227/**
[4836]228 *  the action occuring if the player left the game
[3471]229*/
[3584]230void Player::leftWorld ()
231{}
[3471]232
[3584]233
[5435]234WorldEntity* ref = NULL;
[3471]235/**
[5435]236 *  this function is called, when two entities collide
237 * @param entity: the world entity with whom it collides
238 *
239 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
240 */
241void Player::collidesWith(WorldEntity* entity, const Vector& location)
[3471]242{
[5435]243  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
244  {
245    this->ADDWEAPON();
246    ref = entity;
247    }
248//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
[3471]249}
250
[4592]251/**
[4836]252 *  draws the player after transforming him.
[3471]253*/
[5500]254void Player::draw () const
[4592]255{
[3471]256  glMatrixMode(GL_MODELVIEW);
[3526]257  glPushMatrix();
[3471]258  /* translate */
[4592]259  glTranslatef (this->getAbsCoor ().x,
260                this->getAbsCoor ().y,
261                this->getAbsCoor ().z);
[3471]262  /* rotate */
[4998]263  Vector tmpRot = this->getAbsDir().getSpacialAxis();
264  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[3471]265  this->model->draw();
[3526]266  glPopMatrix();
[3750]267
[3877]268  this->weaponMan->draw();
[4994]269
270  //this->debug(0);
[3471]271}
272
273
274/**
[4836]275 *  the function called for each passing timeSnap
276 * @param time The timespan passed since last update
[3471]277*/
[3584]278void Player::tick (float time)
[3471]279{
[4885]280  // player controlled movement
281  this->move(time);
[4592]282
[3877]283  this->weaponMan->tick(time);
[3584]284  // weapon system manipulation
[4885]285  this->weaponAction();
[3471]286}
287
[3584]288
[3471]289/**
[4836]290 *  action if player moves
291 * @param time the timeslice since the last frame
[3471]292*/
293void Player::move (float time)
294{
295  Vector accel(0.0, 0.0, 0.0);
[5751]296  Vector rot(0.0, 0.0, 0.0);
297  float rotVal = 0.0;
[3471]298  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
299  /* calculate the direction in which the craft is heading  */
300  Vector direction (1.0, 0.0, 0.0);
301  //direction = this->absDirection.apply (direction);
302  Vector orthDirection (0.0, 0.0, 1.0);
303  //orthDirection = orthDirection.cross (direction);
304
[5829]305
306  if( this->outLength >= this->outBufferLength) return;
307
308  if( this->bUp || this->bDown || this->bRight || this->bLeft)
309  {
310    this->outData[this->outLength++] = TIME;
311    this->outData[this->outLength++] = (byte)(lround(time * 100.0f));
312
313    PRINTF(0)("Writing TIME = %i, or %f\n", this->outData[this->outLength-1], time);
314  }
315
[3966]316  if( this->bUp && this->getRelCoor().x < 20)
[5829]317  {
[5751]318    accel += direction;
[5829]319    this->outData[this->outLength++] = UP;
320  }
[3966]321  if( this->bDown && this->getRelCoor().x > -5)
[5829]322  {
[5751]323    accel -= direction;
[5829]324    this->outData[this->outLength++] = DOWN;
325  }
[5483]326  if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2)
[5751]327  {
328    accel -=(orthDirection);
329    rot +=Vector(1,0,0);
330    rotVal -= .4;
[5829]331    this->outData[this->outLength++] = LEFT;
[5751]332  }
[5483]333  if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2)
[5751]334  {
335    accel += orthDirection;
336    rot += Vector(1,0,0);
337    rotVal += .4;
[5829]338    this->outData[this->outLength++] = RIGHT;
[5751]339  }
340  if (this->bAscend )
341  {
342    accel += Vector(0,1,0);
343    rot += Vector(0,0,1);
344    rotVal += .4;
345  }
346  if (this->bDescend )
347  {
348    accel -= Vector(0,1,0);
349    rot += Vector(0,0,1);
350    rotVal -= .4;
351  }
352
[5829]353
[5751]354  Vector move = accel * time *acceleration;
[5483]355
[5751]356/*  if (accel.z < 0)
357    this->setRelDirSoft(Quaternion(-.4, accel), 5);
[5483]358  else if (accel.z > 0)
[5751]359    this->setRelDirSoft(Quaternion(.4, accel), 5);
360  else*/
361  rot.normalize();
362  this->setRelDirSoft(Quaternion(rotVal, rot), 5);
[3811]363  this->shiftCoor (move);
[5829]364
365
[3471]366}
[3584]367
368
369/**
[4885]370 * weapon manipulation by the player
[3584]371*/
[4885]372void Player::weaponAction()
[3584]373{
[3618]374  if( this->bFire)
[3584]375    {
[3875]376      this->weaponMan->fire();
[3584]377    }
378}
379
380/**
[4781]381 * @todo switch statement ??
382 */
[4404]383void Player::process(const Event &event)
384{
[4409]385  if( event.type == KeyMapper::PEV_UP)
386      this->bUp = event.bPressed;
387  else if( event.type == KeyMapper::PEV_DOWN)
388      this->bDown = event.bPressed;
389  else if( event.type == KeyMapper::PEV_RIGHT)
[4413]390      this->bRight= event.bPressed;
[4409]391  else if( event.type == KeyMapper::PEV_LEFT)
[4413]392      this->bLeft = event.bPressed;
[4412]393  else if( event.type == KeyMapper::PEV_FIRE1)
[4885]394      this->bFire = event.bPressed;
[4952]395  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
[4954]396    this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
[4952]397  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
398    this->weaponMan->previousWeaponConfig();
[5751]399
400  else if( event.type == SDLK_PAGEUP)
401    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
402  else if( event.type == SDLK_PAGEDOWN)
403    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
[4404]404}
[5435]405
[5750]406#include "weapons/aiming_turret.h"
[5435]407// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
408void Player::ADDWEAPON()
409{
[5750]410  Weapon* turret = NULL;
[5435]411
[5750]412  if ((float)rand()/RAND_MAX < .1)
413  {
414    //if (this->weaponMan->hasFreeSlot(2, WTYPE_TURRET))
415    {
416      turret = new Turret();
417      this->weaponMan->addWeapon(turret, 2);
418      this->weaponMan->changeWeaponConfig(2);
419    }
420  }
421  else
422  {
423    //if (this->weaponMan->hasFreeSlot(3))
424    {
425      turret = new AimingTurret();
426      this->weaponMan->addWeapon(turret, 3);
[5435]427
[5750]428      this->weaponMan->changeWeaponConfig(3);
429    }
430  }
431
432  if(turret != NULL)
433  {
434    turret->setName("Turret");
435    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
436  }
[5435]437}
[5829]438
439
440
441
442/**
443 *  write data to Synchronizeable
444 */
445void Player::writeBytes(const byte* data, int length)
446{
[5833]447 // PRINTF(0)("Player: got %i bytes of data\n", length);
[5829]448  this->inLength = length;
449
450  /*
451   bytes:  | 0  |  1  |
452           CODE  DIST
453
454
455  CODE:
456       0 :   Up
457       1 :   Down
458       2 :   Right
459       3 :   Left
460       4 :   TIME
461
462  DIST:
463      Coordinate diff multiplied by 100 and casted to a byte: byte a = (byte)(x * 100)
464
465  */
466
467  float time = 0.0f;
468
469  Vector accel(0.0, 0.0, 0.0);
470  Vector direction (1.0, 0.0, 0.0);
471  Vector orthDirection (0.0, 0.0, 1.0);
472
473  byte code = 0;
474
475  /* iterate through all bytes */
476  for( int i = 0; i < length; i++)
477  {
[5833]478   
[5829]479    code = data[i];
480
[5833]481    //PRINTF(0)("got byte: %u\n", data[i]);
[5829]482    /* is it a time code? */
483    if( code == TIME)
484    {
485      /* is it the first time */
[5833]486     
[5829]487      /* read out new movement */
488      time = (float)(data[++i] / 100.0f);
[5833]489      //PRINTF(0)("Got time: %f msec\n", time);
[5829]490    }
[5833]491   
[5829]492    else if( code == UP && this->getRelCoor().x < 20)
493      accel = accel+(direction*acceleration);
494    else if( code == DOWN && this->getRelCoor().x > -5)
495      accel = accel -(direction*acceleration);
496    else if( code == LEFT && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2)
497      accel = accel - (orthDirection*acceleration);
498    else if( code == RIGHT && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2)
499      accel = accel + (orthDirection*acceleration);
[5833]500 
501    if( time > 0.0f )
502    {
503      /* apply movement */
504      Vector move = accel * time;
505
506      if (accel.z < 0)
507        this->setRelDirSoft(Quaternion(-.4, Vector(1,0,0)), 5);
508      else if (accel.z > 0)
509        this->setRelDirSoft(Quaternion(.4, Vector(1,0,0)), 5);
510      else
511        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 5);
512      this->shiftCoor (move);
513    }
514
[5829]515  }
516
[5833]517 
[5829]518
519
520
521  /* and debug output */
522  this->writeDebug();
523}
524
525
526/**
527 *  read data from Synchronizeable
528 */
529int Player::readBytes(byte* data)
530{
[5833]531 // PRINTF(0)("Player: sent %i bytes of data\n", this->sentLength);
[5829]532
533  /* copy data */
534  for( int i = 0; i < this->outLength; ++i)
535    data[i] = this->outData[i];
536
537
538
539  /* debug msg */
540  this->readDebug();
541
542  int length = this->outLength;
543  this->outLength = 0;
544  /* return the length of the test */
545  return length;
546}
547
548
549void Player::writeDebug() const
550{
551
552}
553
554
555void Player::readDebug() const
556{
557
558}
559
Note: See TracBrowser for help on using the repository browser.