Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/spectator.cc @ 10108

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

spectator in singleplayer

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