Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fps weapon work

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