Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/world_entities/creatures/fps_player.cc @ 8705

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

bsp: work flush, some more npc functions, and a positioning aid

File size: 6.3 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 "md2/md2Model.h"
21
22#include "src/lib/util/loading/factory.h"
23
24#include "key_mapper.h"
25
26#include "debug.h"
27
28
29
30CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER);
31
32
33using namespace std;
34
35
36/**
37 *  destructs the FPSPlayer, deletes alocated memory
38 */
39FPSPlayer::~FPSPlayer ()
40{
41  this->setPlayer(NULL);
42}
43
44
45/**
46 *  creates a new FPSPlayer from Xml Data
47 * @param root the xml element containing FPSPlayer data
48 *
49 */
50FPSPlayer::FPSPlayer(const TiXmlElement* root)
51{
52  this->init();
53
54  if (root != NULL)
55    this->loadParams(root);
56
57}
58
59
60/**
61 * initializes a FPSPlayer
62 */
63void FPSPlayer::init()
64{
65  this->setClassID(CL_FPS_PLAYER, "FPSPlayer");
66
67  this->getWeaponManager().changeWeaponConfig(1);
68
69  this->bLeft = false;
70  this->bRight = false;
71  this->bForward = false;
72  this->bBackward = false;
73  this->bJump = false;
74  this->xMouse = 0.0f;
75  this->yMouse = 0.0f;
76
77  this->setHealthMax(100);
78  this->setHealth(80);
79
80  this->mouseDir = this->getAbsDir();
81
82  //add events to the eventlist
83  registerEvent(KeyMapper::PEV_FORWARD);
84  registerEvent(KeyMapper::PEV_BACKWARD);
85  registerEvent(KeyMapper::PEV_LEFT);
86  registerEvent(KeyMapper::PEV_RIGHT);
87  registerEvent(KeyMapper::PEV_FIRE1);
88  registerEvent(KeyMapper::PEV_JUMP);
89  registerEvent(EV_MOUSE_MOTION);
90
91  this->getWeaponManager().setSlotCount(0);
92
93  this->getWeaponManager().getFixedTarget()->setParent(this);
94  this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0);
95
96  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
97
98  // network registration
99  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
100  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
101  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
102  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
103  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) );
104
105  // collision reaction registration
106  this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY);
107}
108
109
110/**
111 * loads the Settings of a FPSPlayer from an XML-element.
112 * @param root the XML-element to load the Spaceship's properties from
113 */
114void FPSPlayer::loadParams(const TiXmlElement* root)
115{
116  Playable::loadParams(root);
117}
118
119void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
120{
121  this->mouseDir = quat;
122  this->angleY = quat.getHeading();
123  this->angleX = quat.getAttitude();
124}
125
126
127void FPSPlayer::reset()
128{
129  this->bLeft = false;
130  this->bRight = false;
131  this->bForward = false;
132  this->bBackward = false;
133  this->xMouse = 0.0f;
134  this->yMouse = 0.0f;
135
136  this->setHealth(80);
137}
138
139
140void FPSPlayer::enter()
141{
142  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false );
143  this->attachCamera();
144}
145
146void FPSPlayer::leave()
147{
148  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
149  this->detachCamera();
150}
151
152
153
154/**
155 *  the function called for each passing timeSnap
156 * @param time The timespan passed since last update
157 */
158void FPSPlayer::tick (float time)
159{
160  Playable::tick( time );
161
162  if( ( xMouse != 0 || yMouse != 0 ) /*&& this->getOwner() == this->getHostID() */)
163  {
164    xMouse *= time ;
165    yMouse *= time ;
166
167    angleX -= xMouse;
168    angleY -= yMouse;
169
170    if ( angleY > 2.05 )
171      angleY = 2.05;
172
173    if ( angleY < -1.15 )
174      angleY = -1.15;
175
176    this->mouseDir = Quaternion( angleX, Vector( 0, 1, 0 ) ) * Quaternion( angleY, Vector( 0, 0, 1 ) );
177
178    xMouse = yMouse = 0;
179  }
180
181  this->setAbsDir( this->mouseDir );
182
183  Vector velocity;
184
185  if ( this->bForward )
186  {
187    velocity += this->getAbsDirX();
188  }
189
190  if ( this->bBackward )
191  {
192    velocity -= this->getAbsDirX();
193  }
194
195  if ( this->bRight )
196  {
197    velocity += this->getAbsDirZ();
198  }
199
200  if ( this->bLeft )
201  {
202    velocity -= this->getAbsDirZ();
203  }
204
205  velocity *= 100;
206
207  this->shiftCoor( velocity*time );
208
209
210
211
212  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
213  {
214    ((InteractiveModel*)this->getModel(0))->tick(time);
215//
216//     // handle animations differently
217//     if( this->bJump && likely(this->getModel(0) != NULL))
218//     {
219//       ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
220//     }
221//     else if( this->bFire && likely(this->getModel(0) != NULL))
222//     {
223//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK)
224//         ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
225//     }
226//     else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
227//     {
228//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN)
229//         ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
230//     }
231//     else if (likely(this->getModel(0) != NULL))
232//     {
233//       if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
234//         ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
235//     }
236  }
237
238}
239
240
241
242/**
243 *  draws the MD2Creature after transforming it.
244 */
245void FPSPlayer::draw () const
246{
247  // only draw if this entity is not the player since the player nevers sees himself
248  if( this->getCurrentPlayer() == NULL)
249    WorldEntity::draw();
250}
251
252
253
254/**
255 * process
256 */
257void FPSPlayer::process(const Event &event)
258{
259  Playable::process(event);
260
261  if( event.type == KeyMapper::PEV_LEFT)
262    this->bLeft = event.bPressed;
263  else if( event.type == KeyMapper::PEV_RIGHT)
264    this->bRight = event.bPressed;
265  else if( event.type == KeyMapper::PEV_FORWARD)
266    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
267  else if( event.type == KeyMapper::PEV_BACKWARD)
268    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
269  else if( event.type == EV_MOUSE_MOTION)
270  {
271    this->xMouse += event.xRel;
272    this->yMouse += event.yRel;
273  }
274  else if( event.type == KeyMapper::PEV_JUMP)
275    this->getAbsCoor().debug();
276}
277
278
279
280
Note: See TracBrowser for help on using the repository browser.