Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cleanup/src/world_entities/spectator.cc @ 10602

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

added ghost feature, which lets player fly around and print coords
add this to level file to enable ghost
<Spectator>

<name>Ghost</name>
<allowGhost>1</allowGhost>

</Spectator>

File size: 7.5 KB
RevLine 
[8067]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
[8708]22#include "shared_network_data.h"
[8067]23
[10114]24#include "src/world_entities/creatures/fps_player.h"
25#include "src/world_entities/npcs/generic_npc.h"
26
[10602]27#include "src/lib/util/loading/load_param.h"
[10114]28
[10602]29#include "player.h"
30
31
[10114]32ObjectListDefinition(Spectator);
[9869]33CREATE_FACTORY(Spectator);
[8067]34
[10602]35Spectator* Spectator::ghost = NULL;
36Playable* Spectator::regularPlayable = NULL;
[8067]37
[10108]38#include "state.h"
[10602]39#include "shell_command.h"
40             
41SHELL_COMMAND( enableGhost, Spectator, enableGhost )
42             ->describe("fly around")
43             ->setAlias("ghost");
[10108]44
45
[8067]46/**
[8228]47 *  destructs the Spectator, deletes alocated memory
[8067]48 */
[8228]49Spectator::~Spectator ()
[8067]50{
[8228]51  this->setPlayer(NULL);
52}
53
54
55/**
56 *  creates a new Spectator from Xml Data
57 * @param root the xml element containing Spectator data
58
59   @todo add more parameters to load
60 */
61Spectator::Spectator(const TiXmlElement* root)
62{
63  this->init();
64  if (root != NULL)
65    this->loadParams(root);
66
67}
68
69
70/**
71 * initializes a Spectator
72 */
73void Spectator::init()
74{
75//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
[9869]76  this->registerObject(this, Spectator::_objectList);
[8067]77
[8228]78  this->getWeaponManager().changeWeaponConfig(1);
79
[8067]80  this->bLeft = false;
81  this->bRight = false;
82  this->bForward = false;
83  this->bBackward = false;
84  this->xMouse = 0.0f;
85  this->yMouse = 0.0f;
[8228]86
87  this->setHealthMax(100);
88  this->setHealth(80);
89
90  this->mouseDir = this->getAbsDir();
91
92  //add events to the eventlist
[8067]93  registerEvent(KeyMapper::PEV_FORWARD);
94  registerEvent(KeyMapper::PEV_BACKWARD);
95  registerEvent(KeyMapper::PEV_LEFT);
96  registerEvent(KeyMapper::PEV_RIGHT);
97  registerEvent(KeyMapper::PEV_FIRE1);
[10114]98  registerEvent(KeyMapper::PEV_JUMP);
[8067]99  registerEvent(EV_MOUSE_MOTION);
[8228]100
101  this->getWeaponManager().setSlotCount(0);
102
103  this->getWeaponManager().getFixedTarget()->setParent(this);
104  this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0);
105
106  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
107
[9869]108
[8067]109  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
110  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
111  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
112  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
113  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mouseDir", PERMISSION_OWNER ) );
[10401]114
115  this->setPlayDirection(Quaternion(0,Vector(0,1,0)), 0.);
[8067]116}
117
[8228]118
[8067]119/**
[8228]120 * loads the Settings of a Spectator from an XML-element.
[10602]121 * @param root the XML-element to load the Spectator's properties from
[8067]122 */
[8228]123void Spectator::loadParams(const TiXmlElement* root)
[8067]124{
[8228]125  Playable::loadParams(root);
[10602]126 
127  LoadParam(root, "allowGhost", this, Spectator, allowGhost)
128    .describe("Allows the Player to fly around");
[8067]129}
130
[10602]131
132
133void Spectator::allowGhost( bool flag )
134{
135  PRINTF(0)( "SPECTATOR ALLOWGHOST: %d\n", flag );
136  if ( flag )
137  {
138    assert( ghost == NULL && "only one flySpectator allowed" );
139   
140    ghost = this;
141  }
142  else
143  {
144    ghost = NULL;
145  }
146}
147
148
149void Spectator::enableGhost( )
150{
151  if ( !regularPlayable )
152  {
153    if ( !State::getPlayer() || !State::getPlayer()->getPlayable() )
154      return;
155   
156    regularPlayable = State::getPlayer()->getPlayable();
157   
158    ghost->setAbsCoor( regularPlayable->getAbsCoor() );
159    ghost->setAbsDir( regularPlayable->getAbsDir() );
160   
161    State::getPlayer()->setPlayable( ghost );
162  }
163  else
164  {
165    if ( !State::getPlayer() || !State::getPlayer()->getPlayable() )
166      return;
167   
168    State::getPlayer()->setPlayable( regularPlayable );
169    regularPlayable = NULL;
170  }
171}
172
[8228]173void Spectator::setPlayDirection(const Quaternion& quat, float speed)
[8067]174{
[8228]175  this->mouseDir = quat;
176  this->angleY = quat.getHeading();
177  this->angleX = quat.getAttitude();
[8067]178}
179
[8228]180
181void Spectator::reset()
[8067]182{
[8228]183  this->bLeft = false;
184  this->bRight = false;
185  this->bForward = false;
186  this->bBackward = false;
187  this->xMouse = 0.0f;
188  this->yMouse = 0.0f;
189
190  this->setHealth(80);
191}
192
193
194void Spectator::enter()
195{
196  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false );
[8067]197  this->attachCamera();
198}
199
[8228]200void Spectator::leave()
[8067]201{
[8228]202  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
[8067]203  this->detachCamera();
204}
205
[8228]206
[8067]207/**
[8228]208 *  this function is called, when two entities collide
209 * @param entity: the world entity with whom it collides
210 *
211 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
212 * @todo dont let Spectator fly through walls
[8067]213 */
[8228]214void Spectator::collidesWith(WorldEntity* entity, const Vector& location)
[8067]215{
216}
217
[8228]218
219
220/**
221 *  the function called for each passing timeSnap
222 * @param time The timespan passed since last update
223 */
224void Spectator::tick (float time)
[8067]225{
[10401]226//   Playable::tick( time );
[9869]227
[10108]228  if( ( xMouse != 0 || yMouse != 0 ) && ( !State::isOnline() || this->getOwner() == SharedNetworkData::getInstance()->getHostID() ) )
[8067]229  {
230    xMouse *= time / 10;
231    yMouse *= time / 10;
[9869]232
[8147]233    angleX -= xMouse;
234    angleY -= yMouse;
[9869]235
[10401]236//     if ( angleY > 1.90 )
237//       angleY = 1.95;
238//
239//     if ( angleY < -1.07 )
240//       angleY = -1.07;
241   
242    xMouse = yMouse = 0;
[9869]243
[8147]244    this->mouseDir = Quaternion( angleX, Vector( 0, 1, 0 ) ) * Quaternion( angleY, Vector( 0, 0, 1 ) );
[9869]245
[10401]246   
247   
[8067]248  }
[9869]249
[8067]250  this->setAbsDir( this->mouseDir );
[9869]251
[8147]252  Vector velocity;
[9869]253
[8147]254  if ( this->bForward )
255  {
256    velocity += this->getAbsDirX();
257  }
[9869]258
[8147]259  if ( this->bBackward )
260  {
261    velocity -= this->getAbsDirX();
262  }
[9869]263
[8147]264  if ( this->bRight )
265  {
266    velocity += this->getAbsDirZ();
267  }
[9869]268
[8147]269  if ( this->bLeft )
270  {
271    velocity -= this->getAbsDirZ();
272  }
[9869]273
[8147]274  velocity *= 100;
[9869]275
[8147]276  this->shiftCoor( velocity*time );
[8067]277}
278
[8228]279/**
280 * @todo switch statement ??
281 */
282void Spectator::process(const Event &event)
[8067]283{
284  Playable::process(event);
285
286  if( event.type == KeyMapper::PEV_LEFT)
287    this->bLeft = event.bPressed;
288  else if( event.type == KeyMapper::PEV_RIGHT)
289    this->bRight = event.bPressed;
290  else if( event.type == KeyMapper::PEV_FORWARD)
291    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
292  else if( event.type == KeyMapper::PEV_BACKWARD)
293    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
294  else if( event.type == EV_MOUSE_MOTION)
295  {
296    this->xMouse += event.xRel;
297    this->yMouse += event.yRel;
298  }
[10601]299  else if( event.type == KeyMapper::PEV_FIRE1 )
[10114]300  {
[10601]301    PRINTF(0)( "CURRENT POS: (%f, %f, %f) ROT (%f, (%f, %f, %f))\n", this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ(), this->getAbsDir().w, this->getAbsDir().v.x, this->getAbsDir().v.y, this->getAbsDir().v.z );
[10114]302//     FPSPlayer * fps = new FPSPlayer();
[10401]303//     //GenericNPC* fps = new GenericNPC();
304//     WorldEntity* fps = new WorldEntity();
305//     //WorldEntity * fps = new WorldEntity();
306//
307//     fps->setAbsCoor( this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ() );
308//     fps->setAbsDir( this->getAbsDir() );
309//     fps->loadMD2Texture( "doom_guy.png" );
310//     fps->loadModel( "models/creatures/doom_guy.md2", 10.0f );
311//     fps->toList( OM_GROUP_00);
312//     //fps->loadModel( "models/ships/terran_cruizer.obj" );
[10114]313
[10401]314//     ((Playable*)fps)->setPlayDirection(  0, 0, 1, 0 );
[10114]315  }
[8067]316}
[8228]317
318
319
320
[10602]321
Note: See TracBrowser for help on using the repository browser.