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: Benjamin Knecht |
---|
13 | co-programmer: Silvan Nellen |
---|
14 | |
---|
15 | */ |
---|
16 | |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
18 | |
---|
19 | #include "executor/executor.h" |
---|
20 | #include "space_ship.h" |
---|
21 | |
---|
22 | #include "util/loading/resource_manager.h" |
---|
23 | |
---|
24 | #include "weapons/test_gun.h" |
---|
25 | #include "weapons/turret.h" |
---|
26 | #include "weapons/cannon.h" |
---|
27 | |
---|
28 | #include "dot_emitter.h" |
---|
29 | #include "sprite_particles.h" |
---|
30 | |
---|
31 | #include "util/loading/factory.h" |
---|
32 | #include "key_mapper.h" |
---|
33 | #include "event_handler.h" |
---|
34 | |
---|
35 | #include "network_game_manager.h" |
---|
36 | |
---|
37 | #include "power_ups/weapon_power_up.h" |
---|
38 | #include "power_ups/param_power_up.h" |
---|
39 | |
---|
40 | #include "graphics_engine.h" |
---|
41 | |
---|
42 | #include "plane.h" |
---|
43 | |
---|
44 | #include "state.h" |
---|
45 | #include "player.h" |
---|
46 | |
---|
47 | #include "util/loading/load_param.h" |
---|
48 | |
---|
49 | |
---|
50 | // #include "lib/gui/gl_gui/glgui_bar.h" |
---|
51 | // #include "lib/gui/gl_gui/glgui_pushbutton.h" |
---|
52 | |
---|
53 | |
---|
54 | using namespace std; |
---|
55 | |
---|
56 | CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP); |
---|
57 | |
---|
58 | |
---|
59 | /** |
---|
60 | * destructs the spaceship, deletes alocated memory |
---|
61 | */ |
---|
62 | SpaceShip::~SpaceShip () |
---|
63 | { |
---|
64 | this->setPlayer(NULL); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * loads a Spaceships information from a specified file. |
---|
69 | * @param fileName the name of the File to load the spaceship from (absolute path) |
---|
70 | */ |
---|
71 | SpaceShip::SpaceShip(const char* fileName) |
---|
72 | { |
---|
73 | this->init(); |
---|
74 | TiXmlDocument doc(fileName); |
---|
75 | |
---|
76 | if(!doc.LoadFile()) |
---|
77 | { |
---|
78 | PRINTF(2)("Loading file %s failed for spaceship.\n", fileName); |
---|
79 | return; |
---|
80 | } |
---|
81 | |
---|
82 | this->loadParams(doc.RootElement()); |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * creates a new Spaceship from Xml Data |
---|
87 | * @param root the xml element containing spaceship data |
---|
88 | |
---|
89 | @todo add more parameters to load |
---|
90 | */ |
---|
91 | SpaceShip::SpaceShip(const TiXmlElement* root) |
---|
92 | { |
---|
93 | this->init(); |
---|
94 | if (root != NULL) |
---|
95 | this->loadParams(root); |
---|
96 | else |
---|
97 | { |
---|
98 | //this->loadModel("models/ships/reap_#.obj"); |
---|
99 | //TODO HACK this is only for network multiplayer games. |
---|
100 | if( this->getOwner()%2 == 0) |
---|
101 | { |
---|
102 | this->loadModel("models/ships/reap_#.obj"); |
---|
103 | this->toList(OM_GROUP_00); |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | this->loadModel( "models/ships/fighter.obj" ); |
---|
108 | this->toList(OM_GROUP_01); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | /** |
---|
116 | * initializes a Spaceship |
---|
117 | */ |
---|
118 | void SpaceShip::init() |
---|
119 | { |
---|
120 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
121 | this->setClassID(CL_SPACE_SHIP, "SpaceShip"); |
---|
122 | |
---|
123 | PRINTF(4)("SPACESHIP INIT\n"); |
---|
124 | |
---|
125 | //weapons: |
---|
126 | Weapon* wpRight = new TestGun(0); |
---|
127 | wpRight->setName("testGun Right"); |
---|
128 | Weapon* wpLeft = new TestGun(1); |
---|
129 | wpLeft->setName("testGun Left"); |
---|
130 | //Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON)); |
---|
131 | |
---|
132 | //cannon->setName("BFG"); |
---|
133 | |
---|
134 | this->addWeapon(wpLeft, 1, 0); |
---|
135 | this->addWeapon(wpRight,1 ,1); |
---|
136 | //this->addWeapon(cannon, 0, 6); |
---|
137 | |
---|
138 | this->getWeaponManager()->changeWeaponConfig(1); |
---|
139 | |
---|
140 | bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false; |
---|
141 | |
---|
142 | xMouse = yMouse = 0; |
---|
143 | yInvert = 1; |
---|
144 | mouseSensitivity = 0.001; |
---|
145 | airViscosity = 0.9; |
---|
146 | controlVelocityX = 25; |
---|
147 | controlVelocityY = 150; |
---|
148 | shipInertia = 1.5; |
---|
149 | // cycle = 0.0; |
---|
150 | |
---|
151 | this->setHealthMax(100); |
---|
152 | this->setHealth(80); |
---|
153 | |
---|
154 | travelSpeed = 0.0; |
---|
155 | acceleration = 3; |
---|
156 | this->velocity = this->getAbsDirX()*travelSpeed; |
---|
157 | this->mouseDir = this->getAbsDir(); |
---|
158 | this->pitchDir = this->getAbsDir(); |
---|
159 | |
---|
160 | // GLGuiButton* button = new GLGuiPushButton(); |
---|
161 | // button->show(); |
---|
162 | // button->setLabel("orxonox"); |
---|
163 | // button->setBindNode(this); |
---|
164 | // GLGuiBar* bar = new GLGuiBar(); |
---|
165 | // bar->show(); |
---|
166 | // bar->setValue(7.0); |
---|
167 | // bar->setMaximum(10); |
---|
168 | // bar->setSize2D( 20, 100); |
---|
169 | // bar->setAbsCoor2D( 10, 200); |
---|
170 | |
---|
171 | //add events to the eventlist |
---|
172 | registerEvent(KeyMapper::PEV_FORWARD); |
---|
173 | registerEvent(KeyMapper::PEV_BACKWARD); |
---|
174 | registerEvent(KeyMapper::PEV_LEFT); |
---|
175 | registerEvent(KeyMapper::PEV_RIGHT); |
---|
176 | //registerEvent(SDLK_q); |
---|
177 | //registerEvent(SDLK_e); |
---|
178 | registerEvent(KeyMapper::PEV_FIRE1); |
---|
179 | registerEvent(KeyMapper::PEV_NEXT_WEAPON); |
---|
180 | registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); |
---|
181 | //registerEvent(SDLK_PAGEUP); |
---|
182 | //registerEvent(SDLK_PAGEDOWN); |
---|
183 | registerEvent(EV_MOUSE_MOTION); |
---|
184 | |
---|
185 | this->getWeaponManager()->setSlotCount(7); |
---|
186 | |
---|
187 | this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
188 | this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
189 | |
---|
190 | this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
191 | this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
192 | |
---|
193 | this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
194 | this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
195 | |
---|
196 | this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
197 | this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
198 | |
---|
199 | this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
200 | this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
201 | |
---|
202 | this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
203 | this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
204 | // |
---|
205 | this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0)); |
---|
206 | this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
207 | // |
---|
208 | // this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0)); |
---|
209 | // this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0))); |
---|
210 | // |
---|
211 | // this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0)); |
---|
212 | // this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));: |
---|
213 | |
---|
214 | this->getWeaponManager()->getFixedTarget()->setParent(this); |
---|
215 | this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0); |
---|
216 | |
---|
217 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
218 | |
---|
219 | this->burstEmitter = new DotEmitter(200, 0.0, .01); |
---|
220 | this->burstEmitter->setParent(this); |
---|
221 | this->burstEmitter->setRelCoor(-1, .5, 0); |
---|
222 | this->burstEmitter->setName("SpaceShip_Burst_emitter"); |
---|
223 | |
---|
224 | this->burstSystem = new SpriteParticles(1000); |
---|
225 | this->burstSystem->addEmitter(this->burstEmitter); |
---|
226 | this->burstSystem->setName("SpaceShip_Burst_System"); |
---|
227 | ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png"); |
---|
228 | this->burstSystem->setLifeSpan(1.0, .3); |
---|
229 | this->burstSystem->setRadius(0.0, 1.0); |
---|
230 | this->burstSystem->setRadius(0.05, 1.0); |
---|
231 | this->burstSystem->setRadius(.5, .8); |
---|
232 | this->burstSystem->setRadius(1.0, 0); |
---|
233 | this->burstSystem->setColor(0.0, .7,.7,1,.7); |
---|
234 | this->burstSystem->setColor(0.2, 0,0,0.8,.5); |
---|
235 | this->burstSystem->setColor(0.5, .5,.5,.8,.8); |
---|
236 | this->burstSystem->setColor(1.0, .8,.8,.8,.0); |
---|
237 | |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | /** |
---|
242 | * loads the Settings of a SpaceShip from an XML-element. |
---|
243 | * @param root the XML-element to load the Spaceship's properties from |
---|
244 | */ |
---|
245 | void SpaceShip::loadParams(const TiXmlElement* root) |
---|
246 | { |
---|
247 | Playable::loadParams(root); |
---|
248 | } |
---|
249 | |
---|
250 | void SpaceShip::setAbsDirPlay(const Quaternion& quat) |
---|
251 | { |
---|
252 | this->mouseDir = quat; |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | void SpaceShip::reset() |
---|
257 | { |
---|
258 | bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false; |
---|
259 | |
---|
260 | xMouse = yMouse = 0; |
---|
261 | |
---|
262 | this->setHealth(80); |
---|
263 | this->velocity = Vector(0.0, 0.0, 0.0); |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | void SpaceShip::enter() |
---|
268 | { |
---|
269 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true); |
---|
270 | this->attachCamera(); |
---|
271 | } |
---|
272 | |
---|
273 | void SpaceShip::leave() |
---|
274 | { |
---|
275 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
276 | this->detachCamera(); |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | /** |
---|
281 | * effect that occurs after the SpaceShip is spawned |
---|
282 | */ |
---|
283 | void SpaceShip::postSpawn () |
---|
284 | { |
---|
285 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * the action occuring if the spaceship left the game |
---|
290 | */ |
---|
291 | void SpaceShip::leftWorld () |
---|
292 | {} |
---|
293 | |
---|
294 | WorldEntity* ref = NULL; |
---|
295 | /** |
---|
296 | * this function is called, when two entities collide |
---|
297 | * @param entity: the world entity with whom it collides |
---|
298 | * |
---|
299 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
300 | */ |
---|
301 | void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location) |
---|
302 | { |
---|
303 | Playable::collidesWith(entity, location); |
---|
304 | |
---|
305 | if( entity->isA(CL_PROJECTILE) && entity != ref) |
---|
306 | { |
---|
307 | if ( isServer() ) |
---|
308 | { |
---|
309 | networkCollisionList.push_back( entity->getHealth() ); |
---|
310 | doCollideNetwork( entity->getHealth() ); |
---|
311 | } |
---|
312 | } |
---|
313 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | * draws the spaceship after transforming it. |
---|
318 | */ |
---|
319 | void SpaceShip::draw () const |
---|
320 | { |
---|
321 | WorldEntity::draw(); |
---|
322 | this->getWeaponManager()->draw(); |
---|
323 | |
---|
324 | //this->debug(0); |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * the function called for each passing timeSnap |
---|
329 | * @param time The timespan passed since last update |
---|
330 | */ |
---|
331 | void SpaceShip::tick (float time) |
---|
332 | { |
---|
333 | Playable::tick(time); |
---|
334 | |
---|
335 | if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == this->getHostID() ) |
---|
336 | { |
---|
337 | if (xMouse > controlVelocityX) xMouse = controlVelocityX; |
---|
338 | else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX; |
---|
339 | if (yMouse > controlVelocityY) yMouse = controlVelocityY; |
---|
340 | else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY; |
---|
341 | |
---|
342 | pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0))); |
---|
343 | |
---|
344 | mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir); |
---|
345 | xMouse = yMouse = 0; |
---|
346 | } |
---|
347 | |
---|
348 | |
---|
349 | // if( this != State::getPlayer()->getControllable()) |
---|
350 | // return; |
---|
351 | |
---|
352 | // spaceship controlled movement |
---|
353 | if (this->getOwner() == this->getHostID()) |
---|
354 | this->calculateVelocity(time); |
---|
355 | |
---|
356 | |
---|
357 | Vector move = velocity*time; |
---|
358 | |
---|
359 | //orient the velocity in the direction of the spaceship. |
---|
360 | travelSpeed = velocity.len(); |
---|
361 | velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity; |
---|
362 | velocity = (velocity.getNormalized())*travelSpeed; |
---|
363 | this->burstEmitter->setEmissionRate(travelSpeed); |
---|
364 | this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1); |
---|
365 | |
---|
366 | //orient the spaceship in direction of the mouse |
---|
367 | rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia); |
---|
368 | if (this->getAbsDir().distance(rotQuat) > 0.00000000000001) |
---|
369 | this->setAbsDir( rotQuat); |
---|
370 | //this->setAbsDirSoft(mouseDir,5); |
---|
371 | |
---|
372 | // this is the air friction (necessary for a smooth control) |
---|
373 | if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001; |
---|
374 | else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001; |
---|
375 | |
---|
376 | //other physics (gravity) |
---|
377 | //if(travelSpeed < 120) |
---|
378 | //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time; |
---|
379 | |
---|
380 | //hoover effect |
---|
381 | //cycle += time; |
---|
382 | //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02); |
---|
383 | |
---|
384 | //readjust |
---|
385 | //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0))); |
---|
386 | //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0))); |
---|
387 | |
---|
388 | //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); |
---|
389 | |
---|
390 | this->shiftCoor(move); |
---|
391 | |
---|
392 | |
---|
393 | } |
---|
394 | |
---|
395 | /** |
---|
396 | * calculate the velocity |
---|
397 | * @param time the timeslice since the last frame |
---|
398 | */ |
---|
399 | void SpaceShip::calculateVelocity (float time) |
---|
400 | { |
---|
401 | Vector accel(0.0, 0.0, 0.0); |
---|
402 | /* |
---|
403 | Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter |
---|
404 | */ |
---|
405 | //float rotVal = 0.0; |
---|
406 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
407 | /* calculate the direction in which the craft is heading */ |
---|
408 | |
---|
409 | //Plane plane(Vector(0,1,0), Vector(0,0,0)); |
---|
410 | |
---|
411 | if( this->bUp ) |
---|
412 | { |
---|
413 | //this->shiftCoor(this->getAbsDirX()); |
---|
414 | //accel += (this->getAbsDirX())*2; |
---|
415 | accel += (this->getAbsDirX())*acceleration; |
---|
416 | |
---|
417 | } |
---|
418 | |
---|
419 | if( this->bDown ) |
---|
420 | { |
---|
421 | //this->shiftCoor((this->getAbsDirX())*-1); |
---|
422 | //accel -= (this->getAbsDirX())*2; |
---|
423 | //if(velocity.len() > 50) |
---|
424 | accel -= (this->getAbsDirX())*0.5*acceleration; |
---|
425 | |
---|
426 | |
---|
427 | |
---|
428 | } |
---|
429 | |
---|
430 | if( this->bLeft/* > -this->getRelCoor().z*2*/) |
---|
431 | { |
---|
432 | this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
433 | // accel -= rightDirection; |
---|
434 | //velocityDir.normalize(); |
---|
435 | //rot +=Vector(1,0,0); |
---|
436 | //rotVal -= .4; |
---|
437 | } |
---|
438 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
439 | { |
---|
440 | this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
441 | |
---|
442 | // accel += rightDirection; |
---|
443 | //velocityDir.normalize(); |
---|
444 | //rot += Vector(1,0,0); |
---|
445 | //rotVal += .4; |
---|
446 | } |
---|
447 | |
---|
448 | |
---|
449 | if( this->bRollL /* > -this->getRelCoor().z*2*/) |
---|
450 | { |
---|
451 | mouseDir *= Quaternion(-time*2, Vector(1,0,0)); |
---|
452 | // accel -= rightDirection; |
---|
453 | //velocityDir.normalize(); |
---|
454 | //rot +=Vector(1,0,0); |
---|
455 | //rotVal -= .4; |
---|
456 | } |
---|
457 | if( this->bRollR /* > this->getRelCoor().z*2*/) |
---|
458 | { |
---|
459 | mouseDir *= Quaternion(time*2, Vector(1,0,0)); |
---|
460 | |
---|
461 | // accel += rightDirection; |
---|
462 | //velocityDir.normalize(); |
---|
463 | //rot += Vector(1,0,0); |
---|
464 | //rotVal += .4; |
---|
465 | } |
---|
466 | if (this->bAscend ) |
---|
467 | { |
---|
468 | this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
469 | |
---|
470 | // accel += upDirection; |
---|
471 | //velocityDir.normalize(); |
---|
472 | //rot += Vector(0,0,1); |
---|
473 | //rotVal += .4; |
---|
474 | } |
---|
475 | if (this->bDescend ) |
---|
476 | { |
---|
477 | this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
478 | |
---|
479 | // accel -= upDirection; |
---|
480 | //velocityDir.normalize(); |
---|
481 | //rot += Vector(0,0,1); |
---|
482 | //rotVal -= .4; |
---|
483 | } |
---|
484 | |
---|
485 | velocity += accel*time*10; |
---|
486 | //rot.normalize(); |
---|
487 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
488 | } |
---|
489 | |
---|
490 | /** |
---|
491 | * @todo switch statement ?? |
---|
492 | */ |
---|
493 | void SpaceShip::process(const Event &event) |
---|
494 | { |
---|
495 | Playable::process(event); |
---|
496 | |
---|
497 | if( event.type == KeyMapper::PEV_LEFT) |
---|
498 | this->bRollL = event.bPressed; |
---|
499 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
500 | this->bRollR = event.bPressed; |
---|
501 | else if( event.type == KeyMapper::PEV_FORWARD) |
---|
502 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
503 | else if( event.type == KeyMapper::PEV_BACKWARD) |
---|
504 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
505 | else if( event.type == EV_MOUSE_MOTION) |
---|
506 | { |
---|
507 | this->xMouse += event.xRel; |
---|
508 | this->yMouse += event.yRel; |
---|
509 | } |
---|
510 | } |
---|
511 | |
---|
512 | |
---|
513 | #define MASK_bUp 1 |
---|
514 | #define MASK_bDown 2 |
---|
515 | #define MASK_bLeft 4 |
---|
516 | #define MASK_bRight 8 |
---|
517 | #define MASK_bAscend 16 |
---|
518 | #define MASK_bDescend 32 |
---|
519 | #define MASK_bRollL 64 |
---|
520 | #define MASK_bRollR 128 |
---|
521 | |
---|
522 | #define DATA_state 1 |
---|
523 | #define DATA_flags 2 |
---|
524 | #define DATA_mouse 3 |
---|
525 | #define DATA_sync 4 |
---|
526 | #define DATA_velocity 5 |
---|
527 | #define DATA_playables 6 |
---|
528 | #define DATA_collision 7 |
---|
529 | |
---|
530 | int SpaceShip::writeBytes( const byte * data, int length, int sender ) |
---|
531 | { |
---|
532 | SYNCHELP_READ_BEGIN(); |
---|
533 | |
---|
534 | byte b; |
---|
535 | |
---|
536 | while ( SYNCHELP_READ_REMAINING()>0 ) |
---|
537 | { |
---|
538 | SYNCHELP_READ_BYTE( b, NWT_SS_B ); |
---|
539 | |
---|
540 | if ( b == DATA_state ) |
---|
541 | { |
---|
542 | setRequestedSync( false ); |
---|
543 | setIsOutOfSync( false ); |
---|
544 | SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_SS_WE_STATE ); |
---|
545 | |
---|
546 | continue; |
---|
547 | } |
---|
548 | |
---|
549 | if ( b == DATA_flags ) |
---|
550 | { |
---|
551 | if ( this->getOwner() != this->getHostID() ) |
---|
552 | { |
---|
553 | byte flags = 0; |
---|
554 | SYNCHELP_READ_BYTE( flags, NWT_SS_FLAGS ); |
---|
555 | |
---|
556 | bUp = (flags & MASK_bUp) != 0; |
---|
557 | bDown = (flags & MASK_bDown) != 0; |
---|
558 | bLeft = (flags & MASK_bLeft) != 0; |
---|
559 | bRight = (flags & MASK_bRight) != 0; |
---|
560 | bAscend = (flags & MASK_bAscend) != 0; |
---|
561 | bDescend = (flags & MASK_bDescend) != 0; |
---|
562 | bRollL = (flags & MASK_bRollL) != 0; |
---|
563 | bRollR = (flags & MASK_bRollR) != 0; |
---|
564 | |
---|
565 | } |
---|
566 | else |
---|
567 | assert(false); |
---|
568 | |
---|
569 | continue; |
---|
570 | } |
---|
571 | |
---|
572 | if ( b == DATA_mouse ) |
---|
573 | { |
---|
574 | if ( this->getOwner() != this->getHostID() ) |
---|
575 | { |
---|
576 | SYNCHELP_READ_FLOAT( mouseDir.w, NWT_SS_MOUSEDIRW ); |
---|
577 | SYNCHELP_READ_FLOAT( mouseDir.v.x, NWT_SS_MOUSEDIRX ); |
---|
578 | SYNCHELP_READ_FLOAT( mouseDir.v.y, NWT_SS_MOUSEDIRY ); |
---|
579 | SYNCHELP_READ_FLOAT( mouseDir.v.z, NWT_SS_MOUSEDIRZ ); |
---|
580 | } |
---|
581 | else |
---|
582 | assert(false); |
---|
583 | |
---|
584 | continue; |
---|
585 | } |
---|
586 | |
---|
587 | if ( b == DATA_sync ) |
---|
588 | { |
---|
589 | if ( this->getOwner() != this->getHostID() ) |
---|
590 | { |
---|
591 | SYNCHELP_READ_FKT( PNode::writeSync, NWT_SS_PN_SYNC ); |
---|
592 | } |
---|
593 | else |
---|
594 | assert(false); |
---|
595 | |
---|
596 | continue; |
---|
597 | } |
---|
598 | |
---|
599 | if ( b == DATA_velocity ) |
---|
600 | { |
---|
601 | if ( this->getOwner() != this->getHostID() ) |
---|
602 | { |
---|
603 | SYNCHELP_READ_FLOAT( velocity.x, NWT_SS_VELX ); |
---|
604 | SYNCHELP_READ_FLOAT( velocity.y, NWT_SS_VELY ); |
---|
605 | SYNCHELP_READ_FLOAT( velocity.z, NWT_SS_VELZ ); |
---|
606 | } |
---|
607 | else |
---|
608 | assert(false); |
---|
609 | |
---|
610 | continue; |
---|
611 | } |
---|
612 | |
---|
613 | if ( b == DATA_playables ) |
---|
614 | { |
---|
615 | if ( this->getOwner() != this->getHostID() ) |
---|
616 | { |
---|
617 | SYNCHELP_READ_FKT( Playable::writeSync, NWT_SS_PL_SYNC ); |
---|
618 | } |
---|
619 | else |
---|
620 | assert(false); |
---|
621 | } |
---|
622 | |
---|
623 | if ( b == DATA_collision ) |
---|
624 | { |
---|
625 | int n; |
---|
626 | float energy; |
---|
627 | SYNCHELP_READ_INT( n, NWT_SS_CO_N ); |
---|
628 | |
---|
629 | for ( int i = 0; i<n; i++ ) |
---|
630 | { |
---|
631 | SYNCHELP_READ_FLOAT( energy, NWT_SS_CO_CLID ); |
---|
632 | doCollideNetwork( energy ); |
---|
633 | } |
---|
634 | } |
---|
635 | } |
---|
636 | |
---|
637 | return SYNCHELP_READ_N; |
---|
638 | } |
---|
639 | |
---|
640 | |
---|
641 | |
---|
642 | int SpaceShip::readBytes( byte * data, int maxLength, int * reciever ) |
---|
643 | { |
---|
644 | SYNCHELP_WRITE_BEGIN(); |
---|
645 | |
---|
646 | if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ ) |
---|
647 | { |
---|
648 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
649 | setRequestedSync( true ); |
---|
650 | PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID()); |
---|
651 | } |
---|
652 | |
---|
653 | int rec = this->getRequestSync(); |
---|
654 | if ( rec > 0 ) |
---|
655 | { |
---|
656 | *reciever = rec; |
---|
657 | |
---|
658 | PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec); |
---|
659 | |
---|
660 | SYNCHELP_WRITE_BYTE( (byte)DATA_state, NWT_SS_B ); |
---|
661 | |
---|
662 | SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_SS_WE_STATE ); |
---|
663 | |
---|
664 | return SYNCHELP_WRITE_N; |
---|
665 | } |
---|
666 | |
---|
667 | *reciever = 0 - this->getOwner(); |
---|
668 | //TODO: implement with SYNCHELP_WRITE_SENT() |
---|
669 | bool sentSomething = false; |
---|
670 | |
---|
671 | if ( PNode::needsReadSync() && ( this->getHostID()==0 || this->getOwner() == this->getHostID() ) ) |
---|
672 | { |
---|
673 | PRINTF(0)("sending PNode::readSync\n"); |
---|
674 | SYNCHELP_WRITE_BYTE( DATA_sync, NWT_SS_B ); |
---|
675 | SYNCHELP_WRITE_FKT( PNode::readSync, NWT_SS_PN_SYNC ); |
---|
676 | sentSomething = true; |
---|
677 | } |
---|
678 | |
---|
679 | if ( this->getHostID()==0 || this->getHostID()==this->getOwner() ) |
---|
680 | { |
---|
681 | byte mask = 0; |
---|
682 | |
---|
683 | if ( bUp ) |
---|
684 | mask |= MASK_bUp; |
---|
685 | if ( bDown ) |
---|
686 | mask |= MASK_bDown; |
---|
687 | if ( bLeft ) |
---|
688 | mask |= MASK_bLeft; |
---|
689 | if ( bRight ) |
---|
690 | mask |= MASK_bRight; |
---|
691 | if ( bAscend ) |
---|
692 | mask |= MASK_bAscend; |
---|
693 | if ( bRollL ) |
---|
694 | mask |= MASK_bRollL; |
---|
695 | if ( bRollR ) |
---|
696 | mask |= MASK_bRollR; |
---|
697 | |
---|
698 | |
---|
699 | if ( mask != oldMask ) |
---|
700 | { |
---|
701 | oldMask = mask; |
---|
702 | PRINTF(0)("sending mask\n"); |
---|
703 | sentSomething = true; |
---|
704 | SYNCHELP_WRITE_BYTE( DATA_flags, NWT_SS_B ); |
---|
705 | SYNCHELP_WRITE_BYTE( mask, NWT_SS_FLAGS ); |
---|
706 | } |
---|
707 | #define __OFFSET_MDIR_W 0.01 |
---|
708 | #define __OFFSET_MDIR_V 0.01 |
---|
709 | if ( fabs( oldMouseDir.w - mouseDir.w ) > __OFFSET_MDIR_W || |
---|
710 | fabs( oldMouseDir.v.x - mouseDir.v.x ) > __OFFSET_MDIR_V || |
---|
711 | fabs( oldMouseDir.v.y - mouseDir.v.y ) > __OFFSET_MDIR_V || |
---|
712 | fabs( oldMouseDir.v.z - mouseDir.v.z ) > __OFFSET_MDIR_V ) |
---|
713 | { |
---|
714 | oldMouseDir = mouseDir; |
---|
715 | |
---|
716 | SYNCHELP_WRITE_BYTE( DATA_mouse, NWT_SS_B ); |
---|
717 | PRINTF(0)("SENDING mousedir\n"); |
---|
718 | sentSomething = true; |
---|
719 | SYNCHELP_WRITE_FLOAT( mouseDir.w, NWT_SS_MOUSEDIRW ); |
---|
720 | SYNCHELP_WRITE_FLOAT( mouseDir.v.x, NWT_SS_MOUSEDIRX ); |
---|
721 | SYNCHELP_WRITE_FLOAT( mouseDir.v.y, NWT_SS_MOUSEDIRY ); |
---|
722 | SYNCHELP_WRITE_FLOAT( mouseDir.v.z, NWT_SS_MOUSEDIRZ ); |
---|
723 | } |
---|
724 | #define __OFFSET_VEL 0.05 |
---|
725 | if ( fabs( oldVelocity.x - velocity.x ) > __OFFSET_VEL*fabs(oldVelocity.x)+0.1 || |
---|
726 | fabs( oldVelocity.y - velocity.y ) > __OFFSET_VEL*fabs(oldVelocity.y)+0.1 || |
---|
727 | fabs( oldVelocity.z - velocity.z ) > __OFFSET_VEL*fabs(oldVelocity.z)+0.1 ) |
---|
728 | { |
---|
729 | oldVelocity.x = velocity.x; |
---|
730 | oldVelocity.y = velocity.y; |
---|
731 | oldVelocity.z = velocity.z; |
---|
732 | PRINTF(0)("SENDING velocity\n"); |
---|
733 | sentSomething = true; |
---|
734 | SYNCHELP_WRITE_BYTE( DATA_velocity, NWT_SS_B ); |
---|
735 | SYNCHELP_WRITE_FLOAT( velocity.x, NWT_SS_VELX ); |
---|
736 | SYNCHELP_WRITE_FLOAT( velocity.y, NWT_SS_VELY ); |
---|
737 | SYNCHELP_WRITE_FLOAT( velocity.z, NWT_SS_VELZ ); |
---|
738 | } |
---|
739 | |
---|
740 | while ( Playable::needsReadSync() ) |
---|
741 | { |
---|
742 | sentSomething = true; |
---|
743 | PRINTF(0)("SYNCHELP_WRITE_FKT( Playable::readSync, NWT_SS_PL_SYNC )\n"); |
---|
744 | SYNCHELP_WRITE_BYTE( DATA_playables, NWT_SS_B ); |
---|
745 | SYNCHELP_WRITE_FKT( Playable::readSync, NWT_SS_PL_SYNC ); |
---|
746 | } |
---|
747 | |
---|
748 | } |
---|
749 | |
---|
750 | if ( !sentSomething ) |
---|
751 | { |
---|
752 | *reciever = 0; |
---|
753 | |
---|
754 | if ( networkCollisionList.size()>0 ) |
---|
755 | { |
---|
756 | SYNCHELP_WRITE_BYTE( DATA_collision, NWT_SS_B ); |
---|
757 | |
---|
758 | SYNCHELP_WRITE_INT( networkCollisionList.size(), NWT_SS_CO_N ); |
---|
759 | |
---|
760 | for ( std::list<float>::iterator it = networkCollisionList.begin(); it!=networkCollisionList.end(); it++ ) |
---|
761 | { |
---|
762 | SYNCHELP_WRITE_FLOAT( *it, NWT_SS_CO_CLID ); |
---|
763 | } |
---|
764 | |
---|
765 | networkCollisionList.clear(); |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | return SYNCHELP_WRITE_N; |
---|
770 | } |
---|
771 | |
---|
772 | |
---|
773 | void SpaceShip::doCollideNetwork( float energy ) |
---|
774 | { |
---|
775 | this->decreaseHealth( energy ); |
---|
776 | if( this->getHealth() <= 0) |
---|
777 | { |
---|
778 | this->die(); |
---|
779 | } |
---|
780 | } |
---|
781 | |
---|
782 | |
---|
783 | |
---|