Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Slots are now PNodes

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