Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/single_player_map/src/world_entities/creatures/fps_player.cc @ 8869

Last change on this file since 8869 was 8869, checked in by snellen, 18 years ago

made FPSPlayer scriptable

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