1 | /*! |
---|
2 | * @file world_entity.h |
---|
3 | * Definition of the basic WorldEntity |
---|
4 | */ |
---|
5 | |
---|
6 | #ifndef _WORLD_ENTITY_H |
---|
7 | #define _WORLD_ENTITY_H |
---|
8 | |
---|
9 | #include "p_node.h" |
---|
10 | #include "synchronizeable.h" |
---|
11 | #include "model.h" |
---|
12 | |
---|
13 | #include "cr_engine.h" |
---|
14 | #include "collision_filter.h" |
---|
15 | #include "object_manager.h" |
---|
16 | #include "glincl.h" |
---|
17 | |
---|
18 | #include "aabb_tree_node.h" |
---|
19 | |
---|
20 | #include "physics_interface.h" |
---|
21 | |
---|
22 | #include <vector> |
---|
23 | |
---|
24 | |
---|
25 | // FORWARD DECLARATION |
---|
26 | namespace OrxSound { class SoundBuffer; class SoundSource; } |
---|
27 | namespace OrxGui { class GLGuiWidget; class GLGuiBar; class GLGuiEnergyWidget; class GLGuiEnergyWidgetVertical; }; |
---|
28 | namespace CoRe { class Collision; } |
---|
29 | |
---|
30 | class BVTree; |
---|
31 | class BoundingVolume; |
---|
32 | class AABBTreeNode; |
---|
33 | class Model; |
---|
34 | class Track; |
---|
35 | class TiXmlElement; |
---|
36 | |
---|
37 | class ObjectInformationFile; |
---|
38 | class MountPoint; |
---|
39 | |
---|
40 | |
---|
41 | //! Basis-class all interactive stuff in the world is derived from |
---|
42 | class WorldEntity : public PNode |
---|
43 | { |
---|
44 | ObjectListDeclaration(WorldEntity); |
---|
45 | |
---|
46 | public: |
---|
47 | WorldEntity(); |
---|
48 | virtual ~WorldEntity (); |
---|
49 | |
---|
50 | virtual void loadParams(const TiXmlElement* root); |
---|
51 | |
---|
52 | void loadModel(const std::string& fileName, float scaling = 1.0f, unsigned int modelNumber = 0, unsigned int obbTreeDepth = 4); |
---|
53 | void loadModel2(const std::string& fileN, float scal = 1.0f){this->loadModel(fileN,scal,0,4);} |
---|
54 | void setModel(Model* model, unsigned int modelNumber = 0); |
---|
55 | Model* getModel(unsigned int modelNumber = 0) const { return (this->models.size() > modelNumber)? this->models[modelNumber] : NULL; }; |
---|
56 | |
---|
57 | void loadMountPoints(const std::string& fileName); |
---|
58 | inline void loadMD2Texture(const std::string& fileName) { this->md2TextureFileName = fileName; } |
---|
59 | |
---|
60 | void addMountPoint(MountPoint* mountPoint); |
---|
61 | void addMountPoint(int slot, MountPoint* mountPoint); |
---|
62 | void mount(int slot, WorldEntity* entity); |
---|
63 | void unmount(int slot); |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | /** @param visibility if the Entity should be visible (been draw) */ |
---|
68 | void setVisibility (bool visibility) { this->bVisible = visibility; }; |
---|
69 | /** @returns true if the entity is visible, false otherwise */ |
---|
70 | inline bool isVisible() const { return this->bVisible; }; |
---|
71 | |
---|
72 | virtual void reset(); |
---|
73 | |
---|
74 | virtual void postSpawn (); |
---|
75 | virtual void leaveWorld (); |
---|
76 | |
---|
77 | virtual void tick (float time); |
---|
78 | virtual void draw () const; |
---|
79 | void draw(const Model* model) const; |
---|
80 | void debugDrawMountPoints() const; |
---|
81 | |
---|
82 | /* --- Collision Detection Block --- */ |
---|
83 | bool buildObbTree(int depth); |
---|
84 | virtual void collidesWith (WorldEntity* entity, const Vector& location); |
---|
85 | virtual void collidesWithGround(const Vector& location); |
---|
86 | virtual void collidesWithGround(const Vector& feet, const Vector& ray_1, const Vector& ray_2); |
---|
87 | |
---|
88 | /** @returns a reference to the obb tree of this worldentity */ |
---|
89 | inline BVTree* getOBBTree() const { return this->obbTree; }; |
---|
90 | inline void setOBBTree(OBBTree* tree) { /*if( this->obbTree != NULL) delete this->obbTree;*/ this->obbTree = (BVTree*)tree; } |
---|
91 | void drawBVTree(int depth, int drawMode) const; |
---|
92 | inline AABB* getModelAABB() const { return (this->aabbNode)?this->aabbNode->getAABB():NULL;} |
---|
93 | |
---|
94 | virtual void hit(float damage, WorldEntity* killer); |
---|
95 | |
---|
96 | |
---|
97 | /* --- Collision Reaction Block --- */ |
---|
98 | void subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1); |
---|
99 | void subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1, const ClassID& target2); |
---|
100 | void subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1, const ClassID& target2, const ClassID& target3); |
---|
101 | |
---|
102 | void unsubscribeReaction(CoRe::CREngine::ReactionType type); |
---|
103 | void unsubscribeReactions(); |
---|
104 | |
---|
105 | /** @return true if there is at least on collision reaction subscribed */ |
---|
106 | inline bool isReactive() const { return this->_collisionFilter.isReactive(); } |
---|
107 | |
---|
108 | /** @param worldEntity the world entity to be checked @returns true if there is a collisionreaction registered for the worldEntity */ |
---|
109 | inline bool isReactive( const WorldEntity& worldEntity) const { return this->_collisionFilter(worldEntity); } |
---|
110 | /** @param worldEntity the world entity to be checked @param type special reaction type @returns true if collision reaction reg. */ |
---|
111 | inline bool isReactive( const WorldEntity& worldEntity, const CoRe::CREngine::ReactionType& type) const |
---|
112 | { return this->_collisionFilter(worldEntity, type); } |
---|
113 | |
---|
114 | |
---|
115 | const CoRe::CollisionFilter& getCollisionFilter(CoRe::CREngine::ReactionType type) const { return this->_collisionFilter; } |
---|
116 | |
---|
117 | /** @returns true if this entity is standing on ground (BSP model) */ |
---|
118 | bool isOnGround() const { return this->_bOnGround; } |
---|
119 | /** @param flag: marks if this entity is standing on ground */ |
---|
120 | void setOnGround(bool flag) { this->_bOnGround = flag; } |
---|
121 | |
---|
122 | virtual void destroy( WorldEntity* killer ); |
---|
123 | |
---|
124 | |
---|
125 | /* @returns the Count of Faces on this WorldEntity */ |
---|
126 | //unsigned int getFaceCount () const { return (this->model != NULL)?this->model->getFaceCount():0; }; |
---|
127 | // void addAbility(Ability* ability); |
---|
128 | // void removeAbility(Ability* ability); |
---|
129 | // void setCharacterAttributes(CharacterAttributes* charAttr); |
---|
130 | // CharacterAttributes* getCharacterAttributes(); |
---|
131 | |
---|
132 | /* --- Object Manager Block --- */ |
---|
133 | void toList(OM_LIST list); |
---|
134 | void toListS(const std::string& listName); |
---|
135 | |
---|
136 | void toReflectionList(); |
---|
137 | void removeFromReflectionList(); |
---|
138 | |
---|
139 | /** @returns a Reference to the objectListNumber to set. */ |
---|
140 | OM_LIST& getOMListNumber() { return this->objectListNumber; } |
---|
141 | /** @returns a Reference to the Iterator */ |
---|
142 | ObjectManager::EntityList::iterator& getEntityIterator() { return this->objectListIterator; } |
---|
143 | |
---|
144 | void hide() { if( this->objectListNumber != OM_DEAD) this->lastObjectListNumber = this->objectListNumber; this->toList(OM_DEAD); } |
---|
145 | void unhide() { if( this->objectListNumber != this->lastObjectListNumber) this->toList(this->lastObjectListNumber); } |
---|
146 | |
---|
147 | |
---|
148 | /* --- Character Attribute Block --- */ |
---|
149 | /** @returns the scaling of the model */ |
---|
150 | float getScaling(){return this->scaling;} |
---|
151 | /** @returns the damage dealt by this world entity */ |
---|
152 | float getDamage() const { return this->damage; } |
---|
153 | /** sets the damage dealt to @param damage damage per second */ |
---|
154 | void setDamage(float damage) { this->damage = damage; } |
---|
155 | /** @returns the Energy of the entity */ |
---|
156 | float getHealth() const { return this->health; }; |
---|
157 | /** @returns the Maximum energy this entity can be charged with */ |
---|
158 | float getHealthMax() const { return this->healthMax; } |
---|
159 | float increaseHealth(float health); |
---|
160 | float decreaseHealth(float health); |
---|
161 | void increaseHealthMax(float increaseHealth); |
---|
162 | OrxGui::GLGuiWidget* getHealthWidget(); |
---|
163 | OrxGui::GLGuiWidget* getShieldWidget(); |
---|
164 | OrxGui::GLGuiWidget* getElectronicWidget(); |
---|
165 | bool hasHealthWidget() const { return this->healthWidget != NULL; }; |
---|
166 | |
---|
167 | float getShield() const { return this->shield; }; |
---|
168 | float getShieldMax() const { return this->shieldMax; }; |
---|
169 | float increaseShield(float shield); |
---|
170 | float decreaseShield(float shield); |
---|
171 | bool getShieldActive() { return this->bShieldActive; }; |
---|
172 | |
---|
173 | float getElectronic() const { return this->electronic; }; |
---|
174 | float getElectronicMax() const { return this->electronicMax; }; |
---|
175 | float increaseElectronic(float electronic); |
---|
176 | float decreaseElectronic(float electronic); |
---|
177 | |
---|
178 | bool systemFailure() { return (this->electronic < float(rand())/float(RAND_MAX) * this->electronicTH); }; |
---|
179 | |
---|
180 | void resetElectronic() { this->electronic = this->electronicMax; }; |
---|
181 | void resetShield() { this->shield = this->shieldMax; this->bShieldActive = true; }; |
---|
182 | void resetHealth() { this->health = this->healthMax; }; |
---|
183 | |
---|
184 | void createHealthWidget(); |
---|
185 | void createShieldWidget(); |
---|
186 | void createElectronicWidget(); |
---|
187 | |
---|
188 | void regen(float time); |
---|
189 | |
---|
190 | void loadShield(float cur, float max, float th, float regen) |
---|
191 | { this->setShield(cur); this->setShieldMax(max); this->setShieldTH(th); this->setShieldRegen(regen); }; |
---|
192 | void loadHealth(float cur, float max, float regen = 0) { this->setHealth(cur); this->setHealthMax(max); this->setHealthRegen(regen); }; |
---|
193 | void loadElectronic(float cur, float max, float th, float regen) |
---|
194 | { this->setElectronic(cur); this->setElectronicMax(max); this->setElectronicTH(th); this->setElectronicRegen(regen); }; |
---|
195 | |
---|
196 | |
---|
197 | virtual void varChangeHandler( std::list<int> & id ); |
---|
198 | |
---|
199 | |
---|
200 | //FIXME: the PhysicsInterface and the whole PEngine should probably be reviewed. Here its just used to store the vars |
---|
201 | /* --- Physics Interface --- */ |
---|
202 | inline PhysicsInterface getPhysicsInterface() const { return this->physicsInterface; } |
---|
203 | inline float getMass() const { return this->physicsInterface.getMass(); } |
---|
204 | inline float getTotalMass() const { return this->physicsInterface.getTotalMass(); } |
---|
205 | inline void setVelocity(const Vector& vel) { this->velocity = vel; } |
---|
206 | |
---|
207 | |
---|
208 | /* --- Misc Stuff Block --- */ |
---|
209 | void debugWE() { this->debugEntity(); } |
---|
210 | ; ///FIXME |
---|
211 | void debugEntity() const; |
---|
212 | |
---|
213 | void pauseTrack(bool stop); |
---|
214 | |
---|
215 | // void updateHealthWidget(); |
---|
216 | // void updateElectronicWidget(); |
---|
217 | // void updateShieldWidget(); |
---|
218 | |
---|
219 | protected: |
---|
220 | inline void setHealth(float health) { this->health = health; this->updateHealthWidget();}; |
---|
221 | void setHealthWidgetVisibility(bool visibility); |
---|
222 | void setHealthMax(float healthMax); |
---|
223 | // void createHealthWidget(); |
---|
224 | // void createShieldWidget(); |
---|
225 | // void createElectronicWidget(); |
---|
226 | void setHealthRegen(float regen) { this->healthRegen = regen; }; |
---|
227 | // CharacterAttributes* charAttr; //!< the character attributes of a world_entity |
---|
228 | |
---|
229 | void setShield(float shield) { this->shield = shield; }; |
---|
230 | void setShieldMax(float shield) { this->shieldMax = shield; }; |
---|
231 | void setShieldTH(float th) { this->shieldTH = th; }; |
---|
232 | void setShieldRegen(float regen) { this->shieldRegen = regen; }; |
---|
233 | void setShieldActive(bool active) { this->bShieldActive = active; }; |
---|
234 | |
---|
235 | void setElectronic(float electronic) { this->electronic = electronic; }; |
---|
236 | void setElectronicMax(float electronic) { this->electronicMax = electronic; }; |
---|
237 | void setElectronicTH(float th) { this->electronicTH = th; }; |
---|
238 | void setElectronicRegen(float regen) { this->electronicRegen = regen; }; |
---|
239 | |
---|
240 | inline void drawDebugTrack(int flag) { this->bDrawTrack = (bool)flag; } |
---|
241 | inline bool isDrawTrack() const { return this->bDrawTrack; } |
---|
242 | |
---|
243 | private: |
---|
244 | void updateHealthWidget(); |
---|
245 | void updateElectronicWidget(); |
---|
246 | void updateShieldWidget(); |
---|
247 | void addTrack(const TiXmlElement* root); |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | protected: |
---|
252 | std::vector<MountPoint*> mountPoints; //!< A list with mount points for this model |
---|
253 | std::map<int, MountPoint*> mountPointMap; |
---|
254 | |
---|
255 | |
---|
256 | |
---|
257 | private: |
---|
258 | //!< TODO maybe we will move the following three entries and the corresponding functions to Playable AND NPC |
---|
259 | //!< started transfering shield/electronic/health from spaceship to WE! (nico, 4.Jun.07) |
---|
260 | float damage; //!< the damage dealt to other objects by colliding. |
---|
261 | float health; //!< The Energy of this Entity, if the Entity has any energy at all. |
---|
262 | float healthMax; //!< The Maximal energy this entity can take. |
---|
263 | float healthRegen; //!< Regeneration Rate of Health, mesured in units per second |
---|
264 | OrxGui::GLGuiEnergyWidgetVertical* healthWidget; //!< The Slider (if wanted). |
---|
265 | |
---|
266 | float shield; //!< current shield |
---|
267 | float shieldMax; //!< maximum shield |
---|
268 | float shieldRegen; //!< shield regeneration rate per second |
---|
269 | float shieldTH; //!< shield threshhold for reactivation |
---|
270 | bool bShieldActive; //!< wheather the shield is working |
---|
271 | OrxGui::GLGuiEnergyWidgetVertical* shieldWidget; //!< holds the widget that shows the shield bar |
---|
272 | |
---|
273 | float electronic; //!< current electronic |
---|
274 | float electronicMax; //!< maximum electronic |
---|
275 | float electronicRegen; //!< electronic regenration rate per tick |
---|
276 | float electronicTH; //!< Threshhold for electronic failure |
---|
277 | OrxGui::GLGuiEnergyWidgetVertical* electronicWidget; //!< holds the widget that shows the electronic bar |
---|
278 | |
---|
279 | |
---|
280 | std::vector<Model*> models; //!< The model that should be loaded for this entity. |
---|
281 | ObjectInformationFile* oiFile; //!< Reference to the object information file discribing the model of this WE |
---|
282 | std::string md2TextureFileName; //!< the file name of the md2 model texture, only if this |
---|
283 | std::string modelLODName; //!< the name of the model lod file |
---|
284 | BVTree* obbTree; //!< this is the obb tree reference needed for collision detection |
---|
285 | AABBTreeNode* aabbNode; //!< the tree node of the first level of a axis aligned bounding boxes tree: model dimension |
---|
286 | |
---|
287 | bool bCollide; //!< If it should be considered for the collisiontest. |
---|
288 | bool bVisible; //!< If it should be visible. |
---|
289 | |
---|
290 | OM_LIST objectListNumber; //!< The ObjectList from ObjectManager this Entity is in. |
---|
291 | ObjectManager::EntityList::iterator objectListIterator; //!< The iterator position of this Entity in the given list of the ObjectManager. |
---|
292 | OM_LIST lastObjectListNumber; //!< the last ObjectList from the ObjectManager this Entity was is in |
---|
293 | |
---|
294 | /* collision reaction stuff */ |
---|
295 | CoRe::CollisionFilter _collisionFilter; //!< filter for collision event filtering (not every entity listens to all collisions) |
---|
296 | bool _bOnGround; //!< flag true if the object is on the ground |
---|
297 | |
---|
298 | PhysicsInterface physicsInterface; //!< the physics object of the WorldEntity |
---|
299 | |
---|
300 | /* network help structures */ |
---|
301 | float scaling; //!< model's scaling factor |
---|
302 | int scaling_handle; //!< handle for syncing var |
---|
303 | |
---|
304 | std::string modelFileName; //!< model's file name |
---|
305 | int modelFileName_handle; //!< handle for syncing var |
---|
306 | |
---|
307 | int list_write; //!< entity's list |
---|
308 | int list_handle; //!< handle for list changes |
---|
309 | |
---|
310 | float health_write; |
---|
311 | int health_handle; |
---|
312 | |
---|
313 | float healthMax_write; |
---|
314 | int healthMax_handle; |
---|
315 | |
---|
316 | |
---|
317 | |
---|
318 | protected: |
---|
319 | Vector velocity; //!< speed of the entity |
---|
320 | Track* entityTrack; //!< this is the track this entity follows (or NULL if none) |
---|
321 | bool bDrawTrack; //!< if true draws the debug track |
---|
322 | |
---|
323 | }; |
---|
324 | |
---|
325 | #endif /* _WORLD_ENTITY_H */ |
---|