Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

weapon position fixed also

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