Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

aiming system integration

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