Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/creatures/fps_player.cc @ 8844

Last change on this file since 8844 was 8844, checked in by bensch, 18 years ago

deleting weapons should be fixed

File size: 7.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: ...
14
15*/
16
17#include "fps_player.h"
18
19#include "interactive_model.h"
20#include "state.h"
21
22#include "src/lib/util/loading/factory.h"
23
24#include "weapons/weapon_manager.h"
25#include "weapons/test_gun.h"
26#include "weapons/turret.h"
27#include "weapons/cannon.h"
28
29#include "key_mapper.h"
30
31#include "debug.h"
32
33
34
35
36CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER);
37
38
39/**
40 *  destructs the FPSPlayer, deletes alocated memory
41 */
42FPSPlayer::~FPSPlayer ()
43{
44  this->setPlayer(NULL);
45}
46
47
48/**
49 *  creates a new FPSPlayer from Xml Data
50 * @param root the xml element containing FPSPlayer data
51 *
52 */
53FPSPlayer::FPSPlayer(const TiXmlElement* root)
54{
55  this->init();
56
57  if (root != NULL)
58    this->loadParams(root);
59
60}
61
62
63/**
64 * initializes a FPSPlayer
65 */
66void FPSPlayer::init()
67{
68  this->setClassID(CL_FPS_PLAYER, "FPSPlayer");
69
70
71  this->bLeft = false;
72  this->bRight = false;
73  this->bForward = false;
74  this->bBackward = false;
75  this->bJump = false;
76
77  this->xMouse = 0.0f;
78  this->yMouse = 0.0f;
79
80  this->setHealthMax(100);
81  this->setHealth(80);
82
83
84  this->cameraNode.setParent(this);
85
86  this->attitude = this->getAbsDir().getAttitude();
87  this->heading = this->getAbsDir().getHeading();
88
89  //add events to the eventlist
90  registerEvent(KeyMapper::PEV_FORWARD);
91  registerEvent(KeyMapper::PEV_BACKWARD);
92  registerEvent(KeyMapper::PEV_LEFT);
93  registerEvent(KeyMapper::PEV_RIGHT);
94  registerEvent(KeyMapper::PEV_FIRE1);
95  registerEvent(KeyMapper::PEV_JUMP);
96  registerEvent(EV_MOUSE_MOTION);
97
98
99
100  // weapon manager for the fps
101  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
102
103  Weapon* wpRight = new TestGun(0);
104  wpRight->setName("testGun Right");
105  Weapon* wpLeft = new TestGun(1);
106//   Weapon* wpLeft = new Turret();
107  wpLeft->setName("testGun Left");
108
109  this->addWeapon(wpLeft, 1, 0);
110  this->addWeapon(wpRight,1 ,1);
111  this->getWeaponManager().changeWeaponConfig(1);
112
113  this->getWeaponManager().setSlotCount(2);
114  this->getWeaponManager().setSlotPosition(0, Vector(-0.5, .2, -1.9));
115  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
116  this->getWeaponManager().setSlotPosition(1, Vector(-0.5, .2, 1.9));
117  this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0)));
118
119  this->getWeaponManager().setParentNode(&this->cameraNode);
120  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
121  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
122
123
124  // network registration
125  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
126  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
127  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
128  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
129//  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) );
130
131
132  // collision reaction registration
133  this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY);
134}
135
136
137/**
138 * loads the Settings of a FPSPlayer from an XML-element.
139 * @param root the XML-element to load the Spaceship's properties from
140 */
141void FPSPlayer::loadParams(const TiXmlElement* root)
142{
143  Playable::loadParams(root);
144}
145
146void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
147{
148  this->attitude = this->getAbsDir().getAttitude();
149  this->heading = this->getAbsDir().getHeading();
150}
151
152
153void FPSPlayer::reset()
154{
155  this->bLeft = false;
156  this->bRight = false;
157  this->bForward = false;
158  this->bBackward = false;
159  this->xMouse = 0.0f;
160  this->yMouse = 0.0f;
161
162  this->setHealth(80);
163}
164
165
166void FPSPlayer::enter()
167{
168  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
169
170  State::getCameraNode()->setParentSoft(&this->cameraNode);
171  State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
172
173  this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode());
174  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
175
176
177  State::getCameraNode()->setRelCoor(0,0,0);
178  State::getCameraTargetNode()->setRelCoor(10,0,0);
179}
180
181void FPSPlayer::leave()
182{
183  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
184  this->detachCamera();
185}
186
187
188
189/**
190 *  the function called for each passing timeSnap
191 * @param time The timespan passed since last update
192 */
193void FPSPlayer::tick (float time)
194{
195  Playable::tick( time );
196
197  if( ( xMouse != 0 || yMouse != 0 ) /*&& this->getOwner() == this->getHostID() */)
198  {
199    xMouse *= time ;
200    yMouse *= time ;
201
202    heading -= xMouse;
203    attitude-= yMouse;
204
205    if ( attitude > 2.05 )
206      attitude = 2.05;
207
208    else if ( attitude < -1.15 )
209      attitude = -1.15;
210
211    this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
212    this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
213
214    xMouse = yMouse = 0;
215  }
216
217 // this->setAbsDir( this->mouseDir );
218
219  Vector velocity;
220
221  if ( this->bForward )
222  {
223    velocity += this->getAbsDirX();
224  }
225
226  if ( this->bBackward )
227  {
228    velocity -= this->getAbsDirX();
229  }
230
231  if ( this->bRight )
232  {
233    velocity += this->getAbsDirZ();
234  }
235
236  if ( this->bLeft )
237  {
238    velocity -= this->getAbsDirZ();
239  }
240
241  velocity *= 100;
242
243  this->shiftCoor( velocity*time );
244
245
246
247
248  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
249  {
250    ((InteractiveModel*)this->getModel(0))->tick(time);
251//
252//     // handle animations differently
253//     if( this->bJump && likely(this->getModel(0) != NULL))
254//     {
255//       ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
256//     }
257//     else if( this->bFire && likely(this->getModel(0) != NULL))
258//     {
259//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK)
260//         ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
261//     }
262//     else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
263//     {
264//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN)
265//         ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
266//     }
267//     else if (likely(this->getModel(0) != NULL))
268//     {
269//       if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
270//         ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
271//     }
272  }
273
274}
275
276
277
278/**
279 *  draws the MD2Creature after transforming it.
280 */
281void FPSPlayer::draw () const
282{
283  // only draw if this entity is not the player since the player nevers sees himself
284  if( this->getCurrentPlayer() == NULL)
285    WorldEntity::draw();
286}
287
288
289
290/**
291 * process
292 */
293void FPSPlayer::process(const Event &event)
294{
295  Playable::process(event);
296
297  if( event.type == KeyMapper::PEV_LEFT)
298    this->bLeft = event.bPressed;
299  else if( event.type == KeyMapper::PEV_RIGHT)
300    this->bRight = event.bPressed;
301  else if( event.type == KeyMapper::PEV_FORWARD)
302    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
303  else if( event.type == KeyMapper::PEV_BACKWARD)
304    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
305  else if( event.type == EV_MOUSE_MOTION)
306  {
307    this->xMouse += event.xRel;
308    this->yMouse += event.yRel;
309  }
310  else if( event.type == KeyMapper::PEV_JUMP)
311    this->getAbsCoor().debug();
312}
313
314
315
316
Note: See TracBrowser for help on using the repository browser.