Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

bigger fps player (right height from model), collision reaction refinement. still there is a possibility to go through walls because of interference in the cr system. not 1st prio but later on fixed

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