Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the mighty useless crosshair…

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