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