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 | main-programmer: Benjamin Grauer |
---|
16 | co-programmer: Christian Meier |
---|
17 | */ |
---|
18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
19 | |
---|
20 | #include "world_entity.h" |
---|
21 | #include "shell_command.h" |
---|
22 | |
---|
23 | #include "util/loading/resource_manager.h" |
---|
24 | #include "resource_obj.h" |
---|
25 | #include "md2/md2Model.h" |
---|
26 | #include "md3/md3_model.h" |
---|
27 | |
---|
28 | #include "oif/object_information_file.h" |
---|
29 | #include "mount_point.h" |
---|
30 | |
---|
31 | #include "aabb_tree_node.h" |
---|
32 | |
---|
33 | #include "util/loading/load_param.h" |
---|
34 | #include "loading/load_param_xml.h" |
---|
35 | #include "util/loading/factory.h" |
---|
36 | |
---|
37 | #include "obb_tree.h" |
---|
38 | |
---|
39 | #include "elements/glgui_energywidget.h" |
---|
40 | #include "elements/glgui_energywidgetvertical.h" |
---|
41 | |
---|
42 | #include "state.h" |
---|
43 | #include "camera.h" |
---|
44 | |
---|
45 | #include "collision_filter.h" |
---|
46 | #include "collision_event.h" |
---|
47 | #include "game_rules.h" |
---|
48 | #include "kill.h" |
---|
49 | #include "debug.h" |
---|
50 | |
---|
51 | #include "track/track.h" |
---|
52 | |
---|
53 | #include "projectiles/projectile.h" |
---|
54 | |
---|
55 | SHELL_COMMAND(model, WorldEntity, loadModel) |
---|
56 | ->describe("sets the Model of the WorldEntity") |
---|
57 | ->defaultValues("models/ships/fighter.obj", 1.0f); |
---|
58 | |
---|
59 | SHELL_COMMAND(debugEntity, WorldEntity, debugWE); |
---|
60 | |
---|
61 | |
---|
62 | ObjectListDefinition(WorldEntity); |
---|
63 | /** |
---|
64 | * Loads the WordEntity-specific Part of any derived Class |
---|
65 | * |
---|
66 | * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves, |
---|
67 | * that can calls WorldEntities loadParams for itself. |
---|
68 | */ |
---|
69 | WorldEntity::WorldEntity() |
---|
70 | : Synchronizeable(), _collisionFilter(this) |
---|
71 | { |
---|
72 | this->registerObject(this, WorldEntity::_objectList); |
---|
73 | |
---|
74 | this->obbTree = NULL; |
---|
75 | this->aabbNode = NULL; |
---|
76 | this->healthWidget = NULL; |
---|
77 | this->healthMax = 1.0f; |
---|
78 | this->health = 1.0f; |
---|
79 | this->damage = 0.0f; // no damage dealt by a default entity |
---|
80 | this->scaling = 1.0f; |
---|
81 | this->oiFile = NULL; |
---|
82 | // add 10 members to this array |
---|
83 | this->mountPoints.reserve(10); |
---|
84 | |
---|
85 | /* OSOLETE */ |
---|
86 | this->bVisible = true; |
---|
87 | this->bCollide = true; |
---|
88 | |
---|
89 | this->objectListNumber = OM_INIT; |
---|
90 | this->lastObjectListNumber = OM_INIT; |
---|
91 | |
---|
92 | this->_bOnGround = false; |
---|
93 | |
---|
94 | // Track of this entity |
---|
95 | this->entityTrack = NULL; |
---|
96 | |
---|
97 | // registering default reactions: |
---|
98 | this->subscribeReaction(CoRe::CREngine::CR_OBJECT_DAMAGE, Projectile::staticClassID()); |
---|
99 | |
---|
100 | this->toList(OM_NULL); |
---|
101 | |
---|
102 | this->registerVar( new SynchronizeableString( &this->md2TextureFileName, &this->md2TextureFileName, "md2TextureFileName", PERMISSION_MASTER_SERVER ) ); |
---|
103 | this->modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName", PERMISSION_MASTER_SERVER ) ); |
---|
104 | this->scaling_handle = registerVarId( new SynchronizeableFloat( &scaling, &scaling, "scaling", PERMISSION_MASTER_SERVER ) ); |
---|
105 | this->list_handle = registerVarId( new SynchronizeableInt( (int*)&objectListNumber, &list_write, "list", PERMISSION_MASTER_SERVER ) ); |
---|
106 | |
---|
107 | this->health_handle = registerVarId( new SynchronizeableFloat( &this->health, &this->health_write, "health", PERMISSION_MASTER_SERVER ) ); |
---|
108 | this->healthMax_handle = registerVarId( new SynchronizeableFloat( &this->healthMax, &this->healthMax_write, "maxHealth", PERMISSION_MASTER_SERVER ) ); |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * standard destructor |
---|
113 | */ |
---|
114 | WorldEntity::~WorldEntity () |
---|
115 | { |
---|
116 | State::getObjectManager()->toList(this, OM_INIT); |
---|
117 | |
---|
118 | // Delete the model (unregister it with the ResourceManager) |
---|
119 | for (unsigned int i = 0; i < this->models.size(); i++) |
---|
120 | this->setModel(NULL, i); |
---|
121 | |
---|
122 | // remove the object information file |
---|
123 | if( this->oiFile) |
---|
124 | delete this->oiFile; |
---|
125 | // and clear all monut points |
---|
126 | this->mountPoints.clear(); |
---|
127 | |
---|
128 | // Delete the obbTree |
---|
129 | if( this->obbTree != NULL) |
---|
130 | delete this->obbTree; |
---|
131 | |
---|
132 | if (this->healthWidget != NULL) |
---|
133 | delete this->healthWidget; |
---|
134 | |
---|
135 | this->unsubscribeReactions(); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * loads the WorldEntity Specific Parameters. |
---|
140 | * @param root: the XML-Element to load the Data From |
---|
141 | */ |
---|
142 | void WorldEntity::loadParams(const TiXmlElement* root) |
---|
143 | { |
---|
144 | // Do the PNode loading stuff |
---|
145 | PNode::loadParams(root); |
---|
146 | |
---|
147 | LoadParam(root, "md2texture", this, WorldEntity, loadMD2Texture) |
---|
148 | .describe("the fileName of the texture, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
---|
149 | .defaultValues(""); |
---|
150 | |
---|
151 | // Model Loading |
---|
152 | LoadParam(root, "model", this, WorldEntity, loadModel) |
---|
153 | .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
---|
154 | .defaultValues("", 1.0f, 0); |
---|
155 | |
---|
156 | LoadParam(root, "mountpoints", this, WorldEntity, loadMountPoints) |
---|
157 | .describe("the fileName of the object information file (optional)"); |
---|
158 | |
---|
159 | // Entity Attributes |
---|
160 | LoadParam(root, "maxHealth", this, WorldEntity, setHealthMax) |
---|
161 | .describe("The Maximum health that can be loaded onto this entity") |
---|
162 | .defaultValues(1.0f); |
---|
163 | |
---|
164 | LoadParam(root, "health", this, WorldEntity, setHealth) |
---|
165 | .describe("The Health the WorldEntity has at this moment") |
---|
166 | .defaultValues(1.0f); |
---|
167 | |
---|
168 | LoadParam(root, "list", this, WorldEntity, toListS); |
---|
169 | |
---|
170 | |
---|
171 | // Track |
---|
172 | LoadParamXML(root, "Track", this, WorldEntity, addTrack) |
---|
173 | .describe("creates and adds a track to this WorldEntity"); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | /** |
---|
178 | * this functions adds a track to this workd entity. This can be usefull, if you like this WE to follow a some waypoints. |
---|
179 | * here the track is created and further initializing left for the Track itself |
---|
180 | */ |
---|
181 | void WorldEntity::addTrack(const TiXmlElement* root) |
---|
182 | { |
---|
183 | // The problem we have is most likely here. The track should be constructed WITH the XML-Code |
---|
184 | this->entityTrack = new Track(root); |
---|
185 | this->setParent(this->entityTrack->getTrackNode()); |
---|
186 | this->entityTrack->getTrackNode()->setParentMode(PNODE_ALL); |
---|
187 | /*LOAD_PARAM_START_CYCLE(root, element); |
---|
188 | { |
---|
189 | PRINTF(4)("element is: %s\n", element->Value()); |
---|
190 | Factory::fabricate(element); |
---|
191 | } |
---|
192 | LOAD_PARAM_END_CYCLE(element);*/ |
---|
193 | |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | /** |
---|
199 | * loads a Model onto a WorldEntity |
---|
200 | * @param fileName the name of the model to load |
---|
201 | * @param scaling the Scaling of the model |
---|
202 | * |
---|
203 | * FIXME |
---|
204 | * @todo: separate the obb tree generation from the model |
---|
205 | */ |
---|
206 | void WorldEntity::loadModel(const std::string& fileName, float scaling, unsigned int modelNumber, unsigned int obbTreeDepth) |
---|
207 | { |
---|
208 | this->modelLODName = fileName; |
---|
209 | this->scaling = scaling; |
---|
210 | |
---|
211 | std::string name = fileName; |
---|
212 | |
---|
213 | if ( name.find( Resources::ResourceManager::getInstance()->mainGlobalPath().name() ) == 0 ) |
---|
214 | { |
---|
215 | name.erase(Resources::ResourceManager::getInstance()->mainGlobalPath().name().size()); |
---|
216 | } |
---|
217 | |
---|
218 | this->modelFileName = name; |
---|
219 | |
---|
220 | if (!fileName.empty()) |
---|
221 | { |
---|
222 | // search for the special character # in the LoadParam |
---|
223 | if (fileName.find('#') != std::string::npos) |
---|
224 | { |
---|
225 | PRINTF(4)("Found # in %s... searching for LOD's\n", fileName.c_str()); |
---|
226 | std::string lodFile = fileName; |
---|
227 | unsigned int offset = lodFile.find('#'); |
---|
228 | for (unsigned int i = 0; i < 3; i++) |
---|
229 | { |
---|
230 | lodFile[offset] = 48+(int)i; |
---|
231 | if (Resources::ResourceManager::getInstance()->checkFileInMainPath( lodFile)) |
---|
232 | this->loadModel(lodFile, scaling, i); |
---|
233 | } |
---|
234 | return; |
---|
235 | } |
---|
236 | if (this->scaling <= 0.0) |
---|
237 | { |
---|
238 | PRINTF(1)("YOU GAVE ME A CRAPY SCALE resetting to 1.0\n"); |
---|
239 | this->scaling = 1.0; |
---|
240 | } |
---|
241 | /// LOADING AN OBJ FILE |
---|
242 | if(fileName.find(".obj") != std::string::npos) |
---|
243 | { |
---|
244 | PRINTF(4)("fetching OBJ file: %s\n", fileName.c_str()); |
---|
245 | // creating the model and loading it |
---|
246 | StaticModel* model = new StaticModel(); |
---|
247 | *model = ResourceOBJ(fileName, this->scaling); |
---|
248 | |
---|
249 | // check if ther is a valid model and load other stuff |
---|
250 | if (model->getVertexCount() > 0) |
---|
251 | { |
---|
252 | this->setModel(model, modelNumber); |
---|
253 | |
---|
254 | if( modelNumber == 0) |
---|
255 | { |
---|
256 | this->buildObbTree(obbTreeDepth); |
---|
257 | } |
---|
258 | } |
---|
259 | else |
---|
260 | delete model; |
---|
261 | } |
---|
262 | /// LOADING AN MD2-model |
---|
263 | else if(fileName.find(".md2") != std::string::npos) |
---|
264 | { |
---|
265 | PRINTF(4)("fetching MD2 file: %s\n", fileName.c_str()); |
---|
266 | Model* m = new MD2Model(fileName, this->md2TextureFileName, this->scaling); |
---|
267 | //this->setModel((Model*)ResourceManager::getInstance()->load(fileName, MD2, RP_CAMPAIGN), 0); |
---|
268 | this->setModel(m, 0); |
---|
269 | |
---|
270 | if( m != NULL) |
---|
271 | this->buildObbTree(obbTreeDepth); |
---|
272 | } |
---|
273 | /// LOADING AN MD3-MODEL. |
---|
274 | else if(fileName.find(".md3") != std::string::npos) |
---|
275 | { |
---|
276 | PRINTF(4)("fetching MD3 file: %s\n", fileName.c_str()); |
---|
277 | // Model* m = new md3::MD3Model(fileName, this->scaling); |
---|
278 | // this->setModel(m, 0); |
---|
279 | |
---|
280 | // if( m != NULL) |
---|
281 | // this->buildObbTree(obbTreeDepth); |
---|
282 | } |
---|
283 | } |
---|
284 | else |
---|
285 | { |
---|
286 | this->setModel(NULL); |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * sets a specific Model for the Object. |
---|
292 | * @param model The Model to set |
---|
293 | * @param modelNumber the n'th model in the List to get. |
---|
294 | */ |
---|
295 | void WorldEntity::setModel(Model* model, unsigned int modelNumber) |
---|
296 | { |
---|
297 | if (this->models.size() <= modelNumber) |
---|
298 | this->models.resize(modelNumber+1, NULL); |
---|
299 | |
---|
300 | if (this->models[modelNumber] != NULL) |
---|
301 | { |
---|
302 | delete this->models[modelNumber]; |
---|
303 | } |
---|
304 | |
---|
305 | this->models[modelNumber] = model; |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | /** |
---|
311 | * loads the object information file for this model |
---|
312 | * @param fileName the name of the file |
---|
313 | */ |
---|
314 | void WorldEntity::loadMountPoints(const std::string& fileName) |
---|
315 | { |
---|
316 | PRINTF(5)("loading the oif File: %s\n", fileName.c_str()); |
---|
317 | |
---|
318 | // now load the object information file |
---|
319 | this->oiFile = new ObjectInformationFile(fileName); |
---|
320 | |
---|
321 | // get the model to load |
---|
322 | Model* model = this->getModel(); |
---|
323 | |
---|
324 | // extract the mount points |
---|
325 | model->extractMountPoints(); |
---|
326 | |
---|
327 | // first get all mount points from the model |
---|
328 | const std::list<mountPointSkeleton> mpList = model->getMountPoints(); |
---|
329 | // for each skeleton create a mounting point world entity |
---|
330 | std::list<mountPointSkeleton>::const_iterator it = mpList.begin(); |
---|
331 | |
---|
332 | for( ; it != mpList.end(); it++) |
---|
333 | { |
---|
334 | // create the mount points world entity |
---|
335 | MountPoint* mp = new MountPoint( (*it).up, (*it).forward, (*it).center, (*it).name); |
---|
336 | // parent it to this WE |
---|
337 | mp->setParent( this); |
---|
338 | // now add to the right group |
---|
339 | mp->toList( (OM_LIST)(this->getOMListNumber()+1)); |
---|
340 | // now get the number and add the mount point to the slot |
---|
341 | std::string nrStr = (*it).name.substr(3, 2); |
---|
342 | // add the mount point |
---|
343 | this->addMountPoint(atoi(nrStr.c_str()), mp); |
---|
344 | |
---|
345 | // now fill the mount point |
---|
346 | mp->initMountPoint( this->oiFile->getMountPointDescription()); |
---|
347 | } |
---|
348 | |
---|
349 | } |
---|
350 | |
---|
351 | |
---|
352 | /** |
---|
353 | * builds the obb-tree |
---|
354 | * @param depth the depth to calculate |
---|
355 | */ |
---|
356 | bool WorldEntity::buildObbTree(int depth) |
---|
357 | { |
---|
358 | if( this->obbTree != NULL) |
---|
359 | { |
---|
360 | delete this->obbTree; |
---|
361 | this->obbTree = NULL; |
---|
362 | } |
---|
363 | |
---|
364 | if (this->models[0] != NULL) |
---|
365 | this->obbTree = new OBBTree(depth, models[0]->getModelInfo(), this); |
---|
366 | else |
---|
367 | { |
---|
368 | PRINTF(1)("could not create obb-tree, because no model was loaded yet\n"); |
---|
369 | this->obbTree = NULL; |
---|
370 | return false; |
---|
371 | } |
---|
372 | |
---|
373 | |
---|
374 | // create the axis aligned bounding box |
---|
375 | if( this->aabbNode != NULL) |
---|
376 | { |
---|
377 | delete this->aabbNode; |
---|
378 | this->aabbNode = NULL; |
---|
379 | } |
---|
380 | |
---|
381 | if( this->models[0] != NULL) |
---|
382 | { |
---|
383 | this->aabbNode = new AABBTreeNode(); |
---|
384 | this->aabbNode->spawnBVTree(this->models[0]); |
---|
385 | } |
---|
386 | else |
---|
387 | { |
---|
388 | PRINTF(1)("could not create aabb bounding box, because no model was loaded yet\n"); |
---|
389 | this->aabbNode = NULL; |
---|
390 | return false; |
---|
391 | } |
---|
392 | return true; |
---|
393 | } |
---|
394 | |
---|
395 | |
---|
396 | /** |
---|
397 | * adds a mount point to the end of the list |
---|
398 | * @param mountPoint point to be added |
---|
399 | */ |
---|
400 | void WorldEntity::addMountPoint(MountPoint* mountPoint) |
---|
401 | { |
---|
402 | // add the mount point at the last position |
---|
403 | this->mountPoints.push_back(mountPoint); |
---|
404 | } |
---|
405 | |
---|
406 | /** |
---|
407 | * adds a mount point to a world entity |
---|
408 | * @param mountPoint point to be added |
---|
409 | */ |
---|
410 | void WorldEntity::addMountPoint(int slot, MountPoint* mountPoint) |
---|
411 | { |
---|
412 | if( this->mountPoints.capacity() < (unsigned int)slot) |
---|
413 | { |
---|
414 | // reserve 5 more slots than needed so this function is called as rare as possible |
---|
415 | this->mountPoints.reserve(slot + 5); |
---|
416 | } |
---|
417 | else if( this->mountPoints[slot] != NULL) |
---|
418 | { |
---|
419 | PRINTF(4)("adding a mount point to a slot, that already is occupied! ignoring - maybe some object did not get connected well (object: %s)\n", this->getClassCName()); |
---|
420 | } |
---|
421 | |
---|
422 | // just connect the mount point |
---|
423 | this->mountPoints[slot] = mountPoint; |
---|
424 | } |
---|
425 | |
---|
426 | |
---|
427 | /** |
---|
428 | * mounts a world entity on a specified mount point (~socket) |
---|
429 | * @param entity entity to be connected |
---|
430 | */ |
---|
431 | void WorldEntity::mount(int slot, WorldEntity* entity) |
---|
432 | { |
---|
433 | if( this->mountPoints[slot] == NULL) |
---|
434 | { |
---|
435 | PRINTF(0)("you tried to add an entity to a mount point that doesn't exist (slot %i)\n", slot); |
---|
436 | return; |
---|
437 | } |
---|
438 | |
---|
439 | // mount the entity |
---|
440 | this->mountPoints[slot]->mount(entity); |
---|
441 | } |
---|
442 | |
---|
443 | |
---|
444 | /** |
---|
445 | * removes a mount point from a specified mount point |
---|
446 | * @param mountPoint entity to be unconnected |
---|
447 | */ |
---|
448 | void WorldEntity::unmount(int slot) |
---|
449 | { |
---|
450 | if( this->mountPoints[slot] == NULL) |
---|
451 | { |
---|
452 | PRINTF(0)("you tried to remove an entity from a mount point that doesn't exist (slot %i)\n", slot); |
---|
453 | return; |
---|
454 | } |
---|
455 | |
---|
456 | // unmount the entity |
---|
457 | this->mountPoints[slot]->unmount(); |
---|
458 | } |
---|
459 | |
---|
460 | |
---|
461 | /** |
---|
462 | * subscribes this world entity to a collision reaction |
---|
463 | * @param type the type of reaction to subscribe to |
---|
464 | * @param target1 a filter target (classID) |
---|
465 | */ |
---|
466 | void WorldEntity::subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1) |
---|
467 | { |
---|
468 | this->_collisionFilter.subscribeReaction(type, target1); |
---|
469 | } |
---|
470 | |
---|
471 | |
---|
472 | /** |
---|
473 | * subscribes this world entity to a collision reaction |
---|
474 | * @param type the type of reaction to subscribe to |
---|
475 | * @param target1 a filter target (classID) |
---|
476 | */ |
---|
477 | void WorldEntity::subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1, const ClassID& target2) |
---|
478 | { |
---|
479 | this->_collisionFilter.subscribeReaction(type, target1, target2); |
---|
480 | } |
---|
481 | |
---|
482 | |
---|
483 | /** |
---|
484 | * subscribes this world entity to a collision reaction |
---|
485 | * @param type the type of reaction to subscribe to |
---|
486 | * @param target1 a filter target (classID) |
---|
487 | */ |
---|
488 | void WorldEntity::subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1, const ClassID& target2, const ClassID& target3) |
---|
489 | { |
---|
490 | this->_collisionFilter.subscribeReaction(type, target1, target2, target3); |
---|
491 | } |
---|
492 | |
---|
493 | |
---|
494 | /** |
---|
495 | * unsubscribes a specific reaction from the worldentity |
---|
496 | * @param type the reaction to unsubscribe |
---|
497 | */ |
---|
498 | void WorldEntity::unsubscribeReaction(CoRe::CREngine::ReactionType type) |
---|
499 | { |
---|
500 | this->_collisionFilter.unsubscribeReaction(type); |
---|
501 | } |
---|
502 | |
---|
503 | |
---|
504 | /** |
---|
505 | * unsubscribes all collision reactions |
---|
506 | */ |
---|
507 | void WorldEntity::unsubscribeReactions() |
---|
508 | { |
---|
509 | this->_collisionFilter.unsubscribeReactions(); |
---|
510 | } |
---|
511 | |
---|
512 | |
---|
513 | /** |
---|
514 | * @brief moves this entity to the List OM_List |
---|
515 | * @param list the list to set this Entity to. |
---|
516 | * |
---|
517 | * this is the same as a call to State::getObjectManager()->toList(entity , list); |
---|
518 | * directly, but with an easier interface. |
---|
519 | * |
---|
520 | * @todo inline this (peut etre) |
---|
521 | */ |
---|
522 | void WorldEntity::toList(OM_LIST list) |
---|
523 | { |
---|
524 | State::getObjectManager()->toList(this, list); |
---|
525 | } |
---|
526 | |
---|
527 | void WorldEntity::toListS(const std::string& listName) |
---|
528 | { |
---|
529 | OM_LIST id = ObjectManager::StringToOMList(listName); |
---|
530 | if (id != OM_NULL) |
---|
531 | this->toList(id); |
---|
532 | else |
---|
533 | PRINTF(2)("List %s not found\n", listName.c_str()); |
---|
534 | } |
---|
535 | |
---|
536 | |
---|
537 | void WorldEntity::toReflectionList() |
---|
538 | { |
---|
539 | State::getObjectManager()->toReflectionList( this ); |
---|
540 | } |
---|
541 | |
---|
542 | void removeFromReflectionList() |
---|
543 | { |
---|
544 | /// TODO |
---|
545 | /// State::getObject |
---|
546 | } |
---|
547 | |
---|
548 | /** |
---|
549 | * sets the character attributes of a worldentity |
---|
550 | * @param character attributes |
---|
551 | * |
---|
552 | * these attributes don't have to be set, only use them, if you need them |
---|
553 | */ |
---|
554 | //void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr) |
---|
555 | //{} |
---|
556 | |
---|
557 | |
---|
558 | /** |
---|
559 | * this function is called, when two entities collide |
---|
560 | * @param entity: the world entity with whom it collides |
---|
561 | * |
---|
562 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
563 | */ |
---|
564 | void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location) |
---|
565 | { |
---|
566 | /** |
---|
567 | * THIS IS A DEFAULT COLLISION-Effect. |
---|
568 | * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT |
---|
569 | * USE:: |
---|
570 | * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; }; |
---|
571 | * |
---|
572 | * You can always define a default Action.... don't be affraid just test it :) |
---|
573 | */ |
---|
574 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassCName(), entity->getClassCName(), location.x, location.y, location.z); |
---|
575 | } |
---|
576 | |
---|
577 | |
---|
578 | /** |
---|
579 | * this function is called, when two entities collide |
---|
580 | * @param entity: the world entity with whom it collides |
---|
581 | * |
---|
582 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
583 | */ |
---|
584 | void WorldEntity::collidesWithGround(const Vector& location) |
---|
585 | { |
---|
586 | PRINTF(0)("BSP_GROUND: %s collides \n", this->getClassCName() ); |
---|
587 | } |
---|
588 | |
---|
589 | void WorldEntity::collidesWithGround(const Vector& feet, const Vector& ray_1, const Vector& ray_2) |
---|
590 | { |
---|
591 | |
---|
592 | // PRINTF(0)("BSP_GROUND: Player collides \n", this->getClassCName() ); |
---|
593 | |
---|
594 | Vector v = this->getAbsDirX(); |
---|
595 | v.x *= 10.1; |
---|
596 | v.y *= 10.1; |
---|
597 | v.z *= 10.1; |
---|
598 | Vector u = Vector(0.0,-20.0,0.0); |
---|
599 | |
---|
600 | |
---|
601 | if(!(this->getAbsCoor().x == ray_2.x && this->getAbsCoor().y == ray_2.y && this->getAbsCoor().z == ray_2.z) ) |
---|
602 | { |
---|
603 | |
---|
604 | this->setAbsCoor(ray_2 - v); |
---|
605 | |
---|
606 | } |
---|
607 | else |
---|
608 | { |
---|
609 | 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) |
---|
610 | { |
---|
611 | this->setAbsCoor(feet -u ); |
---|
612 | } |
---|
613 | |
---|
614 | this->setAbsCoor(ray_2 - v); |
---|
615 | |
---|
616 | } |
---|
617 | |
---|
618 | |
---|
619 | } |
---|
620 | |
---|
621 | /** |
---|
622 | * this is called immediately after the Entity has been constructed, initialized and then Spawned into the World |
---|
623 | * |
---|
624 | */ |
---|
625 | void WorldEntity::postSpawn () |
---|
626 | {} |
---|
627 | |
---|
628 | |
---|
629 | /** |
---|
630 | * this method is called by the world if the WorldEntity leaves the game |
---|
631 | */ |
---|
632 | void WorldEntity::leaveWorld () |
---|
633 | {} |
---|
634 | |
---|
635 | |
---|
636 | /** |
---|
637 | * resets the WorldEntity to its initial values. eg. used for multiplayer games: respawning |
---|
638 | */ |
---|
639 | void WorldEntity::reset() |
---|
640 | { |
---|
641 | this->setHealth( this->getHealthMax() ); |
---|
642 | } |
---|
643 | |
---|
644 | /** |
---|
645 | * this method is called every frame |
---|
646 | * @param time: the time in seconds that has passed since the last tick |
---|
647 | * |
---|
648 | * Handle all stuff that should update with time inside this method (movement, animation, etc.) |
---|
649 | */ |
---|
650 | void WorldEntity::tick(float time) |
---|
651 | {} |
---|
652 | |
---|
653 | |
---|
654 | /** |
---|
655 | * the entity is drawn onto the screen with this function |
---|
656 | * |
---|
657 | * This is a central function of an entity: call it to let the entity painted to the screen. |
---|
658 | * Just override this function with whatever you want to be drawn. |
---|
659 | */ |
---|
660 | void WorldEntity::draw() const |
---|
661 | { |
---|
662 | //PRINTF(0)("(%s::%s)\n", this->getClassCName(), this->getName()); |
---|
663 | // assert(!unlikely(this->models.empty())); |
---|
664 | { |
---|
665 | glMatrixMode(GL_MODELVIEW); |
---|
666 | glPushMatrix(); |
---|
667 | |
---|
668 | /* translate */ |
---|
669 | glTranslatef (this->getAbsCoor ().x, |
---|
670 | this->getAbsCoor ().y, |
---|
671 | this->getAbsCoor ().z); |
---|
672 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
673 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
674 | |
---|
675 | |
---|
676 | // This Draws the LOD's |
---|
677 | float cameraDistance = State::getCamera()->distance(this); |
---|
678 | if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL) |
---|
679 | { |
---|
680 | this->models[2]->draw(); |
---|
681 | } |
---|
682 | else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL) |
---|
683 | { |
---|
684 | this->models[1]->draw(); |
---|
685 | } |
---|
686 | else if (this->models.size() >= 1 && this->models[0] != NULL) |
---|
687 | { |
---|
688 | this->models[0]->draw(); |
---|
689 | } |
---|
690 | |
---|
691 | //if (this->entityTrack) |
---|
692 | //this->entityTrack->drawGraph(0.02); |
---|
693 | |
---|
694 | // if( this->aabbNode != NULL) |
---|
695 | // this->aabbNode->drawBV(0, DRAW_BV_POLYGON, Vector(1, 0.6, 0.2), true); |
---|
696 | |
---|
697 | glPopMatrix(); |
---|
698 | } |
---|
699 | } |
---|
700 | |
---|
701 | |
---|
702 | /** |
---|
703 | * the entity is drawn onto the screen with this function |
---|
704 | * |
---|
705 | * This is a central function of an entity: call it to let the entity painted to the screen. |
---|
706 | * Just override this function with whatever you want to be drawn. |
---|
707 | */ |
---|
708 | void WorldEntity::draw(const Model* model) const |
---|
709 | { |
---|
710 | glMatrixMode(GL_MODELVIEW); |
---|
711 | glPushMatrix(); |
---|
712 | |
---|
713 | /* translate */ |
---|
714 | glTranslatef (this->getAbsCoor ().x, |
---|
715 | this->getAbsCoor ().y, |
---|
716 | this->getAbsCoor ().z); |
---|
717 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
718 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
719 | |
---|
720 | |
---|
721 | // This Draws the LOD's |
---|
722 | if( model != NULL) |
---|
723 | model->draw(); |
---|
724 | |
---|
725 | glPopMatrix(); |
---|
726 | } |
---|
727 | |
---|
728 | |
---|
729 | /** |
---|
730 | * @param health the Health to add. |
---|
731 | * @returns the health left (this->healthMax - health+this->health) |
---|
732 | */ |
---|
733 | float WorldEntity::increaseHealth(float health) |
---|
734 | { |
---|
735 | this->health += health; |
---|
736 | if (this->health > this->healthMax) |
---|
737 | { |
---|
738 | float retHealth = this->healthMax - this->health; |
---|
739 | this->health = this->healthMax; |
---|
740 | this->updateHealthWidget(); |
---|
741 | return retHealth; |
---|
742 | } |
---|
743 | this->updateHealthWidget(); |
---|
744 | return 0.0; |
---|
745 | } |
---|
746 | |
---|
747 | /** |
---|
748 | * @param health the Health to be removed |
---|
749 | * @returns 0.0 or the rest, that was not substracted (bellow 0.0) |
---|
750 | */ |
---|
751 | float WorldEntity::decreaseHealth(float health) |
---|
752 | { |
---|
753 | this->health -= health; |
---|
754 | |
---|
755 | if (this->health < 0) |
---|
756 | { |
---|
757 | float retHealth = -this->health; |
---|
758 | this->health = 0.0f; |
---|
759 | this->updateHealthWidget(); |
---|
760 | return retHealth; |
---|
761 | } |
---|
762 | this->updateHealthWidget(); |
---|
763 | return 0.0; |
---|
764 | |
---|
765 | } |
---|
766 | |
---|
767 | /** |
---|
768 | * @param maxHealth the maximal health that can be loaded onto the entity. |
---|
769 | */ |
---|
770 | void WorldEntity::setHealthMax(float healthMax) |
---|
771 | { |
---|
772 | this->healthMax = healthMax; |
---|
773 | if (this->health > this->healthMax) |
---|
774 | { |
---|
775 | 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()); |
---|
776 | this->health = this->healthMax; |
---|
777 | } |
---|
778 | this->updateHealthWidget(); |
---|
779 | } |
---|
780 | |
---|
781 | /** |
---|
782 | * @brief creates the HealthWidget |
---|
783 | * |
---|
784 | * since not all entities need an HealthWidget, it is only created on request. |
---|
785 | */ |
---|
786 | void WorldEntity::createHealthWidget() |
---|
787 | { |
---|
788 | if (this->healthWidget == NULL) |
---|
789 | { |
---|
790 | this->healthWidget = new OrxGui::GLGuiEnergyWidgetVertical(); |
---|
791 | //this->healthWidget->setDisplayedName("Health"); |
---|
792 | //this->healthWidget->setSize2D(100,20); |
---|
793 | //this->healthWidget->setAbsCoor2D(100,200); |
---|
794 | |
---|
795 | this->updateHealthWidget(); |
---|
796 | } |
---|
797 | else |
---|
798 | PRINTF(3)("Allready created the HealthWidget for %s::%s\n", this->getClassCName(), this->getCName()); |
---|
799 | } |
---|
800 | |
---|
801 | void WorldEntity::increaseHealthMax(float increaseHealth) |
---|
802 | { |
---|
803 | this->healthMax += increaseHealth; |
---|
804 | this->updateHealthWidget(); |
---|
805 | } |
---|
806 | |
---|
807 | |
---|
808 | OrxGui::GLGuiWidget* WorldEntity::getHealthWidget() |
---|
809 | { |
---|
810 | this->createHealthWidget(); |
---|
811 | return this->healthWidget; |
---|
812 | } |
---|
813 | |
---|
814 | /** |
---|
815 | * @param visibility shows or hides the health-bar |
---|
816 | * (creates the widget if needed) |
---|
817 | */ |
---|
818 | void WorldEntity::setHealthWidgetVisibilit(bool visibility) |
---|
819 | { |
---|
820 | if (visibility) |
---|
821 | { |
---|
822 | if (this->healthWidget != NULL) |
---|
823 | this->healthWidget->show(); |
---|
824 | else |
---|
825 | { |
---|
826 | this->createHealthWidget(); |
---|
827 | this->updateHealthWidget(); |
---|
828 | this->healthWidget->show(); |
---|
829 | } |
---|
830 | } |
---|
831 | else if (this->healthWidget != NULL) |
---|
832 | this->healthWidget->hide(); |
---|
833 | } |
---|
834 | |
---|
835 | |
---|
836 | /** |
---|
837 | * hit the world entity with |
---|
838 | * @param damage damage to be dealt |
---|
839 | */ |
---|
840 | void WorldEntity::hit(float damage, WorldEntity* killer) |
---|
841 | { |
---|
842 | |
---|
843 | this->decreaseHealth(damage); |
---|
844 | |
---|
845 | PRINTF(5)("Hit me: %s::%s now only %f/%f health\n", this->getClassCName(), this->getCName(), this->getHealth(), this->getHealthMax()); |
---|
846 | |
---|
847 | if( this->getHealth() > 0) |
---|
848 | { |
---|
849 | // any small explosion animaitions |
---|
850 | } |
---|
851 | else |
---|
852 | { |
---|
853 | this->destroy( killer ); |
---|
854 | } |
---|
855 | } |
---|
856 | |
---|
857 | |
---|
858 | /** |
---|
859 | * destoys the world entity |
---|
860 | */ |
---|
861 | void WorldEntity::destroy(WorldEntity* killer) |
---|
862 | { |
---|
863 | this->toList(OM_DEAD); |
---|
864 | } |
---|
865 | |
---|
866 | |
---|
867 | /** |
---|
868 | * @brief updates the HealthWidget |
---|
869 | */ |
---|
870 | void WorldEntity::updateHealthWidget() |
---|
871 | { |
---|
872 | if (this->healthWidget != NULL) |
---|
873 | { |
---|
874 | this->healthWidget->setMaximum(this->healthMax); |
---|
875 | this->healthWidget->setValue(this->health); |
---|
876 | } |
---|
877 | } |
---|
878 | |
---|
879 | |
---|
880 | /** |
---|
881 | * DEBUG-DRAW OF THE BV-Tree. |
---|
882 | * @param depth What depth to draw |
---|
883 | * @param drawMode the mode to draw this entity under |
---|
884 | */ |
---|
885 | void WorldEntity::drawBVTree(int depth, int drawMode) const |
---|
886 | { |
---|
887 | glMatrixMode(GL_MODELVIEW); |
---|
888 | glPushMatrix(); |
---|
889 | /* translate */ |
---|
890 | glTranslatef (this->getAbsCoor ().x, |
---|
891 | this->getAbsCoor ().y, |
---|
892 | this->getAbsCoor ().z); |
---|
893 | /* rotate */ |
---|
894 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
895 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
896 | |
---|
897 | |
---|
898 | if (this->obbTree) |
---|
899 | this->obbTree->drawBV(depth, drawMode); |
---|
900 | |
---|
901 | |
---|
902 | glPopMatrix(); |
---|
903 | } |
---|
904 | |
---|
905 | |
---|
906 | |
---|
907 | /** |
---|
908 | * draw the mounting points |
---|
909 | */ |
---|
910 | void WorldEntity::debugDrawMountPoints() const |
---|
911 | { |
---|
912 | |
---|
913 | std::vector<MountPoint*>::const_iterator it = this->mountPoints.begin(); |
---|
914 | for( ; it < this->mountPoints.end(); it++) |
---|
915 | { |
---|
916 | if( (*it) != NULL) |
---|
917 | { |
---|
918 | (*it)->debugDraw(); |
---|
919 | } |
---|
920 | } |
---|
921 | } |
---|
922 | |
---|
923 | |
---|
924 | /** |
---|
925 | * Debug the WorldEntity |
---|
926 | */ |
---|
927 | void WorldEntity::debugEntity() const |
---|
928 | { |
---|
929 | PRINT(0)("WorldEntity %s::%s (DEBUG)\n", this->getClassCName(), this->getCName()); |
---|
930 | this->debugNode(); |
---|
931 | PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber).c_str(), this->models.size()); |
---|
932 | for (unsigned int i = 0; i < this->models.size(); i++) |
---|
933 | { |
---|
934 | if (models[i] != NULL) |
---|
935 | PRINT(0)(" : %d:%s", i, this->models[i]->getCName()); |
---|
936 | } |
---|
937 | PRINT(0)("\n"); |
---|
938 | |
---|
939 | } |
---|
940 | |
---|
941 | |
---|
942 | /** |
---|
943 | * handler for changes on registred vars |
---|
944 | * @param id id's which changed |
---|
945 | */ |
---|
946 | void WorldEntity::varChangeHandler( std::list< int > & id ) |
---|
947 | { |
---|
948 | if ( std::find( id.begin(), id.end(), modelFileName_handle ) != id.end() || |
---|
949 | std::find( id.begin(), id.end(), scaling_handle ) != id.end() |
---|
950 | ) |
---|
951 | { |
---|
952 | loadModel( modelFileName, scaling ); |
---|
953 | } |
---|
954 | |
---|
955 | if ( std::find( id.begin(), id.end(), list_handle ) != id.end() ) |
---|
956 | { |
---|
957 | this->toList( (OM_LIST)list_write ); |
---|
958 | } |
---|
959 | |
---|
960 | if ( std::find( id.begin(), id.end(), health_handle ) != id.end() ) |
---|
961 | { |
---|
962 | this->setHealth( health_write ); |
---|
963 | } |
---|
964 | |
---|
965 | if ( std::find( id.begin(), id.end(), healthMax_handle ) != id.end() ) |
---|
966 | { |
---|
967 | this->setHealthMax( healthMax_write ); |
---|
968 | } |
---|
969 | |
---|
970 | PNode::varChangeHandler( id ); |
---|
971 | } |
---|
972 | |
---|