Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/creatures/fps_player.cc @ 9023

Last change on this file since 9023 was 9003, checked in by bensch, 18 years ago

orxonox/trunk: merged the single_player_map branche back
merged with command:
svn merge -r8896:HEAD https://svn.orxonox.net/orxonox/branches/single_player_map .
no conflicts

File size: 8.7 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
35
36
37CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER);
38
39#include "script_class.h"
40CREATE_SCRIPTABLE_CLASS(FPSPlayer, CL_FPS_PLAYER,
41                        addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor))
42                            ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX))
43                            ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY))
44                            ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ))
45                       );
46
47
48/**
49 *  destructs the FPSPlayer, deletes alocated memory
50 */
51FPSPlayer::~FPSPlayer ()
52{
53  this->setPlayer(NULL);
54}
55
56
57/**
58 *  creates a new FPSPlayer from Xml Data
59 * @param root the xml element containing FPSPlayer data
60 *
61 */
62FPSPlayer::FPSPlayer(const TiXmlElement* root)
63{
64  this->init();
65
66  if (root != NULL)
67    this->loadParams(root);
68
69}
70
71
72/**
73 * initializes a FPSPlayer
74 */
75void FPSPlayer::init()
76{
77  this->setClassID(CL_FPS_PLAYER, "FPSPlayer");
78
79
80  this->bLeft = false;
81  this->bRight = false;
82  this->bForward = false;
83  this->bBackward = false;
84  this->bJump = false;
85  this->bPosBut = false;
86
87  this->xMouse = 0.0f;
88  this->yMouse = 0.0f;
89
90  this->setHealthMax(100);
91  this->setHealth(80);
92
93  this->fallVelocity = 0.0f;
94
95  this->cameraNode.setParent(this);
96
97  this->attitude = this->getAbsDir().getAttitude();
98  this->heading = this->getAbsDir().getHeading();
99
100  //add events to the eventlist
101  registerEvent(KeyMapper::PEV_FORWARD);
102  registerEvent(KeyMapper::PEV_BACKWARD);
103  registerEvent(KeyMapper::PEV_LEFT);
104  registerEvent(KeyMapper::PEV_RIGHT);
105  registerEvent(KeyMapper::PEV_FIRE1);
106  registerEvent(KeyMapper::PEV_JUMP);
107  registerEvent(EV_MOUSE_MOTION);
108
109
110
111  // weapon manager for the fps
112  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
113
114  Weapon* wpRight = new FPSSniperRifle(0);
115  wpRight->setName("testGun Right");
116/*  Weapon* wpLeft = new TestGun(1);*/
117//   Weapon* wpLeft = new Turret();
118//   wpLeft->setName("testGun Left");
119
120//   this->addWeapon(wpLeft, 1, 0);
121  this->addWeapon(wpRight,1, 0);
122  this->getWeaponManager().changeWeaponConfig(1);
123
124  this->getWeaponManager().setSlotCount(2);
125  this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1));
126//   this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_2, Vector(0,1,0)));
127  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
128  this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0));
129  this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0)));
130
131  this->getWeaponManager().setParentNode(&this->cameraNode);
132  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
133
134  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
135  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
136
137
138  // network registration
139  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
140  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
141  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
142  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
143//  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) );
144
145
146  // collision reaction registration
147  this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY);
148}
149
150
151/**
152 * loads the Settings of a FPSPlayer from an XML-element.
153 * @param root the XML-element to load the Spaceship's properties from
154 */
155void FPSPlayer::loadParams(const TiXmlElement* root)
156{
157  Playable::loadParams(root);
158}
159
160void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
161{
162  this->attitude = this->getAbsDir().getAttitude();
163  this->heading = this->getAbsDir().getHeading();
164}
165
166
167void FPSPlayer::reset()
168{
169  this->bLeft = false;
170  this->bRight = false;
171  this->bForward = false;
172  this->bBackward = false;
173  this->xMouse = 0.0f;
174  this->yMouse = 0.0f;
175
176  this->setHealth(80);
177}
178
179
180void FPSPlayer::enter()
181{
182  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
183
184  State::getCameraNode()->setParentSoft(&this->cameraNode);
185  State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
186
187  this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode());
188  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
189
190
191  State::getCameraNode()->setRelCoor(0,0,0);
192  State::getCameraTargetNode()->setRelCoor(10,0,0);
193}
194
195void FPSPlayer::leave()
196{
197  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
198  this->detachCamera();
199}
200
201
202
203/**
204 *  the function called for each passing timeSnap
205 * @param time The timespan passed since last update
206 */
207void FPSPlayer::tick (float time)
208{
209 
210  if( this->bPosBut)
211  {
212    this->bPosBut = false;
213    printf("prisoner:walkTo( %f, height, %f)\n",this->getAbsCoorX(),this->getAbsCoorZ());
214  }
215 
216  Playable::tick( time );
217
218  if( ( xMouse != 0 || yMouse != 0 ) /*&& this->getOwner() == this->getHostID() */)
219  {
220    xMouse *= time ;
221    yMouse *= time ;
222
223    heading -= xMouse;
224    attitude-= yMouse;
225
226    if ( attitude > 2.05 )
227      attitude = 2.05;
228
229    else if ( attitude < -1.15 )
230      attitude = -1.15;
231
232    this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
233    this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
234
235    xMouse = yMouse = 0;
236  }
237
238 // this->setAbsDir( this->mouseDir );
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.