Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9103 was 9103, checked in by patrick, 19 years ago

animations

File size: 9.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 "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
100  this->cameraNode.setParent(this);
101
102  this->attitude = this->getAbsDir().getAttitude();
103  this->heading = this->getAbsDir().getHeading();
104
105  //add events to the eventlist
106  registerEvent(KeyMapper::PEV_FORWARD);
107  registerEvent(KeyMapper::PEV_BACKWARD);
108  registerEvent(KeyMapper::PEV_LEFT);
109  registerEvent(KeyMapper::PEV_RIGHT);
110  registerEvent(KeyMapper::PEV_FIRE1);
111  registerEvent(KeyMapper::PEV_JUMP);
112  registerEvent(EV_MOUSE_MOTION);
113
114
115
116  // weapon manager for the fps
117  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
118
119  Weapon* wpRight = new FPSSniperRifle(0);
120  wpRight->setName("testGun Right");
121/*  Weapon* wpLeft = new TestGun(1);*/
122//   Weapon* wpLeft = new Turret();
123//   wpLeft->setName("testGun Left");
124
125//   this->addWeapon(wpLeft, 1, 0);
126  this->addWeapon(wpRight,1, 0);
127  this->getWeaponManager().changeWeaponConfig(1);
128
129  this->getWeaponManager().setSlotCount(2);
130//   this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_2, Vector(0,1,0)));
131  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
132  this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0)));
133  this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1));
134  this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0));
135
136
137  this->getWeaponManager().setParentNode(&this->cameraNode);
138  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
139
140  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
141  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
142
143
144  // network registration
145  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
146  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
147  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
148  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
149  registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) );
150  registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) );
151
152
153  // collision reaction registration
154  this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_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  AABB* box = this->getModelAABB();
199  if( box != NULL)
200  {
201    State::getCameraNode()->setRelCoor(0, box->halfLength[1] * 2.0f, 0);
202    State::getCameraTargetNode()->setRelCoor(10, box->halfLength[1] * 2.0f, 0);
203
204    this->getWeaponManager().setSlotPosition(0, Vector(1.5, box->halfLength[1] * 2.0f - 0.7, 1.1));
205    this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * 2.0f, 0.0));
206  }
207}
208
209void FPSPlayer::leave()
210{
211  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
212  this->detachCamera();
213}
214
215
216
217/**
218 *  the function called for each passing timeSnap
219 * @param time The timespan passed since last update
220 */
221void FPSPlayer::tick (float time)
222{
223
224  if( this->bPosBut)
225  {
226    this->bPosBut = false;
227    printf("mechanic:walkTo( %f, mheight, %f)\n",this->getAbsCoorX(),this->getAbsCoorZ());
228  }
229
230  Playable::tick( time );
231
232  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
233  {
234    xMouse *= time ;
235    yMouse *= time ;
236
237    heading -= xMouse;
238    attitude-= yMouse;
239
240    if ( attitude > 2.05 )
241      attitude = 2.05;
242
243    else if ( attitude < -1.15 )
244      attitude = -1.15;
245
246    xMouse = yMouse = 0;
247  }
248
249  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
250  this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
251
252  Vector velocity;
253
254  if ( this->bForward )
255  {
256    velocity += this->getAbsDirX();
257  }
258
259  if ( this->bBackward )
260  {
261    velocity -= this->getAbsDirX();
262  }
263
264  if ( this->bRight )
265  {
266    velocity += this->getAbsDirZ();
267  }
268
269  if ( this->bLeft )
270  {
271    velocity -= this->getAbsDirZ();
272  }
273
274
275  velocity *= 100;
276
277  if(velocity.len() != 0.0f)
278  {
279    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
280      ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
281  }
282  else
283  {
284    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
285      ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
286  }
287
288
289
290  // physical falling of the player
291  if( !this->isOnGround())
292  {
293    this->fallVelocity += 300.0f * time;
294    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
295  }
296  else
297  {
298    this->fallVelocity = 0.0f;
299  }
300
301
302  this->shiftCoor( velocity*time );
303
304
305
306
307
308
309
310  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
311  {
312    ((InteractiveModel*)this->getModel(0))->tick(time);
313
314    // handle animations differently
315
316
317
318    if( this->bJump && likely(this->getModel(0) != NULL))
319    {
320      if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
321        ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
322    }
323
324//     else if( this->bFire && likely(this->getModel(0) != NULL))
325//     {
326//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK)
327//         ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
328//     }
329//     else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
330//     {
331//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN)
332//         ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
333//     }
334//     else if (likely(this->getModel(0) != NULL))
335//     {
336//       if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
337//         ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
338//     }
339  }
340
341}
342
343
344
345/**
346 *  draws the MD2Creature after transforming it.
347 */
348void FPSPlayer::draw () const
349{
350  // only draw if this entity is not the player since the player nevers sees himself
351//   if( this->getCurrentPlayer() == NULL)
352    WorldEntity::draw();
353}
354
355
356
357/**
358 * process
359 */
360void FPSPlayer::process(const Event &event)
361{
362  Playable::process(event);
363
364  if( event.type == KeyMapper::PEV_LEFT)
365    this->bLeft = event.bPressed;
366  else if( event.type == KeyMapper::PEV_RIGHT)
367    this->bRight = event.bPressed;
368  else if( event.type == KeyMapper::PEV_FORWARD)
369    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
370  else if( event.type == KeyMapper::PEV_BACKWARD)
371    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
372  else if( event.type == EV_MOUSE_MOTION)
373  {
374    this->xMouse += event.xRel;
375    this->yMouse += event.yRel;
376  }
377  else if( event.type == KeyMapper::PEV_JUMP)
378    this->bPosBut = event.bPressed;
379}
380
381
382
383
Note: See TracBrowser for help on using the repository browser.