Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4433 was 4414, checked in by patrick, 19 years ago

orxonox/trunk: connected the viewport change functions in the camera system with the event handling system - works great

File size: 7.8 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_PLAYER
17
18#include "player.h"
19
20#include "track_manager.h"
21#include "objModel.h"
22#include "resource_manager.h"
23#include "weapon.h"
24#include "test_gun.h"
25#include "world.h"
26
27#include "list.h"
28#include "stdincl.h"
29
30#include "event_handler.h"
31
32#include "projectile.h"
33#include "event.h"
34
35using namespace std;
36
37CREATE_FACTORY(Player);
38
39/**
40   \brief creates a new Player
41   \param isFree if the player is free
42*/
43Player::Player() : WorldEntity(), PhysicsInterface(this)
44{
45  /*
46    this is the debug player - actualy we would have to make a new
47     class derivated from Player for each player. for now, we just use
48     the player.cc for debug also
49  */
50  this->setClassName("Player");
51  this->model = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN);
52  travelSpeed = 15.0;
53  velocity = new Vector();
54  bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
55  bFire = false;
56  this->bWeaponChange = false;
57  acceleration = 10.0;
58  //weapons:
59  this->weaponMan = new WeaponManager();
60  Weapon* wpRight = new TestGun(this, Vector(-2.6, 0.1, 3.0), Quaternion(), 0);
61  Weapon* wpLeft = new TestGun(this, Vector(-2.6, 0.1, -3.0), Quaternion(), 1);
62 
63  this->weaponMan->addWeapon(wpRight, W_CONFIG0, W_SLOT0);
64  this->weaponMan->addWeapon(wpLeft, W_CONFIG1, W_SLOT1);
65  this->weaponMan->addWeapon(wpRight, W_CONFIG2);
66  this->weaponMan->addWeapon(wpLeft, W_CONFIG2);
67}
68
69
70/**
71   \brief destructs the player, deletes alocated memory
72*/
73Player::~Player ()
74{
75  /* do not delete the weapons, they are contained in the pnode tree
76     and will be deleted there.
77     this only frees the memory allocated to save the list.
78  */
79  delete this->weaponMan;
80}
81
82
83/**
84   \brief creates a new Player from Xml Data
85   \param root the xml element containing player data
86   
87   \todo add more parameters to load
88*/
89Player::Player(const TiXmlElement* root) : WorldEntity(root), PhysicsInterface(this)
90{
91  this->setClassName("Player");
92  this->weapons = new tList<Weapon>();
93  this->activeWeapon = NULL;
94  /*
95    this is the debug player - actualy we would have to make a new
96     class derivated from Player for each player. for now, we just use
97     the player.cc for debug also
98  */
99  travelSpeed = 15.0;
100  velocity = new Vector();
101  bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
102  bFire = false;
103  this->bWeaponChange = false;
104  acceleration = 10.0;
105  //weapons:
106  this->weaponMan = new WeaponManager();
107  Weapon* wpRight = new TestGun(this, Vector(-2.6, 0.1, 3.0), Quaternion(), 0);
108  Weapon* wpLeft = new TestGun(this, Vector(-2.6, 0.1, -3.0), Quaternion(), 1);
109 
110  this->weaponMan->addWeapon(wpRight, W_CONFIG0, W_SLOT0);
111  this->weaponMan->addWeapon(wpLeft, W_CONFIG1, W_SLOT1);
112  this->weaponMan->addWeapon(wpRight, W_CONFIG2);
113  this->weaponMan->addWeapon(wpLeft, W_CONFIG2);
114}
115
116
117/**
118   \brief adds a weapon to the weapon list of player
119   \param weapon to add
120*/
121void Player::addWeapon(Weapon* weapon)
122{
123  this->weaponMan->addWeapon(weapon);
124}
125
126
127/**
128   \brief removes a weapon from the player
129   \param weapon to remove
130*/
131void Player::removeWeapon(Weapon* weapon)
132{
133  this->weaponMan->removeWeapon(weapon);
134}
135
136
137/**
138   \brief effect that occurs after the player is spawned
139*/
140void Player::postSpawn ()
141{
142  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
143}
144
145
146/**
147   \brief the action occuring if the player left the game
148*/
149void Player::leftWorld ()
150{}
151
152
153
154/**
155   \brief if the player is hit, call this function
156   \param weapon hit by this weapon
157   \param loc ??
158*/
159void Player::hit (WorldEntity* weapon, Vector* loc)
160{
161}
162
163
164/**
165    \brief Collision with another Entity has this effect
166    \param other the other colider
167    \param ownhitflags ??
168    \param otherhitflags ??
169*/
170void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags)
171{
172}
173
174
175/**
176   \brief draws the player after transforming him.
177*/
178void Player::draw ()
179{ 
180  glMatrixMode(GL_MODELVIEW);
181  glPushMatrix();
182  float matrix[4][4];
183 
184  /* translate */
185  glTranslatef (this->getAbsCoor ().x, 
186                this->getAbsCoor ().y, 
187                this->getAbsCoor ().z);
188  /* rotate */
189  this->getAbsDir ().matrix (matrix);
190  glMultMatrixf((float*)matrix);
191 
192  this->model->draw();
193  glPopMatrix();
194
195  this->weaponMan->draw();
196}
197
198
199/**
200   \brief the function called for each passing timeSnap
201   \param time The timespan passed since last update
202*/
203void Player::tick (float time)
204{
205  //printf("%p\n", this);
206  //this->getRelCoor().debug();
207 
208  /* link tick to weapon */
209  //this->activeWeapon->tick(time);
210  //this->activeWeaponL->tick(time); //FIX FIX DELETE REMOVE
211  this->weaponMan->tick(time);
212  // player controlled movement
213  this->move(time);
214  // weapon system manipulation
215  this->weapon();
216}
217
218
219/**
220   \brief action if player moves
221   \param time the timeslice since the last frame
222*/
223void Player::move (float time)
224{
225  Vector accel(0.0, 0.0, 0.0);
226  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
227  /* calculate the direction in which the craft is heading  */
228  Vector direction (1.0, 0.0, 0.0);
229  //direction = this->absDirection.apply (direction);
230  Vector orthDirection (0.0, 0.0, 1.0);
231  //orthDirection = orthDirection.cross (direction);
232
233  if( this->bUp && this->getRelCoor().x < 20)
234    accel = accel+(direction*acceleration);
235  if( this->bDown && this->getRelCoor().x > -5)
236    accel = accel -(direction*acceleration);
237  if( this->bLeft &&  TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2)
238    accel = accel - (orthDirection*acceleration); 
239  if( this->bRight &&  TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2)
240    accel = accel + (orthDirection*acceleration);
241  if( this->bAscend )
242  if( this->bDescend) {/* FIXME */} /* \todo up and down player movement */
243
244  Vector move = accel * time;
245  this->shiftCoor (move);
246}
247
248
249/**
250   \brief weapon manipulation by the player
251*/
252void Player::weapon()
253{
254  if( this->bFire)
255    {
256      this->weaponMan->fire();
257    }
258  if( this->bWeaponChange)
259    {
260      this->weaponMan->nextWeaponConf();
261      this->bWeaponChange = false;
262    }
263}
264
265
266/**
267   \brief The connection to the command node
268   \param cmd the Command unit from witch to map
269
270   here the commands are mapped to the players movement/weaponary
271*/
272void Player::command (Command* cmd)
273{
274  PRINTF(3)("recieved command [%s]\n", cmd->cmd);
275  if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_UP)) this->bUp = !cmd->bUp;
276  if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_DOWN)) this->bDown = !cmd->bUp;
277  if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_LEFT)) this->bLeft = !cmd->bUp;
278  if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_RIGHT)) this->bRight = !cmd->bUp;
279  if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_FIRE)) this->bFire = !cmd->bUp;
280  if( !strcmp( cmd->cmd, CONFIG_NAME_PLAYER_NEXT_WEAPON)) if(cmd->bUp) this->bWeaponChange = !this->bWeaponChange;
281}
282
283
284void Player::process(const Event &event)
285{
286  if( event.type == KeyMapper::PEV_UP)
287    {
288      this->bUp = event.bPressed;
289    }
290  else if( event.type == KeyMapper::PEV_DOWN)
291    {
292      this->bDown = event.bPressed;
293    }
294  else if( event.type == KeyMapper::PEV_RIGHT)
295    {
296      this->bRight= event.bPressed;
297    }
298  else if( event.type == KeyMapper::PEV_LEFT)
299    {
300      this->bLeft = event.bPressed;
301    }
302  else if( event.type == KeyMapper::PEV_FIRE1)
303    {
304       this->bFire = event.bPressed;
305    }
306  else if( event.type == KeyMapper::PEV_NEXT_WEAPON)
307    {
308      if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
309    }
310
311}
Note: See TracBrowser for help on using the repository browser.