Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the new WeaponManager is online :)

File size: 6.4 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(this, Vector(-2.6, 0.1, 3.0), Quaternion(), 0);
49  Weapon* wpLeft = new TestGun(this, Vector(-2.6, 0.1, -3.0), Quaternion(), 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(this, Vector(-2.6, 0.1, 3.0), Quaternion(), 0);
83  Weapon* wpLeft = new TestGun(this, Vector(-2.6, 0.1, -3.0), Quaternion(), 1);
84
85  this->weaponMan->addWeapon(wpRight, 0,0);
86  this->weaponMan->addWeapon(wpLeft, 0, 1);
87  this->weaponMan->addWeapon(wpLeft, 1, 1);
88  this->weaponMan->addWeapon(wpRight, 2, 0);
89/*  this->weaponMan->addWeapon(wpRight, WM_CONFIG2);
90  this->weaponMan->addWeapon(wpLeft, WM_CONFIG2);*/
91}
92
93/**
94 * initializes a Player
95 */
96void Player::init()
97{
98  this->setClassID(CL_PLAYER, "Player");
99
100//  this->model = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN);
101  travelSpeed = 15.0;
102  velocity = new Vector();
103  bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
104  bFire = false;
105  this->bWeaponChange = false;
106  acceleration = 10.0;
107
108
109  this->weaponMan = new WeaponManager(2);
110}
111
112
113/**
114 * loads the Settings of a Player from an XML-element.
115 * @param root the XML-element to load the Player's properties from
116 */
117void Player::loadParams(const TiXmlElement* root)
118{
119  static_cast<WorldEntity*>(this)->loadParams(root);
120
121
122
123}
124
125
126/**
127 * adds a weapon to the weapon list of player
128 * @param weapon to add
129*/
130void Player::addWeapon(Weapon* weapon)
131{
132  this->weaponMan->addWeapon(weapon);
133}
134
135
136/**
137 *  removes a weapon from the player
138 * @param weapon to remove
139*/
140void Player::removeWeapon(Weapon* weapon)
141{
142  this->weaponMan->removeWeapon(weapon);
143}
144
145
146/**
147 *  effect that occurs after the player is spawned
148*/
149void Player::postSpawn ()
150{
151  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
152}
153
154
155/**
156 *  the action occuring if the player left the game
157*/
158void Player::leftWorld ()
159{}
160
161
162
163/**
164 *  if the player is hit, call this function
165 * @param weapon hit by this weapon
166 * @param loc ??
167*/
168void Player::hit (WorldEntity* weapon, Vector* loc)
169{
170}
171
172
173/**
174  *  Collision with another Entity has this effect
175  * @param other the other colider
176  * @param ownhitflags ??
177  * @param otherhitflags ??
178*/
179void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags)
180{
181}
182
183
184/**
185 *  draws the player after transforming him.
186*/
187void Player::draw ()
188{
189  glMatrixMode(GL_MODELVIEW);
190  glPushMatrix();
191  float matrix[4][4];
192
193  /* translate */
194  glTranslatef (this->getAbsCoor ().x,
195                this->getAbsCoor ().y,
196                this->getAbsCoor ().z);
197  /* rotate */
198  this->getAbsDir ().matrix (matrix);
199  glMultMatrixf((float*)matrix);
200
201  this->model->draw();
202  glPopMatrix();
203
204  this->weaponMan->draw();
205}
206
207
208/**
209 *  the function called for each passing timeSnap
210 * @param time The timespan passed since last update
211*/
212void Player::tick (float time)
213{
214  // player controlled movement
215  this->move(time);
216
217  this->weaponMan->tick(time);
218  // weapon system manipulation
219  this->weaponAction();
220}
221
222
223/**
224 *  action if player moves
225 * @param time the timeslice since the last frame
226*/
227void Player::move (float time)
228{
229  Vector accel(0.0, 0.0, 0.0);
230  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
231  /* calculate the direction in which the craft is heading  */
232  Vector direction (1.0, 0.0, 0.0);
233  //direction = this->absDirection.apply (direction);
234  Vector orthDirection (0.0, 0.0, 1.0);
235  //orthDirection = orthDirection.cross (direction);
236
237  if( this->bUp && this->getRelCoor().x < 20)
238    accel = accel+(direction*acceleration);
239  if( this->bDown && this->getRelCoor().x > -5)
240    accel = accel -(direction*acceleration);
241  if( this->bLeft &&  TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2)
242    accel = accel - (orthDirection*acceleration);
243  if( this->bRight &&  TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2)
244    accel = accel + (orthDirection*acceleration);
245  if( this->bAscend ) { /* FIXME */ }
246  if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */
247
248  Vector move = accel * time;
249  this->shiftCoor (move);
250}
251
252
253/**
254 * weapon manipulation by the player
255*/
256void Player::weaponAction()
257{
258  if( this->bFire)
259    {
260      this->weaponMan->fire();
261    }
262  if( this->bWeaponChange)
263    {
264      this->weaponMan->nextWeaponConf();
265      this->bWeaponChange = false;
266    }
267}
268
269/**
270 * @todo switch statement ??
271 */
272void Player::process(const Event &event)
273{
274  if( event.type == KeyMapper::PEV_UP)
275      this->bUp = event.bPressed;
276  else if( event.type == KeyMapper::PEV_DOWN)
277      this->bDown = event.bPressed;
278  else if( event.type == KeyMapper::PEV_RIGHT)
279      this->bRight= event.bPressed;
280  else if( event.type == KeyMapper::PEV_LEFT)
281      this->bLeft = event.bPressed;
282  else if( event.type == KeyMapper::PEV_FIRE1)
283      this->bFire = event.bPressed;
284  else if( event.type == KeyMapper::PEV_NEXT_WEAPON)
285      if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
286
287}
Note: See TracBrowser for help on using the repository browser.