1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: Christian Meyer |
---|
16 | */ |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
18 | |
---|
19 | #include "world_entity.h" |
---|
20 | #include "shell_command.h" |
---|
21 | |
---|
22 | #include "model.h" |
---|
23 | #include "md2/md2Model.h" |
---|
24 | #include "md3/md3_model.h" |
---|
25 | |
---|
26 | #include "aabb_tree_node.h" |
---|
27 | |
---|
28 | #include "util/loading/resource_manager.h" |
---|
29 | #include "util/loading/load_param.h" |
---|
30 | #include "vector.h" |
---|
31 | #include "obb_tree.h" |
---|
32 | |
---|
33 | #include "glgui_bar.h" |
---|
34 | |
---|
35 | #include "state.h" |
---|
36 | #include "camera.h" |
---|
37 | |
---|
38 | #include "collision_handle.h" |
---|
39 | #include "collision_event.h" |
---|
40 | #include "game_rules.h" |
---|
41 | #include "kill.h" |
---|
42 | |
---|
43 | #include <stdarg.h> |
---|
44 | |
---|
45 | |
---|
46 | using namespace std; |
---|
47 | |
---|
48 | SHELL_COMMAND(model, WorldEntity, loadModel) |
---|
49 | ->describe("sets the Model of the WorldEntity") |
---|
50 | ->defaultValues("models/ships/fighter.obj", 1.0f); |
---|
51 | |
---|
52 | SHELL_COMMAND(debugEntity, WorldEntity, debugWE); |
---|
53 | |
---|
54 | /** |
---|
55 | * Loads the WordEntity-specific Part of any derived Class |
---|
56 | * |
---|
57 | * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves, |
---|
58 | * that can calls WorldEntities loadParams for itself. |
---|
59 | */ |
---|
60 | WorldEntity::WorldEntity() |
---|
61 | : Synchronizeable() |
---|
62 | { |
---|
63 | this->setClassID(CL_WORLD_ENTITY, "WorldEntity"); |
---|
64 | |
---|
65 | this->obbTree = NULL; |
---|
66 | this->aabbNode = NULL; |
---|
67 | this->healthWidget = NULL; |
---|
68 | this->healthMax = 1.0f; |
---|
69 | this->health = 1.0f; |
---|
70 | this->damage = 0.0f; // no damage dealt by a default entity |
---|
71 | this->scaling = 1.0f; |
---|
72 | |
---|
73 | /* OSOLETE */ |
---|
74 | this->bVisible = true; |
---|
75 | this->bCollide = true; |
---|
76 | |
---|
77 | this->objectListNumber = OM_INIT; |
---|
78 | this->objectListIterator = NULL; |
---|
79 | |
---|
80 | // reset all collision handles to NULL == unsubscribed state |
---|
81 | for(int i = 0; i < CREngine::CR_NUMBER; ++i) |
---|
82 | this->collisionHandles[i] = NULL; |
---|
83 | this->bReactive = false; |
---|
84 | |
---|
85 | // registering default reactions: |
---|
86 | this->subscribeReaction(CREngine::CR_OBJECT_DAMAGE, CL_WORLD_ENTITY); |
---|
87 | |
---|
88 | this->toList(OM_NULL); |
---|
89 | |
---|
90 | modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) ); |
---|
91 | scaling_handle = registerVarId( new SynchronizeableFloat( &scaling, &scaling, "scaling" ) ); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * standard destructor |
---|
96 | */ |
---|
97 | WorldEntity::~WorldEntity () |
---|
98 | { |
---|
99 | State::getObjectManager()->toList(this, OM_INIT); |
---|
100 | |
---|
101 | // Delete the model (unregister it with the ResourceManager) |
---|
102 | for (unsigned int i = 0; i < this->models.size(); i++) |
---|
103 | this->setModel(NULL, i); |
---|
104 | |
---|
105 | // Delete the obbTree |
---|
106 | if( this->obbTree != NULL) |
---|
107 | delete this->obbTree; |
---|
108 | |
---|
109 | if (this->healthWidget != NULL) |
---|
110 | delete this->healthWidget; |
---|
111 | |
---|
112 | this->unsubscribeReaction(); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * loads the WorldEntity Specific Parameters. |
---|
117 | * @param root: the XML-Element to load the Data From |
---|
118 | */ |
---|
119 | void WorldEntity::loadParams(const TiXmlElement* root) |
---|
120 | { |
---|
121 | // Do the PNode loading stuff |
---|
122 | PNode::loadParams(root); |
---|
123 | |
---|
124 | LoadParam(root, "md2texture", this, WorldEntity, loadMD2Texture) |
---|
125 | .describe("the fileName of the texture, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
---|
126 | .defaultValues(""); |
---|
127 | |
---|
128 | // Model Loading |
---|
129 | LoadParam(root, "model", this, WorldEntity, loadModel) |
---|
130 | .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
---|
131 | .defaultValues("", 1.0f, 0); |
---|
132 | |
---|
133 | LoadParam(root, "maxHealth", this, WorldEntity, setHealthMax) |
---|
134 | .describe("The Maximum health that can be loaded onto this entity") |
---|
135 | .defaultValues(1.0f); |
---|
136 | |
---|
137 | LoadParam(root, "health", this, WorldEntity, setHealth) |
---|
138 | .describe("The Health the WorldEntity has at this moment") |
---|
139 | .defaultValues(1.0f); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | * loads a Model onto a WorldEntity |
---|
145 | * @param fileName the name of the model to load |
---|
146 | * @param scaling the Scaling of the model |
---|
147 | * |
---|
148 | * FIXME |
---|
149 | * @todo: separate the obb tree generation from the model |
---|
150 | */ |
---|
151 | void WorldEntity::loadModel(const std::string& fileName, float scaling, unsigned int modelNumber, unsigned int obbTreeDepth) |
---|
152 | { |
---|
153 | this->modelLODName = fileName; |
---|
154 | this->scaling = scaling; |
---|
155 | |
---|
156 | std::string name = fileName; |
---|
157 | |
---|
158 | if ( name.find( ResourceManager::getInstance()->getDataDir() ) == 0 ) |
---|
159 | { |
---|
160 | name.erase(ResourceManager::getInstance()->getDataDir().size()); |
---|
161 | } |
---|
162 | |
---|
163 | this->modelFileName = name; |
---|
164 | |
---|
165 | if (!fileName.empty()) |
---|
166 | { |
---|
167 | // search for the special character # in the LoadParam |
---|
168 | if (fileName.find('#') != std::string::npos) |
---|
169 | { |
---|
170 | PRINTF(4)("Found # in %s... searching for LOD's\n", fileName.c_str()); |
---|
171 | std::string lodFile = fileName; |
---|
172 | unsigned int offset = lodFile.find('#'); |
---|
173 | for (unsigned int i = 0; i < 3; i++) |
---|
174 | { |
---|
175 | lodFile[offset] = 48+(int)i; |
---|
176 | if (ResourceManager::isInDataDir(lodFile)) |
---|
177 | this->loadModel(lodFile, scaling, i); |
---|
178 | } |
---|
179 | return; |
---|
180 | } |
---|
181 | if (this->scaling <= 0.0) |
---|
182 | { |
---|
183 | PRINTF(1)("YOU GAVE ME A CRAPY SCALE resetting to 1.0\n"); |
---|
184 | this->scaling = 1.0; |
---|
185 | } |
---|
186 | if(fileName.find(".obj") != std::string::npos) |
---|
187 | { |
---|
188 | PRINTF(4)("fetching OBJ file: %s\n", fileName.c_str()); |
---|
189 | BaseObject* loadedModel = ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, this->scaling); |
---|
190 | if (loadedModel != NULL) |
---|
191 | this->setModel(dynamic_cast<Model*>(loadedModel), modelNumber); |
---|
192 | else |
---|
193 | PRINTF(1)("OBJ-File %s not found.\n", fileName.c_str()); |
---|
194 | |
---|
195 | if( modelNumber == 0) |
---|
196 | this->buildObbTree(obbTreeDepth); |
---|
197 | } |
---|
198 | else if(fileName.find(".md2") != std::string::npos) |
---|
199 | { |
---|
200 | PRINTF(4)("fetching MD2 file: %s\n", fileName.c_str()); |
---|
201 | Model* m = new MD2Model(fileName, this->md2TextureFileName, this->scaling); |
---|
202 | //this->setModel((Model*)ResourceManager::getInstance()->load(fileName, MD2, RP_CAMPAIGN), 0); |
---|
203 | this->setModel(m, 0); |
---|
204 | |
---|
205 | if( m != NULL) |
---|
206 | this->buildObbTree(obbTreeDepth); |
---|
207 | } |
---|
208 | else /*if(fileName.find(".md3") != std::string::npos)*/ |
---|
209 | { |
---|
210 | PRINTF(4)("fetching MD3 file: %s\n", fileName.c_str()); |
---|
211 | Model* m = new md3::MD3Model(fileName, this->scaling); |
---|
212 | this->setModel(m, 0); |
---|
213 | |
---|
214 | // if( m != NULL) |
---|
215 | // this->buildObbTree(obbTreeDepth); |
---|
216 | } |
---|
217 | } |
---|
218 | else |
---|
219 | { |
---|
220 | this->setModel(NULL); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * sets a specific Model for the Object. |
---|
226 | * @param model The Model to set |
---|
227 | * @param modelNumber the n'th model in the List to get. |
---|
228 | */ |
---|
229 | void WorldEntity::setModel(Model* model, unsigned int modelNumber) |
---|
230 | { |
---|
231 | if (this->models.size() <= modelNumber) |
---|
232 | this->models.resize(modelNumber+1, NULL); |
---|
233 | |
---|
234 | if (this->models[modelNumber] != NULL) |
---|
235 | { |
---|
236 | Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(dynamic_cast<BaseObject*>(this->models[modelNumber])); |
---|
237 | if (resource != NULL) |
---|
238 | ResourceManager::getInstance()->unload(resource, RP_LEVEL); |
---|
239 | else |
---|
240 | { |
---|
241 | PRINTF(4)("Forcing model deletion\n"); |
---|
242 | delete this->models[modelNumber]; |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | this->models[modelNumber] = model; |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | /** |
---|
251 | * builds the obb-tree |
---|
252 | * @param depth the depth to calculate |
---|
253 | */ |
---|
254 | bool WorldEntity::buildObbTree(int depth) |
---|
255 | { |
---|
256 | if (this->obbTree) |
---|
257 | delete this->obbTree; |
---|
258 | |
---|
259 | if (this->models[0] != NULL) |
---|
260 | this->obbTree = new OBBTree(depth, models[0]->getModelInfo(), this); |
---|
261 | else |
---|
262 | { |
---|
263 | PRINTF(1)("could not create obb-tree, because no model was loaded yet\n"); |
---|
264 | this->obbTree = NULL; |
---|
265 | return false; |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | // create the axis aligned bounding box |
---|
270 | if( this->aabbNode != NULL) |
---|
271 | { |
---|
272 | delete this->aabbNode; |
---|
273 | this->aabbNode = NULL; |
---|
274 | } |
---|
275 | |
---|
276 | if( this->models[0] != NULL) { |
---|
277 | this->aabbNode = new AABBTreeNode(); |
---|
278 | this->aabbNode->spawnBVTree(this->models[0]); |
---|
279 | } |
---|
280 | return true; |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | /** |
---|
285 | * subscribes this world entity to a collision reaction |
---|
286 | * @param type the type of reaction to subscribe to |
---|
287 | * @param target1 a filter target (classID) |
---|
288 | */ |
---|
289 | void WorldEntity::subscribeReaction(CREngine::CRType type, long target1) |
---|
290 | { |
---|
291 | this->subscribeReaction(type); |
---|
292 | |
---|
293 | // add the target filter |
---|
294 | this->collisionHandles[type]->addTarget(target1); |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | /** |
---|
299 | * subscribes this world entity to a collision reaction |
---|
300 | * @param type the type of reaction to subscribe to |
---|
301 | * @param target1 a filter target (classID) |
---|
302 | */ |
---|
303 | void WorldEntity::subscribeReaction(CREngine::CRType type, long target1, long target2) |
---|
304 | { |
---|
305 | this->subscribeReaction(type); |
---|
306 | |
---|
307 | // add the target filter |
---|
308 | this->collisionHandles[type]->addTarget(target1); |
---|
309 | this->collisionHandles[type]->addTarget(target2); |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | /** |
---|
314 | * subscribes this world entity to a collision reaction |
---|
315 | * @param type the type of reaction to subscribe to |
---|
316 | * @param target1 a filter target (classID) |
---|
317 | */ |
---|
318 | void WorldEntity::subscribeReaction(CREngine::CRType type, long target1, long target2, long target3) |
---|
319 | { |
---|
320 | this->subscribeReaction(type); |
---|
321 | |
---|
322 | // add the target filter |
---|
323 | this->collisionHandles[type]->addTarget(target1); |
---|
324 | this->collisionHandles[type]->addTarget(target2); |
---|
325 | this->collisionHandles[type]->addTarget(target3); |
---|
326 | } |
---|
327 | |
---|
328 | |
---|
329 | /** |
---|
330 | * subscribes this world entity to a collision reaction |
---|
331 | * @param type the type of reaction to subscribe to |
---|
332 | * @param target1 a filter target (classID) |
---|
333 | */ |
---|
334 | void WorldEntity::subscribeReaction(CREngine::CRType type, long target1, long target2, long target3, long target4) |
---|
335 | { |
---|
336 | this->subscribeReaction(type); |
---|
337 | |
---|
338 | // add the target filter |
---|
339 | this->collisionHandles[type]->addTarget(target1); |
---|
340 | this->collisionHandles[type]->addTarget(target2); |
---|
341 | this->collisionHandles[type]->addTarget(target3); |
---|
342 | this->collisionHandles[type]->addTarget(target4); |
---|
343 | } |
---|
344 | |
---|
345 | |
---|
346 | /** |
---|
347 | * subscribes this world entity to a collision reaction |
---|
348 | * @param type the type of reaction to subscribe to |
---|
349 | * @param nrOfTargets number of target filters |
---|
350 | * @param ... the targets as classIDs |
---|
351 | */ |
---|
352 | void WorldEntity::subscribeReaction(CREngine::CRType type) |
---|
353 | { |
---|
354 | if( this->collisionHandles[type] != NULL) { |
---|
355 | PRINTF(2)("Registering for a CollisionReaction already subscribed to! Skipping\n"); |
---|
356 | return; |
---|
357 | } |
---|
358 | |
---|
359 | this->collisionHandles[type] = CREngine::getInstance()->subscribeReaction(this, type); |
---|
360 | |
---|
361 | // now there is at least one collision reaction subscribed |
---|
362 | this->bReactive = true; |
---|
363 | } |
---|
364 | |
---|
365 | |
---|
366 | /** |
---|
367 | * unsubscribes a specific reaction from the worldentity |
---|
368 | * @param type the reaction to unsubscribe |
---|
369 | */ |
---|
370 | void WorldEntity::unsubscribeReaction(CREngine::CRType type) |
---|
371 | { |
---|
372 | if( this->collisionHandles[type] == NULL) |
---|
373 | return; |
---|
374 | |
---|
375 | CREngine::getInstance()->unsubscribeReaction(this->collisionHandles[type]); |
---|
376 | this->collisionHandles[type] = NULL; |
---|
377 | |
---|
378 | // check if there is still any handler registered |
---|
379 | for(int i = 0; i < CREngine::CR_NUMBER; ++i) |
---|
380 | { |
---|
381 | if( this->collisionHandles[i] != NULL) |
---|
382 | { |
---|
383 | this->bReactive = true; |
---|
384 | return; |
---|
385 | } |
---|
386 | } |
---|
387 | this->bReactive = false; |
---|
388 | } |
---|
389 | |
---|
390 | |
---|
391 | /** |
---|
392 | * unsubscribes all collision reactions |
---|
393 | */ |
---|
394 | void WorldEntity::unsubscribeReaction() |
---|
395 | { |
---|
396 | for( int i = 0; i < CREngine::CR_NUMBER; i++) |
---|
397 | this->unsubscribeReaction((CREngine::CRType)i); |
---|
398 | |
---|
399 | // there are no reactions subscribed from now on |
---|
400 | this->bReactive = false; |
---|
401 | } |
---|
402 | |
---|
403 | |
---|
404 | /** |
---|
405 | * registers a new collision event to this world entity |
---|
406 | * @param entityA entity of the collision |
---|
407 | * @param entityB entity of the collision |
---|
408 | * @param bvA colliding bounding volume of entityA |
---|
409 | * @param bvB colliding bounding volume of entityA |
---|
410 | */ |
---|
411 | bool WorldEntity::registerCollision(WorldEntity* entityA, WorldEntity* entityB, BoundingVolume* bvA, BoundingVolume* bvB) |
---|
412 | { |
---|
413 | // is there any handler listening? |
---|
414 | if( !this->bReactive) |
---|
415 | return false; |
---|
416 | |
---|
417 | // get a collision event |
---|
418 | CollisionEvent* c = CREngine::getInstance()->popCollisionEventObject(); |
---|
419 | assert(c != NULL); // if this should fail: we got not enough precached CollisionEvents: alter value in cr_defs.h |
---|
420 | c->collide(entityA, entityB, bvA, bvB); |
---|
421 | |
---|
422 | for( int i = 0; i < CREngine::CR_NUMBER; ++i) |
---|
423 | if( this->collisionHandles[i] != NULL) |
---|
424 | this->collisionHandles[i]->registerCollisionEvent(c); |
---|
425 | return true; |
---|
426 | } |
---|
427 | |
---|
428 | |
---|
429 | /** |
---|
430 | * registers a new collision event to this woeld entity |
---|
431 | * @param entity the entity that collides |
---|
432 | * @param plane it stands on |
---|
433 | * @param position it collides on the plane |
---|
434 | */ |
---|
435 | bool WorldEntity::registerCollision(WorldEntity* entity, WorldEntity* groundEntity, Vector normal, Vector position) |
---|
436 | { |
---|
437 | // is there any handler listening? |
---|
438 | if( !this->bReactive) |
---|
439 | return false; |
---|
440 | |
---|
441 | // get a collision event |
---|
442 | CollisionEvent* c = CREngine::getInstance()->popCollisionEventObject(); |
---|
443 | assert(c != NULL); // if this should fail: we got not enough precached CollisionEvents: alter value in cr_defs.h |
---|
444 | c->collide(entity, groundEntity, normal, position); |
---|
445 | |
---|
446 | for( int i = 0; i < CREngine::CR_NUMBER; ++i) |
---|
447 | if( this->collisionHandles[i] != NULL) |
---|
448 | this->collisionHandles[i]->registerCollisionEvent(c); |
---|
449 | return true; |
---|
450 | } |
---|
451 | |
---|
452 | |
---|
453 | /** |
---|
454 | * @brief moves this entity to the List OM_List |
---|
455 | * @param list the list to set this Entity to. |
---|
456 | * |
---|
457 | * this is the same as a call to State::getObjectManager()->toList(entity , list); |
---|
458 | * directly, but with an easier interface. |
---|
459 | * |
---|
460 | * @todo inline this (peut etre) |
---|
461 | */ |
---|
462 | void WorldEntity::toList(OM_LIST list) |
---|
463 | { |
---|
464 | State::getObjectManager()->toList(this, list); |
---|
465 | } |
---|
466 | |
---|
467 | void WorldEntity::toReflectionList() |
---|
468 | { |
---|
469 | State::getObjectManager()->toReflectionList( this ); |
---|
470 | } |
---|
471 | |
---|
472 | void removeFromReflectionList() |
---|
473 | { |
---|
474 | /// TODO |
---|
475 | /// State::getObject |
---|
476 | } |
---|
477 | |
---|
478 | /** |
---|
479 | * sets the character attributes of a worldentity |
---|
480 | * @param character attributes |
---|
481 | * |
---|
482 | * these attributes don't have to be set, only use them, if you need them |
---|
483 | */ |
---|
484 | //void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr) |
---|
485 | //{} |
---|
486 | |
---|
487 | |
---|
488 | /** |
---|
489 | * this function is called, when two entities collide |
---|
490 | * @param entity: the world entity with whom it collides |
---|
491 | * |
---|
492 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
493 | */ |
---|
494 | void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location) |
---|
495 | { |
---|
496 | /** |
---|
497 | * THIS IS A DEFAULT COLLISION-Effect. |
---|
498 | * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT |
---|
499 | * USE:: |
---|
500 | * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; }; |
---|
501 | * |
---|
502 | * You can always define a default Action.... don't be affraid just test it :) |
---|
503 | */ |
---|
504 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
505 | } |
---|
506 | |
---|
507 | |
---|
508 | /** |
---|
509 | * this function is called, when two entities collide |
---|
510 | * @param entity: the world entity with whom it collides |
---|
511 | * |
---|
512 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
513 | */ |
---|
514 | void WorldEntity::collidesWithGround(const Vector& location) |
---|
515 | { |
---|
516 | PRINTF(0)("BSP_GROUND: %s collides \n", this->getClassName() ); |
---|
517 | } |
---|
518 | |
---|
519 | void WorldEntity::collidesWithGround(const Vector& feet, const Vector& ray_1, const Vector& ray_2) |
---|
520 | { |
---|
521 | |
---|
522 | // PRINTF(0)("BSP_GROUND: Player collides \n", this->getClassName() ); |
---|
523 | |
---|
524 | Vector v = this->getAbsDirX(); |
---|
525 | v.x *= 10.1; |
---|
526 | v.y *= 10.1; |
---|
527 | v.z *= 10.1; |
---|
528 | Vector u = Vector(0.0,-20.0,0.0); |
---|
529 | |
---|
530 | |
---|
531 | if(!(this->getAbsCoor().x == ray_2.x && this->getAbsCoor().y == ray_2.y && this->getAbsCoor().z == ray_2.z) ) |
---|
532 | { |
---|
533 | |
---|
534 | this->setAbsCoor(ray_2 - v); |
---|
535 | |
---|
536 | } |
---|
537 | else |
---|
538 | { |
---|
539 | if(ray_1.x == this->getAbsCoor().x + v.x && ray_1.y == this->getAbsCoor().y + v.y + 0.1 && ray_1.z ==this->getAbsCoor().z + v.z) |
---|
540 | { |
---|
541 | this->setAbsCoor(feet -u ); |
---|
542 | } |
---|
543 | |
---|
544 | this->setAbsCoor(ray_2 - v); |
---|
545 | |
---|
546 | } |
---|
547 | |
---|
548 | |
---|
549 | } |
---|
550 | |
---|
551 | /** |
---|
552 | * this is called immediately after the Entity has been constructed, initialized and then Spawned into the World |
---|
553 | * |
---|
554 | */ |
---|
555 | void WorldEntity::postSpawn () |
---|
556 | {} |
---|
557 | |
---|
558 | |
---|
559 | /** |
---|
560 | * this method is called by the world if the WorldEntity leaves the game |
---|
561 | */ |
---|
562 | void WorldEntity::leaveWorld () |
---|
563 | {} |
---|
564 | |
---|
565 | |
---|
566 | /** |
---|
567 | * resets the WorldEntity to its initial values. eg. used for multiplayer games: respawning |
---|
568 | */ |
---|
569 | void WorldEntity::reset() |
---|
570 | {} |
---|
571 | |
---|
572 | /** |
---|
573 | * this method is called every frame |
---|
574 | * @param time: the time in seconds that has passed since the last tick |
---|
575 | * |
---|
576 | * Handle all stuff that should update with time inside this method (movement, animation, etc.) |
---|
577 | */ |
---|
578 | void WorldEntity::tick(float time) |
---|
579 | {} |
---|
580 | |
---|
581 | |
---|
582 | /** |
---|
583 | * the entity is drawn onto the screen with this function |
---|
584 | * |
---|
585 | * This is a central function of an entity: call it to let the entity painted to the screen. |
---|
586 | * Just override this function with whatever you want to be drawn. |
---|
587 | */ |
---|
588 | void WorldEntity::draw() const |
---|
589 | { |
---|
590 | //PRINTF(0)("(%s::%s)\n", this->getClassName(), this->getName()); |
---|
591 | // assert(!unlikely(this->models.empty())); |
---|
592 | { |
---|
593 | glMatrixMode(GL_MODELVIEW); |
---|
594 | glPushMatrix(); |
---|
595 | |
---|
596 | /* translate */ |
---|
597 | glTranslatef (this->getAbsCoor ().x, |
---|
598 | this->getAbsCoor ().y, |
---|
599 | this->getAbsCoor ().z); |
---|
600 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
601 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
602 | |
---|
603 | |
---|
604 | // This Draws the LOD's |
---|
605 | float cameraDistance = State::getCamera()->distance(this); |
---|
606 | if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL) |
---|
607 | { |
---|
608 | this->models[2]->draw(); |
---|
609 | } |
---|
610 | else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL) |
---|
611 | { |
---|
612 | this->models[1]->draw(); |
---|
613 | } |
---|
614 | else if (this->models.size() >= 1 && this->models[0] != NULL) |
---|
615 | { |
---|
616 | this->models[0]->draw(); |
---|
617 | } |
---|
618 | |
---|
619 | if( this->aabbNode != NULL) |
---|
620 | this->aabbNode->drawBV(0, DRAW_BV_POLYGON, Vector(1, 0.6, 0.2), true); |
---|
621 | |
---|
622 | glPopMatrix(); |
---|
623 | } |
---|
624 | } |
---|
625 | |
---|
626 | /** |
---|
627 | * @param health the Health to add. |
---|
628 | * @returns the health left (this->healthMax - health+this->health) |
---|
629 | */ |
---|
630 | float WorldEntity::increaseHealth(float health) |
---|
631 | { |
---|
632 | this->health += health; |
---|
633 | if (this->health > this->healthMax) |
---|
634 | { |
---|
635 | float retHealth = this->healthMax - this->health; |
---|
636 | this->health = this->healthMax; |
---|
637 | this->updateHealthWidget(); |
---|
638 | return retHealth; |
---|
639 | } |
---|
640 | this->updateHealthWidget(); |
---|
641 | return 0.0; |
---|
642 | } |
---|
643 | |
---|
644 | /** |
---|
645 | * @param health the Health to be removed |
---|
646 | * @returns 0.0 or the rest, that was not substracted (bellow 0.0) |
---|
647 | */ |
---|
648 | float WorldEntity::decreaseHealth(float health) |
---|
649 | { |
---|
650 | this->health -= health; |
---|
651 | |
---|
652 | if (this->health < 0) |
---|
653 | { |
---|
654 | float retHealth = -this->health; |
---|
655 | this->health = 0.0f; |
---|
656 | this->updateHealthWidget(); |
---|
657 | return retHealth; |
---|
658 | } |
---|
659 | this->updateHealthWidget(); |
---|
660 | return 0.0; |
---|
661 | |
---|
662 | } |
---|
663 | |
---|
664 | /** |
---|
665 | * @param maxHealth the maximal health that can be loaded onto the entity. |
---|
666 | */ |
---|
667 | void WorldEntity::setHealthMax(float healthMax) |
---|
668 | { |
---|
669 | this->healthMax = healthMax; |
---|
670 | if (this->health > this->healthMax) |
---|
671 | { |
---|
672 | PRINTF(3)("new maxHealth is bigger as the old health. Did you really intend to do this for (%s::%s)\n", this->getClassName(), this->getName()); |
---|
673 | this->health = this->healthMax; |
---|
674 | } |
---|
675 | this->updateHealthWidget(); |
---|
676 | } |
---|
677 | |
---|
678 | /** |
---|
679 | * @brief creates the HealthWidget |
---|
680 | * |
---|
681 | * since not all entities need an HealthWidget, it is only created on request. |
---|
682 | */ |
---|
683 | void WorldEntity::createHealthWidget() |
---|
684 | { |
---|
685 | if (this->healthWidget == NULL) |
---|
686 | { |
---|
687 | this->healthWidget = new OrxGui::GLGuiBar(); |
---|
688 | this->healthWidget->setSize2D(30,400); |
---|
689 | this->healthWidget->setAbsCoor2D(10,100); |
---|
690 | |
---|
691 | this->updateHealthWidget(); |
---|
692 | } |
---|
693 | else |
---|
694 | PRINTF(3)("Allready created the HealthWidget for %s::%s\n", this->getClassName(), this->getName()); |
---|
695 | } |
---|
696 | |
---|
697 | void WorldEntity::increaseHealthMax(float increaseHealth) |
---|
698 | { |
---|
699 | this->healthMax += increaseHealth; |
---|
700 | this->updateHealthWidget(); |
---|
701 | } |
---|
702 | |
---|
703 | |
---|
704 | OrxGui::GLGuiWidget* WorldEntity::getHealthWidget() |
---|
705 | { |
---|
706 | this->createHealthWidget(); |
---|
707 | return this->healthWidget; |
---|
708 | } |
---|
709 | |
---|
710 | /** |
---|
711 | * @param visibility shows or hides the health-bar |
---|
712 | * (creates the widget if needed) |
---|
713 | */ |
---|
714 | void WorldEntity::setHealthWidgetVisibilit(bool visibility) |
---|
715 | { |
---|
716 | if (visibility) |
---|
717 | { |
---|
718 | if (this->healthWidget != NULL) |
---|
719 | this->healthWidget->show(); |
---|
720 | else |
---|
721 | { |
---|
722 | this->createHealthWidget(); |
---|
723 | this->updateHealthWidget(); |
---|
724 | this->healthWidget->show(); |
---|
725 | } |
---|
726 | } |
---|
727 | else if (this->healthWidget != NULL) |
---|
728 | this->healthWidget->hide(); |
---|
729 | } |
---|
730 | |
---|
731 | |
---|
732 | /** |
---|
733 | * hit the world entity with |
---|
734 | * @param damage damage to be dealt |
---|
735 | */ |
---|
736 | void WorldEntity::hit(float damage) |
---|
737 | { |
---|
738 | this->decreaseHealth(damage); |
---|
739 | |
---|
740 | PRINTF(5)("Hit me: %s now only %f/%f health\n", this->getClassName(), this->getHealth(), this->getHealthMax()); |
---|
741 | |
---|
742 | if( this->getHealth() > 0) |
---|
743 | { |
---|
744 | // any small explosion animaitions |
---|
745 | } |
---|
746 | else |
---|
747 | { |
---|
748 | this->destroy(); |
---|
749 | |
---|
750 | if( State::getGameRules() != NULL) |
---|
751 | State::getGameRules()->registerKill(Kill(NULL, this)); |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | |
---|
756 | /** |
---|
757 | * destoys the world entity |
---|
758 | */ |
---|
759 | void WorldEntity::destroy() |
---|
760 | { |
---|
761 | this->toList(OM_DEAD); |
---|
762 | } |
---|
763 | |
---|
764 | |
---|
765 | /** |
---|
766 | * @brief updates the HealthWidget |
---|
767 | */ |
---|
768 | void WorldEntity::updateHealthWidget() |
---|
769 | { |
---|
770 | if (this->healthWidget != NULL) |
---|
771 | { |
---|
772 | this->healthWidget->setMaximum(this->healthMax); |
---|
773 | this->healthWidget->setValue(this->health); |
---|
774 | } |
---|
775 | } |
---|
776 | |
---|
777 | |
---|
778 | /** |
---|
779 | * DEBUG-DRAW OF THE BV-Tree. |
---|
780 | * @param depth What depth to draw |
---|
781 | * @param drawMode the mode to draw this entity under |
---|
782 | */ |
---|
783 | void WorldEntity::drawBVTree(int depth, int drawMode) const |
---|
784 | { |
---|
785 | glMatrixMode(GL_MODELVIEW); |
---|
786 | glPushMatrix(); |
---|
787 | /* translate */ |
---|
788 | glTranslatef (this->getAbsCoor ().x, |
---|
789 | this->getAbsCoor ().y, |
---|
790 | this->getAbsCoor ().z); |
---|
791 | /* rotate */ |
---|
792 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
793 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
794 | |
---|
795 | |
---|
796 | if (this->obbTree) |
---|
797 | this->obbTree->drawBV(depth, drawMode); |
---|
798 | |
---|
799 | |
---|
800 | glPopMatrix(); |
---|
801 | } |
---|
802 | |
---|
803 | |
---|
804 | /** |
---|
805 | * Debug the WorldEntity |
---|
806 | */ |
---|
807 | void WorldEntity::debugEntity() const |
---|
808 | { |
---|
809 | PRINT(0)("WorldEntity %s::%s (DEBUG)\n", this->getClassName(), this->getName()); |
---|
810 | this->debugNode(); |
---|
811 | PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber) , this->models.size()); |
---|
812 | for (unsigned int i = 0; i < this->models.size(); i++) |
---|
813 | { |
---|
814 | if (models[i] != NULL) |
---|
815 | PRINT(0)(" : %d:%s", i, this->models[i]->getName()); |
---|
816 | } |
---|
817 | PRINT(0)("\n"); |
---|
818 | |
---|
819 | } |
---|
820 | |
---|
821 | |
---|
822 | /** |
---|
823 | * handler for changes on registred vars |
---|
824 | * @param id id's which changed |
---|
825 | */ |
---|
826 | void WorldEntity::varChangeHandler( std::list< int > & id ) |
---|
827 | { |
---|
828 | if ( std::find( id.begin(), id.end(), modelFileName_handle ) != id.end() || |
---|
829 | std::find( id.begin(), id.end(), scaling_handle ) != id.end() |
---|
830 | ) |
---|
831 | { |
---|
832 | loadModel( modelFileName, scaling ); |
---|
833 | } |
---|
834 | |
---|
835 | PNode::varChangeHandler( id ); |
---|
836 | } |
---|
837 | |
---|