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