Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/creatures/fps_player.cc @ 9126

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

different collision reactions for npcs and full walk ability

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