Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: extemely stupid aiming, but it works. Had to fix quite a few bugs in the process.

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