Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

jump animation and debugs

File size: 9.4 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
154/**
155 * loads the Settings of a FPSPlayer from an XML-element.
156 * @param root the XML-element to load the Spaceship's properties from
157 */
158void FPSPlayer::loadParams(const TiXmlElement* root)
159{
160  Playable::loadParams(root);
161}
162
163void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
164{
165  this->attitude = this->getAbsDir().getAttitude();
166  this->heading = this->getAbsDir().getHeading();
167}
168
169
170void FPSPlayer::reset()
171{
172  this->bLeft = false;
173  this->bRight = false;
174  this->bForward = false;
175  this->bBackward = false;
176  this->xMouse = 0.0f;
177  this->yMouse = 0.0f;
178
179  this->setHealth(80);
180}
181
182
183void FPSPlayer::enter()
184{
185  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
186
187  State::getCameraNode()->setParentSoft(&this->cameraNode);
188  State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
189
190  this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode());
191  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
192
193
194  AABB* box = this->getModelAABB();
195  if( box != NULL)
196  {
197    State::getCameraNode()->setRelCoor(0, box->halfLength[1] * 2.0f, 0);
198    State::getCameraTargetNode()->setRelCoor(10, box->halfLength[1] * 2.0f, 0);
199
200    this->getWeaponManager().setSlotPosition(0, Vector(1.5, box->halfLength[1] * 2.0f - 0.7, 1.1));
201    this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * 2.0f, 0.0));
202  }
203}
204
205void FPSPlayer::leave()
206{
207  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
208  this->detachCamera();
209}
210
211
212
213/**
214 *  the function called for each passing timeSnap
215 * @param time The timespan passed since last update
216 */
217void FPSPlayer::tick (float time)
218{
219
220  if( this->bPosBut)
221  {
222    this->bPosBut = false;
223    printf("mechanic:walkTo( %f, %f, %f)\n",this->getAbsCoorX(),this->getAbsCoorY(),this->getAbsCoorZ());
224  }
225
226  Playable::tick( time );
227
228  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
229  {
230    xMouse *= time ;
231    yMouse *= time ;
232
233    heading -= xMouse;
234    attitude-= yMouse;
235
236    if ( attitude > 2.05 )
237      attitude = 2.05;
238
239    else if ( attitude < -1.15 )
240      attitude = -1.15;
241
242    xMouse = yMouse = 0;
243  }
244
245  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
246  this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
247
248  Vector velocity;
249
250  if ( this->bForward )
251  {
252    velocity += this->getAbsDirX();
253  }
254
255  if ( this->bBackward )
256  {
257    velocity -= this->getAbsDirX();
258  }
259
260  if ( this->bRight )
261  {
262    velocity += this->getAbsDirZ();
263  }
264
265  if ( this->bLeft )
266  {
267    velocity -= this->getAbsDirZ();
268  }
269
270
271  velocity *= 100;
272
273  if( this->bJump && likely(this->getModel(0) != NULL))
274  {
275    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
276      ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
277  }
278  else if(velocity.len() != 0.0f)
279  {
280    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
281      ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
282  }
283  else
284  {
285    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
286      ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
287  }
288
289
290
291  // physical falling of the player
292  if( !this->isOnGround())
293  {
294    this->fallVelocity += 300.0f * time;
295    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
296  }
297  else
298  {
299    this->fallVelocity = 0.0f;
300  }
301
302
303  this->shiftCoor( velocity*time );
304
305
306
307
308
309
310
311  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
312  {
313    ((InteractiveModel*)this->getModel(0))->tick(time);
314
315    // handle animations differently
316
317
318
319
320
321//     else if( this->bFire && likely(this->getModel(0) != NULL))
322//     {
323//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK)
324//         ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
325//     }
326//     else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
327//     {
328//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN)
329//         ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
330//     }
331//     else if (likely(this->getModel(0) != NULL))
332//     {
333//       if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
334//         ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
335//     }
336  }
337
338}
339
340
341
342/**
343 *  draws the MD2Creature after transforming it.
344 */
345void FPSPlayer::draw () const
346{
347  // only draw if this entity is not the player since the player nevers sees himself
348//   if( this->getCurrentPlayer() == NULL)
349    WorldEntity::draw();
350}
351
352
353
354/**
355 * process
356 */
357void FPSPlayer::process(const Event &event)
358{
359  Playable::process(event);
360
361  if( event.type == KeyMapper::PEV_LEFT)
362    this->bLeft = event.bPressed;
363  else if( event.type == KeyMapper::PEV_RIGHT)
364    this->bRight = event.bPressed;
365  else if( event.type == KeyMapper::PEV_FORWARD)
366    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
367  else if( event.type == KeyMapper::PEV_BACKWARD)
368    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
369  else if( event.type == EV_MOUSE_MOTION)
370  {
371    this->xMouse += event.xRel;
372    this->yMouse += event.yRel;
373  }
374  else if( event.type == KeyMapper::PEV_JUMP)
375    this->bJump = event.bPressed;
376    this->bPosBut = event.bPressed;
377}
378
379
380
381
Note: See TracBrowser for help on using the repository browser.