Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9091 was 9085, checked in by rennerc, 19 years ago

updated fpsplayer syncvars

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