Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/single_player_map/src/world_entities/creatures/fps_player.cc @ 8940

Last change on this file since 8940 was 8940, checked in by patrick, 18 years ago

falling down fps player

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