Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/spectator.cc @ 9921

Last change on this file since 9921 was 9921, checked in by rennerc, 17 years ago

spectator spawns fpsplayer on space key

File size: 5.8 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: Christoph Renner
13   co-programmer: ...
14
15*/
16
17#include "spectator.h"
18
19#include "src/lib/util/loading/factory.h"
20#include "key_mapper.h"
21
22#include "shared_network_data.h"
23
24#include "src/world_entities/creatures/fps_player.h"
25
26#include "class_id_DEPRECATED.h"
27ObjectListDefinitionID(Spectator, CL_SPECTATOR);
28CREATE_FACTORY(Spectator);
29
30
31/**
32 *  destructs the Spectator, deletes alocated memory
33 */
34Spectator::~Spectator ()
35{
36  this->setPlayer(NULL);
37}
38
39
40/**
41 *  creates a new Spectator from Xml Data
42 * @param root the xml element containing Spectator data
43
44   @todo add more parameters to load
45 */
46Spectator::Spectator(const TiXmlElement* root)
47{
48  this->init();
49  if (root != NULL)
50    this->loadParams(root);
51
52}
53
54
55/**
56 * initializes a Spectator
57 */
58void Spectator::init()
59{
60//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
61  this->registerObject(this, Spectator::_objectList);
62
63  this->getWeaponManager().changeWeaponConfig(1);
64
65  this->bLeft = false;
66  this->bRight = false;
67  this->bForward = false;
68  this->bBackward = false;
69  this->xMouse = 0.0f;
70  this->yMouse = 0.0f;
71
72  this->setHealthMax(100);
73  this->setHealth(80);
74
75  this->mouseDir = this->getAbsDir();
76
77  //add events to the eventlist
78  registerEvent(KeyMapper::PEV_FORWARD);
79  registerEvent(KeyMapper::PEV_BACKWARD);
80  registerEvent(KeyMapper::PEV_LEFT);
81  registerEvent(KeyMapper::PEV_RIGHT);
82  registerEvent(KeyMapper::PEV_FIRE1);
83  registerEvent(KeyMapper::PEV_JUMP);
84  registerEvent(EV_MOUSE_MOTION);
85
86  this->getWeaponManager().setSlotCount(0);
87
88  this->getWeaponManager().getFixedTarget()->setParent(this);
89  this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0);
90
91  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
92
93
94  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
95  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
96  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
97  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
98  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) );
99}
100
101
102/**
103 * loads the Settings of a Spectator from an XML-element.
104 * @param root the XML-element to load the Spaceship's properties from
105 */
106void Spectator::loadParams(const TiXmlElement* root)
107{
108  Playable::loadParams(root);
109}
110
111void Spectator::setPlayDirection(const Quaternion& quat, float speed)
112{
113  this->mouseDir = quat;
114  this->angleY = quat.getHeading();
115  this->angleX = quat.getAttitude();
116}
117
118
119void Spectator::reset()
120{
121  this->bLeft = false;
122  this->bRight = false;
123  this->bForward = false;
124  this->bBackward = false;
125  this->xMouse = 0.0f;
126  this->yMouse = 0.0f;
127
128  this->setHealth(80);
129}
130
131
132void Spectator::enter()
133{
134  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false );
135  this->attachCamera();
136}
137
138void Spectator::leave()
139{
140  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
141  this->detachCamera();
142}
143
144
145/**
146 *  this function is called, when two entities collide
147 * @param entity: the world entity with whom it collides
148 *
149 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
150 * @todo dont let Spectator fly through walls
151 */
152void Spectator::collidesWith(WorldEntity* entity, const Vector& location)
153{
154}
155
156
157
158/**
159 *  the function called for each passing timeSnap
160 * @param time The timespan passed since last update
161 */
162void Spectator::tick (float time)
163{
164  Playable::tick( time );
165
166  if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == SharedNetworkData::getInstance()->getHostID() )
167  {
168    xMouse *= time / 10;
169    yMouse *= time / 10;
170
171    angleX -= xMouse;
172    angleY -= yMouse;
173
174    if ( angleY > 2.05 )
175      angleY = 2.05;
176
177    if ( angleY < -1.15 )
178      angleY = -1.15;
179
180    this->mouseDir = Quaternion( angleX, Vector( 0, 1, 0 ) ) * Quaternion( angleY, Vector( 0, 0, 1 ) );
181
182    xMouse = yMouse = 0;
183  }
184
185  this->setAbsDir( this->mouseDir );
186
187  Vector velocity;
188
189  if ( this->bForward )
190  {
191    velocity += this->getAbsDirX();
192  }
193
194  if ( this->bBackward )
195  {
196    velocity -= this->getAbsDirX();
197  }
198
199  if ( this->bRight )
200  {
201    velocity += this->getAbsDirZ();
202  }
203
204  if ( this->bLeft )
205  {
206    velocity -= this->getAbsDirZ();
207  }
208
209  velocity *= 100;
210
211  this->shiftCoor( velocity*time );
212}
213
214/**
215 * @todo switch statement ??
216 */
217void Spectator::process(const Event &event)
218{
219  Playable::process(event);
220
221  if( event.type == KeyMapper::PEV_LEFT)
222    this->bLeft = event.bPressed;
223  else if( event.type == KeyMapper::PEV_RIGHT)
224    this->bRight = event.bPressed;
225  else if( event.type == KeyMapper::PEV_FORWARD)
226    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
227  else if( event.type == KeyMapper::PEV_BACKWARD)
228    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
229  else if( event.type == EV_MOUSE_MOTION)
230  {
231    this->xMouse += event.xRel;
232    this->yMouse += event.yRel;
233  }
234  else if( event.type == KeyMapper::PEV_JUMP)
235  {
236    FPSPlayer * fps = new FPSPlayer();
237    //WorldEntity * fps = new WorldEntity();
238   
239    fps->setAbsCoor( this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ() );
240    fps->setAbsDir( this->getAbsDir() );
241    fps->loadMD2Texture( "doom_guy.png" );
242    fps->loadModel( "models/creatures/doom_guy.md2", 10.0f );
243    //fps->loadModel( "models/ships/terran_cruizer.obj" );
244
245    //((Playable*)fps)->setPlayDirection(  0, 0, 1, 0 );
246  }
247}
248
249
250
251
Note: See TracBrowser for help on using the repository browser.