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: Patrick Boenzli |
---|
13 | co-programmer: ... |
---|
14 | |
---|
15 | */ |
---|
16 | |
---|
17 | #include "fps_player.h" |
---|
18 | |
---|
19 | #include "interactive_model.h" |
---|
20 | #include "state.h" |
---|
21 | |
---|
22 | #include "src/lib/util/loading/factory.h" |
---|
23 | |
---|
24 | #include "md2/md2Model.h" |
---|
25 | |
---|
26 | #include "weapons/weapon_manager.h" |
---|
27 | #include "weapons/test_gun.h" |
---|
28 | #include "weapons/turret.h" |
---|
29 | #include "weapons/cannon.h" |
---|
30 | #include "weapons/fps_sniper_rifle.h" |
---|
31 | #include "weapons/aiming_system.h" |
---|
32 | |
---|
33 | #include "aabb.h" |
---|
34 | #include "bsp_entity.h" |
---|
35 | |
---|
36 | #include "key_mapper.h" |
---|
37 | |
---|
38 | #include "debug.h" |
---|
39 | |
---|
40 | #include "shared_network_data.h" |
---|
41 | #include "terrain.h" |
---|
42 | |
---|
43 | |
---|
44 | #include "class_id_DEPRECATED.h" |
---|
45 | ObjectListDefinitionID(FPSPlayer, CL_FPS_PLAYER); |
---|
46 | CREATE_FACTORY(FPSPlayer); |
---|
47 | |
---|
48 | #include "script_class.h" |
---|
49 | CREATE_SCRIPTABLE_CLASS(FPSPlayer, |
---|
50 | addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) |
---|
51 | ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) |
---|
52 | ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) |
---|
53 | ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) |
---|
54 | ); |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | * destructs the FPSPlayer, deletes alocated memory |
---|
59 | */ |
---|
60 | FPSPlayer::~FPSPlayer () |
---|
61 | { |
---|
62 | this->setPlayer(NULL); |
---|
63 | |
---|
64 | if( this->aimingSystem) |
---|
65 | delete this->aimingSystem; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | * creates a new FPSPlayer from Xml Data |
---|
71 | * @param root the xml element containing FPSPlayer data |
---|
72 | * |
---|
73 | */ |
---|
74 | FPSPlayer::FPSPlayer(const TiXmlElement* root) |
---|
75 | { |
---|
76 | this->init(); |
---|
77 | |
---|
78 | if (root != NULL) |
---|
79 | this->loadParams(root); |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | /** |
---|
85 | * initializes a FPSPlayer |
---|
86 | */ |
---|
87 | void FPSPlayer::init() |
---|
88 | { |
---|
89 | this->registerObject(this, FPSPlayer::_objectList); |
---|
90 | |
---|
91 | this->bLeft = false; |
---|
92 | this->bRight = false; |
---|
93 | this->bForward = false; |
---|
94 | this->bBackward = false; |
---|
95 | this->bJump = false; |
---|
96 | this->bPosBut = false; |
---|
97 | this->bFire = false; |
---|
98 | |
---|
99 | this->xMouse = 0.0f; |
---|
100 | this->yMouse = 0.0f; |
---|
101 | |
---|
102 | this->setHealthMax(100); |
---|
103 | this->setHealth(80); |
---|
104 | |
---|
105 | this->fallVelocity = 0.0f; |
---|
106 | this->jumpForce = 0.0f; |
---|
107 | |
---|
108 | this->cameraNode.setParent(this); |
---|
109 | |
---|
110 | this->attitude = this->getAbsDir().getAttitude(); |
---|
111 | this->heading = this->getAbsDir().getHeading(); |
---|
112 | |
---|
113 | //add events to the eventlist |
---|
114 | registerEvent(KeyMapper::PEV_FORWARD); |
---|
115 | registerEvent(KeyMapper::PEV_BACKWARD); |
---|
116 | registerEvent(KeyMapper::PEV_LEFT); |
---|
117 | registerEvent(KeyMapper::PEV_RIGHT); |
---|
118 | registerEvent(KeyMapper::PEV_FIRE1); |
---|
119 | registerEvent(KeyMapper::PEV_JUMP); |
---|
120 | registerEvent(EV_MOUSE_MOTION); |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | // weapon manager for the fps |
---|
125 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
---|
126 | |
---|
127 | Weapon* wpRight = new FPSSniperRifle(0); |
---|
128 | wpRight->setName("testGun Right"); |
---|
129 | /* Weapon* wpLeft = new TestGun(1);*/ |
---|
130 | // Weapon* wpLeft = new Turret(); |
---|
131 | // wpLeft->setName("testGun Left"); |
---|
132 | |
---|
133 | // this->addWeapon(wpLeft, 1, 0); |
---|
134 | if( State::isOnline()) |
---|
135 | this->addWeapon(wpRight,1, 0); |
---|
136 | this->getWeaponManager().changeWeaponConfig(1); |
---|
137 | |
---|
138 | this->getWeaponManager().setSlotCount(2); |
---|
139 | this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_4*-0.55f, Vector(0,0,1))); |
---|
140 | this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
141 | this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
142 | this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1)); |
---|
143 | this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0)); |
---|
144 | |
---|
145 | // this->getWeaponManager().getFixedTarget()->setRelDir(Quaternion(M_PI_4*-0.6f, Vector(0,0,1))); |
---|
146 | |
---|
147 | |
---|
148 | this->getWeaponManager().setParentNode(&this->cameraNode); |
---|
149 | this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
150 | |
---|
151 | this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode); |
---|
152 | this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0); |
---|
153 | |
---|
154 | this->aimingSystem = new AimingSystem(this); |
---|
155 | //this->addChild(this->aimingSystem); |
---|
156 | wpRight->addChild(this->aimingSystem); |
---|
157 | // this->getWeaponManager().sl |
---|
158 | |
---|
159 | |
---|
160 | // network registration |
---|
161 | registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) ); |
---|
162 | registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) ); |
---|
163 | registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) ); |
---|
164 | registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) ); |
---|
165 | registerVar( new SynchronizeableBool( &bJump, &bJump, "bJump", PERMISSION_OWNER ) ); |
---|
166 | registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) ); |
---|
167 | registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) ); |
---|
168 | |
---|
169 | //subscribe to collision reaction |
---|
170 | //this->subscribeReaction(CREngine::CR_PHYSICS_FULL_WALK, BspEntity::staticClassID()); |
---|
171 | this->subscribeReaction(CREngine::CR_PHYSICS_FULL_WALK, Terrain::staticClassID()); |
---|
172 | |
---|
173 | this->initWeapon = false; |
---|
174 | this->damageTicker = 0.0f; |
---|
175 | |
---|
176 | if( State::isOnline()) |
---|
177 | toList( OM_PLAYERS ); |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | /** |
---|
182 | * loads the Settings of a FPSPlayer from an XML-element. |
---|
183 | * @param root the XML-element to load the Spaceship's properties from |
---|
184 | */ |
---|
185 | void FPSPlayer::loadParams(const TiXmlElement* root) |
---|
186 | { |
---|
187 | Playable::loadParams(root); |
---|
188 | } |
---|
189 | |
---|
190 | void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed) |
---|
191 | { |
---|
192 | this->attitude = this->getAbsDir().getAttitude(); |
---|
193 | this->heading = this->getAbsDir().getHeading(); |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | void FPSPlayer::reset() |
---|
198 | { |
---|
199 | this->bLeft = false; |
---|
200 | this->bRight = false; |
---|
201 | this->bForward = false; |
---|
202 | this->bBackward = false; |
---|
203 | this->xMouse = 0.0f; |
---|
204 | this->yMouse = 0.0f; |
---|
205 | |
---|
206 | this->setHealth(80); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | void FPSPlayer::enter() |
---|
211 | { |
---|
212 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true ); |
---|
213 | |
---|
214 | State::getCameraNode()->setParentSoft(&this->cameraNode); |
---|
215 | State::getCameraTargetNode()->setParentSoft(&this->cameraNode); |
---|
216 | |
---|
217 | this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode()); |
---|
218 | this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0); |
---|
219 | |
---|
220 | if ( !State::isOnline() ) |
---|
221 | { |
---|
222 | this->respawn(); |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | void FPSPlayer::leave() |
---|
227 | { |
---|
228 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
---|
229 | this->detachCamera(); |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | |
---|
234 | /** |
---|
235 | * the function called for each passing timeSnap |
---|
236 | * @param time The timespan passed since last update |
---|
237 | */ |
---|
238 | void FPSPlayer::tick (float time) |
---|
239 | { |
---|
240 | |
---|
241 | if ( !this->initWeapon ) |
---|
242 | { |
---|
243 | this->initWeapon = true; |
---|
244 | |
---|
245 | this->cameraNode.setParentMode(PNODE_ROTATE_AND_MOVE); |
---|
246 | |
---|
247 | this->getWeaponManager().getParentNode()->setParentMode(PNODE_ROTATE_AND_MOVE); |
---|
248 | this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode); |
---|
249 | this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ROTATE_AND_MOVE); |
---|
250 | |
---|
251 | |
---|
252 | this->aimingSystem->toList(OM_GROUP_01); |
---|
253 | this->aimingSystem->setParent(&this->cameraNode); |
---|
254 | // this->aimingSystem->setParentMode(PNODE_ROTATE_AND_MOVE); |
---|
255 | this->aimingSystem->setRelDir(Quaternion(M_PI_4*-0.58f, Vector(0,0,1))); |
---|
256 | this->aimingSystem->setRelCoor(0, -1, -1); |
---|
257 | |
---|
258 | |
---|
259 | AABB* box = this->getModelAABB(); |
---|
260 | if( box != NULL) |
---|
261 | { |
---|
262 | float f = 1.0; |
---|
263 | this->cameraNode.setRelCoor(0, box->halfLength[1] * f, 0); |
---|
264 | // this->cameraNode.setRelCoor(10, box->halfLength[1] * f, 0); |
---|
265 | |
---|
266 | float v = 0.1f; |
---|
267 | this->getWeaponManager().setSlotPosition(0, Vector(-8.0, box->halfLength[1] * v, 1.1)); |
---|
268 | this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * v, 0.0)); |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | this->getWeaponManager().tick(time); |
---|
274 | if( this->bFire) |
---|
275 | { |
---|
276 | this->getWeaponManager().fire(); |
---|
277 | |
---|
278 | // WorldEntity* target = this->aimingSystem->getNearestTarget(); |
---|
279 | // if( target != NULL) |
---|
280 | // { |
---|
281 | // PRINTF(0)("hit hit hit, got: %s\n", target->getClassCName()); |
---|
282 | // } |
---|
283 | // else |
---|
284 | // { |
---|
285 | // PRINTF(0)("nothing hit\n"); |
---|
286 | // } |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | //dealing damage |
---|
291 | |
---|
292 | if ( State::isOnline() && (SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/)) |
---|
293 | { |
---|
294 | this->damageTicker -= time; |
---|
295 | |
---|
296 | if ( this->damageTicker <= 0.0f && this->beFire() ) |
---|
297 | { |
---|
298 | this->damageTicker = 0.25; |
---|
299 | |
---|
300 | WorldEntity * victim = aimingSystem->getNearestTarget(); |
---|
301 | |
---|
302 | if ( victim ) |
---|
303 | { |
---|
304 | PRINTF(0)("FIRE: hit %s\n", victim->getClassCName()); |
---|
305 | victim->hit( 20, this ); |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | PRINTF(0)("FIRE: nothing hit\n"); |
---|
310 | } |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) ) |
---|
316 | { |
---|
317 | xMouse *= time ; |
---|
318 | yMouse *= time ; |
---|
319 | |
---|
320 | heading -= xMouse; |
---|
321 | attitude-= yMouse; |
---|
322 | |
---|
323 | |
---|
324 | if ( attitude > 1.95 ) |
---|
325 | attitude = 1.95; |
---|
326 | else if ( attitude < -1.07 ) |
---|
327 | attitude = -1.07; |
---|
328 | |
---|
329 | xMouse = yMouse = 0; |
---|
330 | } |
---|
331 | |
---|
332 | this->setAbsDir(Quaternion(heading, Vector(0,1,0))); |
---|
333 | this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) )); |
---|
334 | |
---|
335 | Vector velocity; |
---|
336 | |
---|
337 | if ( this->bForward ) |
---|
338 | { |
---|
339 | velocity += this->getAbsDirX(); |
---|
340 | } |
---|
341 | |
---|
342 | if ( this->bBackward ) |
---|
343 | { |
---|
344 | velocity -= this->getAbsDirX(); |
---|
345 | } |
---|
346 | |
---|
347 | if ( this->bRight ) |
---|
348 | { |
---|
349 | velocity += this->getAbsDirZ(); |
---|
350 | } |
---|
351 | |
---|
352 | if ( this->bLeft ) |
---|
353 | { |
---|
354 | velocity -= this->getAbsDirZ(); |
---|
355 | } |
---|
356 | |
---|
357 | |
---|
358 | velocity *= 100; |
---|
359 | |
---|
360 | if( this->bJump && likely(this->getModel(0) != NULL)) |
---|
361 | { |
---|
362 | if( this->jumpForce < 1.0f) |
---|
363 | { |
---|
364 | this->jumpForce = 300.0f; |
---|
365 | |
---|
366 | if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP) |
---|
367 | ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP); |
---|
368 | } |
---|
369 | } |
---|
370 | else if(velocity.len() != 0.0f) |
---|
371 | { |
---|
372 | if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN) |
---|
373 | ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); |
---|
374 | } |
---|
375 | else |
---|
376 | { |
---|
377 | if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND) |
---|
378 | ((InteractiveModel*)this->getModel(0))->setAnimation(STAND); |
---|
379 | } |
---|
380 | |
---|
381 | |
---|
382 | velocity.y += this->jumpForce; |
---|
383 | if( this->jumpForce > 1.0f) |
---|
384 | this->jumpForce *= 0.9f; |
---|
385 | |
---|
386 | |
---|
387 | // physical falling of the player |
---|
388 | if( !this->isOnGround()) |
---|
389 | { |
---|
390 | this->fallVelocity += 300.0f * time; |
---|
391 | velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity; |
---|
392 | |
---|
393 | // PRINTF(0)("vel %f\n", this->fallVelocity); |
---|
394 | } |
---|
395 | else |
---|
396 | { |
---|
397 | this->fallVelocity = 0.0f; |
---|
398 | } |
---|
399 | |
---|
400 | this->shiftCoor( velocity*time ); |
---|
401 | |
---|
402 | |
---|
403 | |
---|
404 | |
---|
405 | |
---|
406 | |
---|
407 | |
---|
408 | if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL)) |
---|
409 | { |
---|
410 | ((InteractiveModel*)this->getModel(0))->tick(time); |
---|
411 | |
---|
412 | // handle animations differently |
---|
413 | |
---|
414 | |
---|
415 | |
---|
416 | |
---|
417 | |
---|
418 | // else if( this->bFire && likely(this->getModel(0) != NULL)) |
---|
419 | // { |
---|
420 | // if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK) |
---|
421 | // ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK); |
---|
422 | // } |
---|
423 | // else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL)) |
---|
424 | // { |
---|
425 | // if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN) |
---|
426 | // ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); |
---|
427 | // } |
---|
428 | // else if (likely(this->getModel(0) != NULL)) |
---|
429 | // { |
---|
430 | // if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND) |
---|
431 | // ((InteractiveModel*)this->getModel(0))->setAnimation(STAND); |
---|
432 | // } |
---|
433 | } |
---|
434 | |
---|
435 | this->setOnGround(false); |
---|
436 | this->aimingSystem->flushList(); |
---|
437 | } |
---|
438 | |
---|
439 | |
---|
440 | |
---|
441 | /** |
---|
442 | * draws the MD2Creature after transforming it. |
---|
443 | */ |
---|
444 | void FPSPlayer::draw () const |
---|
445 | { |
---|
446 | // only draw if this entity is not the player since the player nevers sees himself |
---|
447 | if( this->getCurrentPlayer() == NULL) |
---|
448 | WorldEntity::draw(); |
---|
449 | } |
---|
450 | |
---|
451 | |
---|
452 | |
---|
453 | /** |
---|
454 | * process |
---|
455 | */ |
---|
456 | void FPSPlayer::process(const Event &event) |
---|
457 | { |
---|
458 | Playable::process(event); |
---|
459 | |
---|
460 | if( event.type == KeyMapper::PEV_LEFT) |
---|
461 | this->bLeft = event.bPressed; |
---|
462 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
463 | this->bRight = event.bPressed; |
---|
464 | else if( event.type == KeyMapper::PEV_FORWARD) |
---|
465 | this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
466 | else if( event.type == KeyMapper::PEV_BACKWARD) |
---|
467 | this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
468 | else if( event.type == EV_MOUSE_MOTION) |
---|
469 | { |
---|
470 | this->xMouse += event.xRel; |
---|
471 | this->yMouse += event.yRel; |
---|
472 | } |
---|
473 | else if( event.type == KeyMapper::PEV_JUMP) |
---|
474 | { |
---|
475 | this->bJump = event.bPressed; |
---|
476 | } |
---|
477 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
478 | { |
---|
479 | this->bFire = event.bPressed; |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | |
---|
484 | void FPSPlayer::respawn( ) |
---|
485 | { |
---|
486 | if( State::isOnline()) |
---|
487 | toList( OM_PLAYERS ); |
---|
488 | |
---|
489 | this->damageTicker = 0.0f; |
---|
490 | |
---|
491 | Playable::respawn(); |
---|
492 | } |
---|
493 | |
---|
494 | |
---|
495 | void FPSPlayer::destroy( WorldEntity* killer ) |
---|
496 | { |
---|
497 | Playable::destroy( killer ); |
---|
498 | |
---|
499 | toList( OM_DEAD ); |
---|
500 | } |
---|
501 | |
---|