Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: optimized vector class

File size: 7.4 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
[3590]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PLAYER
17
[3471]18#include "player.h"
[3596]19
[3620]20#include "track_manager.h"
[3484]21#include "objModel.h"
[3655]22#include "resource_manager.h"
[4826]23
24#include "weapon_manager.h"
[3620]25#include "test_gun.h"
[4963]26#include "turret.h"
[3620]27#include "world.h"
[3471]28
[3620]29#include "list.h"
30
[4404]31#include "event_handler.h"
[4389]32
[4404]33#include "event.h"
[4287]34
[4780]35
[3471]36using namespace std;
37
[4010]38CREATE_FACTORY(Player);
39
[3471]40/**
[4885]41 * creates a new Player
[4836]42 * @param isFree if the player is free
[3471]43*/
[4762]44Player::Player()
[3471]45{
[4780]46  this->init();
[4597]47
[3620]48  //weapons:
[4955]49  Weapon* wpRight = new TestGun(this->weaponMan, 0);
50  Weapon* wpLeft = new TestGun(this->weaponMan, 1);
[4592]51
[4951]52  this->weaponMan->addWeapon(wpRight);
[4910]53//  this->weaponMan->addWeapon(wpLeft, WM_CONFIG1, WM_SLOT1);
54//  this->weaponMan->addWeapon(wpRight, WM_CONFIG2);
55//  this->weaponMan->addWeapon(wpLeft, WM_CONFIG2);
[3471]56}
57
58/**
[4975]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)
[3471]63{
[4975]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());
[3583]74}
[3566]75
[4010]76/**
[4836]77 *  creates a new Player from Xml Data
78 * @param root the xml element containing player data
[4592]79
[4836]80   @todo add more parameters to load
[4010]81*/
[4780]82Player::Player(const TiXmlElement* root)
[4010]83{
[4780]84  this->init();
85  this->loadParams(root);
[4597]86
[4010]87  //weapons:
[4955]88  Weapon* wpRight = new TestGun(this->weaponMan, 0);
[4953]89  wpRight->setName("testGun Right");
[4955]90  Weapon* wpLeft = new TestGun(this->weaponMan, 1);
[4953]91  wpLeft->setName("testGun Left");
[4592]92
[4963]93  Weapon* turret = new Turret(this->weaponMan);
[4986]94  turret->setName("main-Turret");
[4969]95  Weapon* turret2 = new Turret(this->weaponMan);
[4953]96  this->weaponMan->addWeapon(wpLeft, 1, 0);
97  this->weaponMan->addWeapon(wpRight,1 ,1);
[4967]98  this->weaponMan->addWeapon(turret, 2, 2);
[4969]99  this->weaponMan->addWeapon(turret2, 2, 3);
[4967]100  this->weaponMan->addWeapon(wpLeft, 3, 0);
101  this->weaponMan->addWeapon(wpRight,3 ,1);
[4953]102
103  this->weaponMan->changeWeaponConfig(0);
[4010]104}
[3583]105
[4780]106/**
[4975]107 *  destructs the player, deletes alocated memory
108 */
109Player::~Player ()
110{
111  /* do not delete the weapons, they are contained in the pnode tree
112  and will be deleted there.
113  this only frees the memory allocated to save the list.
114  */
115  delete this->weaponMan;
116}
117
118/**
[4780]119 * initializes a Player
120 */
121void Player::init()
122{
[4997]123  this->setAbsDir(0,1,0);
124
[4780]125  this->setClassID(CL_PLAYER, "Player");
[4404]126
[4990]127  //  this->model = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN);
[4780]128  travelSpeed = 15.0;
129  velocity = new Vector();
130  bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
131  bFire = false;
132  acceleration = 10.0;
[4834]133
134
[4953]135  this->weaponMan = new WeaponManager(this);
[4969]136  this->weaponMan->setSlotCount(4);
[4953]137  this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0));
138  this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0));
[4969]139  this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5));
140  this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
141  this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5));
142  this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
[4780]143}
144
145
[3583]146/**
[4885]147 * loads the Settings of a Player from an XML-element.
[4780]148 * @param root the XML-element to load the Player's properties from
149 */
150void Player::loadParams(const TiXmlElement* root)
151{
152  static_cast<WorldEntity*>(this)->loadParams(root);
153
154
155
156}
157
158
159/**
[4885]160 * adds a weapon to the weapon list of player
[4836]161 * @param weapon to add
[3583]162*/
163void Player::addWeapon(Weapon* weapon)
164{
[3878]165  this->weaponMan->addWeapon(weapon);
[3471]166}
167
[3583]168
[3471]169/**
[4836]170 *  removes a weapon from the player
171 * @param weapon to remove
[3583]172*/
173void Player::removeWeapon(Weapon* weapon)
174{
[3878]175  this->weaponMan->removeWeapon(weapon);
[3583]176}
177
178
179/**
[4836]180 *  effect that occurs after the player is spawned
[3471]181*/
182void Player::postSpawn ()
183{
[3474]184  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
[3471]185}
186
[3584]187
[3471]188/**
[4836]189 *  the action occuring if the player left the game
[3471]190*/
[3584]191void Player::leftWorld ()
192{}
[3471]193
[3584]194
195
[3471]196/**
[4836]197 *  if the player is hit, call this function
198 * @param weapon hit by this weapon
199 * @param loc ??
[3471]200*/
[3578]201void Player::hit (WorldEntity* weapon, Vector* loc)
[3471]202{
203}
204
205
[4592]206/**
[4836]207  *  Collision with another Entity has this effect
208  * @param other the other colider
209  * @param ownhitflags ??
210  * @param otherhitflags ??
[3471]211*/
212void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags)
213{
214}
215
216
217/**
[4836]218 *  draws the player after transforming him.
[3471]219*/
220void Player::draw ()
[4592]221{
[3471]222  glMatrixMode(GL_MODELVIEW);
[3526]223  glPushMatrix();
[3471]224  float matrix[4][4];
[4592]225
[3471]226  /* translate */
[4592]227  glTranslatef (this->getAbsCoor ().x,
228                this->getAbsCoor ().y,
229                this->getAbsCoor ().z);
[3471]230  /* rotate */
231  this->getAbsDir ().matrix (matrix);
232  glMultMatrixf((float*)matrix);
[4592]233
[3471]234  this->model->draw();
[3526]235  glPopMatrix();
[3750]236
[3877]237  this->weaponMan->draw();
[4994]238
239  //this->debug(0);
[3471]240}
241
242
243/**
[4836]244 *  the function called for each passing timeSnap
245 * @param time The timespan passed since last update
[3471]246*/
[3584]247void Player::tick (float time)
[3471]248{
[4885]249  // player controlled movement
250  this->move(time);
[4592]251
[3877]252  this->weaponMan->tick(time);
[3584]253  // weapon system manipulation
[4885]254  this->weaponAction();
[3471]255}
256
[3584]257
[3471]258/**
[4836]259 *  action if player moves
260 * @param time the timeslice since the last frame
[3471]261*/
262void Player::move (float time)
263{
264  Vector accel(0.0, 0.0, 0.0);
265  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
266  /* calculate the direction in which the craft is heading  */
267  Vector direction (1.0, 0.0, 0.0);
268  //direction = this->absDirection.apply (direction);
269  Vector orthDirection (0.0, 0.0, 1.0);
270  //orthDirection = orthDirection.cross (direction);
271
[3966]272  if( this->bUp && this->getRelCoor().x < 20)
[3596]273    accel = accel+(direction*acceleration);
[3966]274  if( this->bDown && this->getRelCoor().x > -5)
[4412]275    accel = accel -(direction*acceleration);
[3966]276  if( this->bLeft &&  TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2)
[4592]277    accel = accel - (orthDirection*acceleration);
[3966]278  if( this->bRight &&  TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2)
[4413]279    accel = accel + (orthDirection*acceleration);
[4885]280  if( this->bAscend ) { /* FIXME */ }
[4836]281  if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */
[3471]282
283  Vector move = accel * time;
[3811]284  this->shiftCoor (move);
[3471]285}
[3584]286
287
288/**
[4885]289 * weapon manipulation by the player
[3584]290*/
[4885]291void Player::weaponAction()
[3584]292{
[3618]293  if( this->bFire)
[3584]294    {
[3875]295      this->weaponMan->fire();
[3584]296    }
297}
298
299/**
[4781]300 * @todo switch statement ??
301 */
[4404]302void Player::process(const Event &event)
303{
[4409]304  if( event.type == KeyMapper::PEV_UP)
305      this->bUp = event.bPressed;
306  else if( event.type == KeyMapper::PEV_DOWN)
307      this->bDown = event.bPressed;
308  else if( event.type == KeyMapper::PEV_RIGHT)
[4413]309      this->bRight= event.bPressed;
[4409]310  else if( event.type == KeyMapper::PEV_LEFT)
[4413]311      this->bLeft = event.bPressed;
[4412]312  else if( event.type == KeyMapper::PEV_FIRE1)
[4885]313      this->bFire = event.bPressed;
[4952]314  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
[4954]315    this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
[4952]316  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
317    this->weaponMan->previousWeaponConfig();
[4404]318}
Note: See TracBrowser for help on using the repository browser.