Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

no more segfault

File size: 7.6 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->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
121
122  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
123  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
124
125
126  // network registration
127  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
128  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
129  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
130  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
131//  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) );
132
133
134  // collision reaction registration
135  this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY);
136}
137
138
139/**
140 * loads the Settings of a FPSPlayer from an XML-element.
141 * @param root the XML-element to load the Spaceship's properties from
142 */
143void FPSPlayer::loadParams(const TiXmlElement* root)
144{
145  Playable::loadParams(root);
146}
147
148void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
149{
150  this->attitude = this->getAbsDir().getAttitude();
151  this->heading = this->getAbsDir().getHeading();
152}
153
154
155void FPSPlayer::reset()
156{
157  this->bLeft = false;
158  this->bRight = false;
159  this->bForward = false;
160  this->bBackward = false;
161  this->xMouse = 0.0f;
162  this->yMouse = 0.0f;
163
164  this->setHealth(80);
165}
166
167
168void FPSPlayer::enter()
169{
170  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
171
172  State::getCameraNode()->setParentSoft(&this->cameraNode);
173  State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
174
175  this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode());
176  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
177
178
179  State::getCameraNode()->setRelCoor(0,0,0);
180  State::getCameraTargetNode()->setRelCoor(10,0,0);
181}
182
183void FPSPlayer::leave()
184{
185  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
186  this->detachCamera();
187}
188
189
190
191/**
192 *  the function called for each passing timeSnap
193 * @param time The timespan passed since last update
194 */
195void FPSPlayer::tick (float time)
196{
197  Playable::tick( time );
198
199  if( ( xMouse != 0 || yMouse != 0 ) /*&& this->getOwner() == this->getHostID() */)
200  {
201    xMouse *= time ;
202    yMouse *= time ;
203
204    heading -= xMouse;
205    attitude-= yMouse;
206
207    if ( attitude > 2.05 )
208      attitude = 2.05;
209
210    else if ( attitude < -1.15 )
211      attitude = -1.15;
212
213    this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
214    this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
215
216    xMouse = yMouse = 0;
217  }
218
219 // this->setAbsDir( this->mouseDir );
220
221  Vector velocity;
222
223  if ( this->bForward )
224  {
225    velocity += this->getAbsDirX();
226  }
227
228  if ( this->bBackward )
229  {
230    velocity -= this->getAbsDirX();
231  }
232
233  if ( this->bRight )
234  {
235    velocity += this->getAbsDirZ();
236  }
237
238  if ( this->bLeft )
239  {
240    velocity -= this->getAbsDirZ();
241  }
242
243  velocity *= 100;
244
245  this->shiftCoor( velocity*time );
246
247
248
249
250  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
251  {
252    ((InteractiveModel*)this->getModel(0))->tick(time);
253//
254//     // handle animations differently
255//     if( this->bJump && likely(this->getModel(0) != NULL))
256//     {
257//       ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
258//     }
259//     else if( this->bFire && likely(this->getModel(0) != NULL))
260//     {
261//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK)
262//         ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
263//     }
264//     else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
265//     {
266//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN)
267//         ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
268//     }
269//     else if (likely(this->getModel(0) != NULL))
270//     {
271//       if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
272//         ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
273//     }
274  }
275
276}
277
278
279
280/**
281 *  draws the MD2Creature after transforming it.
282 */
283void FPSPlayer::draw () const
284{
285  // only draw if this entity is not the player since the player nevers sees himself
286  if( this->getCurrentPlayer() == NULL)
287    WorldEntity::draw();
288}
289
290
291
292/**
293 * process
294 */
295void FPSPlayer::process(const Event &event)
296{
297  Playable::process(event);
298
299  if( event.type == KeyMapper::PEV_LEFT)
300    this->bLeft = event.bPressed;
301  else if( event.type == KeyMapper::PEV_RIGHT)
302    this->bRight = event.bPressed;
303  else if( event.type == KeyMapper::PEV_FORWARD)
304    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
305  else if( event.type == KeyMapper::PEV_BACKWARD)
306    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
307  else if( event.type == EV_MOUSE_MOTION)
308  {
309    this->xMouse += event.xRel;
310    this->yMouse += event.yRel;
311  }
312  else if( event.type == KeyMapper::PEV_JUMP)
313    this->getAbsCoor().debug();
314}
315
316
317
318
Note: See TracBrowser for help on using the repository browser.