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
RevLine 
[8685]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"
[8731]20#include "state.h"
[8685]21
22#include "src/lib/util/loading/factory.h"
23
[8776]24#include "weapons/weapon_manager.h"
25#include "weapons/test_gun.h"
26#include "weapons/turret.h"
27#include "weapons/cannon.h"
[8890]28#include "weapons/fps_sniper_rifle.h"
[8776]29
[8685]30#include "key_mapper.h"
31
[8687]32#include "debug.h"
[8685]33
34
[8687]35
[8731]36
[8685]37CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER);
38
[8869]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                       );
[8685]46
[8869]47
[8685]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;
[8731]85
[8685]86  this->xMouse = 0.0f;
87  this->yMouse = 0.0f;
88
89  this->setHealthMax(100);
90  this->setHealth(80);
91
92
[8731]93  this->cameraNode.setParent(this);
94
95  this->attitude = this->getAbsDir().getAttitude();
96  this->heading = this->getAbsDir().getHeading();
97
[8685]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);
[8687]104  registerEvent(KeyMapper::PEV_JUMP);
[8685]105  registerEvent(EV_MOUSE_MOTION);
106
107
108
[8776]109  // weapon manager for the fps
[8685]110  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
111
[8890]112  Weapon* wpRight = new FPSSniperRifle(0);
[8776]113  wpRight->setName("testGun Right");
[8890]114/*  Weapon* wpLeft = new TestGun(1);*/
[8776]115//   Weapon* wpLeft = new Turret();
[8890]116//   wpLeft->setName("testGun Left");
[8776]117
[8890]118//   this->addWeapon(wpLeft, 1, 0);
[8891]119  this->addWeapon(wpRight,1, 0);
[8776]120  this->getWeaponManager().changeWeaponConfig(1);
121
122  this->getWeaponManager().setSlotCount(2);
[8891]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)));
[8776]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
[8686]133  // network registration
[8685]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 ) );
[8731]138//  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) );
[8686]139
[8776]140
[8686]141  // collision reaction registration
[8693]142  this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY);
[8685]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{
[8731]157  this->attitude = this->getAbsDir().getAttitude();
158  this->heading = this->getAbsDir().getHeading();
[8685]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{
[8731]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);
[8685]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
[8699]206  if( ( xMouse != 0 || yMouse != 0 ) /*&& this->getOwner() == this->getHostID() */)
[8685]207  {
[8687]208    xMouse *= time ;
209    yMouse *= time ;
[8685]210
[8731]211    heading -= xMouse;
212    attitude-= yMouse;
[8685]213
[8731]214    if ( attitude > 2.05 )
215      attitude = 2.05;
[8685]216
[8731]217    else if ( attitude < -1.15 )
218      attitude = -1.15;
[8685]219
[8731]220    this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
221    this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
[8685]222
223    xMouse = yMouse = 0;
224  }
225
[8731]226 // this->setAbsDir( this->mouseDir );
[8685]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
[8693]257  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
258  {
259    ((InteractiveModel*)this->getModel(0))->tick(time);
[8687]260//
261//     // handle animations differently
262//     if( this->bJump && likely(this->getModel(0) != NULL))
263//     {
264//       ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
265//     }
[8685]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//     }
[8687]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//     }
[8693]281  }
[8685]282
283}
284
285
286
[8686]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
[8699]293  if( this->getCurrentPlayer() == NULL)
[8686]294    WorldEntity::draw();
295}
[8685]296
297
298
299/**
[8686]300 * process
[8685]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  }
[8705]319  else if( event.type == KeyMapper::PEV_JUMP)
320    this->getAbsCoor().debug();
[8685]321}
322
323
324
325
Note: See TracBrowser for help on using the repository browser.