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
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#include "weapons/fps_sniper_rifle.h"
29
30#include "key_mapper.h"
31
32#include "debug.h"
33
34
35
36
37CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER);
38
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                       );
46
47
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;
85
86  this->xMouse = 0.0f;
87  this->yMouse = 0.0f;
88
89  this->setHealthMax(100);
90  this->setHealth(80);
91
92  this->fallVelocity = 0.0f;
93
94  this->cameraNode.setParent(this);
95
96  this->attitude = this->getAbsDir().getAttitude();
97  this->heading = this->getAbsDir().getHeading();
98
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);
105  registerEvent(KeyMapper::PEV_JUMP);
106  registerEvent(EV_MOUSE_MOTION);
107
108
109
110  // weapon manager for the fps
111  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
112
113  Weapon* wpRight = new FPSSniperRifle(0);
114  wpRight->setName("testGun Right");
115/*  Weapon* wpLeft = new TestGun(1);*/
116//   Weapon* wpLeft = new Turret();
117//   wpLeft->setName("testGun Left");
118
119//   this->addWeapon(wpLeft, 1, 0);
120  this->addWeapon(wpRight,1, 0);
121  this->getWeaponManager().changeWeaponConfig(1);
122
123  this->getWeaponManager().setSlotCount(2);
124  this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1));
125//   this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_2, Vector(0,1,0)));
126  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
127  this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0));
128  this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0)));
129
130  this->getWeaponManager().setParentNode(&this->cameraNode);
131  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
132
133  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
134  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
135
136
137  // network registration
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 ) );
142//  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) );
143
144
145  // collision reaction registration
146  this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY);
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{
161  this->attitude = this->getAbsDir().getAttitude();
162  this->heading = this->getAbsDir().getHeading();
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{
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);
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
210  if( ( xMouse != 0 || yMouse != 0 ) /*&& this->getOwner() == this->getHostID() */)
211  {
212    xMouse *= time ;
213    yMouse *= time ;
214
215    heading -= xMouse;
216    attitude-= yMouse;
217
218    if ( attitude > 2.05 )
219      attitude = 2.05;
220
221    else if ( attitude < -1.15 )
222      attitude = -1.15;
223
224    this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
225    this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
226
227    xMouse = yMouse = 0;
228  }
229
230 // this->setAbsDir( this->mouseDir );
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
254
255  velocity *= 100;
256
257
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  }
268
269
270  this->shiftCoor( velocity*time );
271
272
273
274
275
276
277
278  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
279  {
280    ((InteractiveModel*)this->getModel(0))->tick(time);
281//
282//     // handle animations differently
283//     if( this->bJump && likely(this->getModel(0) != NULL))
284//     {
285//       ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
286//     }
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//     }
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//     }
302  }
303
304}
305
306
307
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
314  if( this->getCurrentPlayer() == NULL)
315    WorldEntity::draw();
316}
317
318
319
320/**
321 * process
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  }
340  else if( event.type == KeyMapper::PEV_JUMP)
341    this->getAbsCoor().debug();
342}
343
344
345
346
Note: See TracBrowser for help on using the repository browser.