Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8770 was 8731, checked in by bensch, 18 years ago

new player

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