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 "resource_manager.h" |
---|
24 | #include "load_param.h" |
---|
25 | #include "list.h" |
---|
26 | #include "vector.h" |
---|
27 | #include "obb_tree.h" |
---|
28 | |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | SHELL_COMMAND(model, WorldEntity, loadModel) |
---|
32 | ->describe("sets the Model of the WorldEntity") |
---|
33 | ->defaultValues(2, "models/ships/fighter.obj", 1.0); |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | * Loads the WordEntity-specific Part of any derived Class |
---|
38 | * |
---|
39 | * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves, |
---|
40 | * that can calls WorldEntities loadParams for itself. |
---|
41 | */ |
---|
42 | WorldEntity::WorldEntity(const TiXmlElement* root) |
---|
43 | { |
---|
44 | this->setClassID(CL_WORLD_ENTITY, "WorldEntity"); |
---|
45 | |
---|
46 | this->model = NULL; |
---|
47 | this->obbTree = NULL; |
---|
48 | |
---|
49 | if (root) |
---|
50 | this->loadParams(root); |
---|
51 | |
---|
52 | this->setVisibiliy(true); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * standard destructor |
---|
57 | */ |
---|
58 | WorldEntity::~WorldEntity () |
---|
59 | { |
---|
60 | // Delete the model (unregister it with the ResourceManager) |
---|
61 | if (likely(this->model != NULL)) |
---|
62 | ResourceManager::getInstance()->unload(this->model); |
---|
63 | // Delete the obbTree |
---|
64 | if( this->obbTree != NULL) |
---|
65 | delete this->obbTree; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * loads the WorldEntity Specific Parameters. |
---|
70 | * @param root: the XML-Element to load the Data From |
---|
71 | */ |
---|
72 | void WorldEntity::loadParams(const TiXmlElement* root) |
---|
73 | { |
---|
74 | // Do the PNode loading stuff |
---|
75 | static_cast<PNode*>(this)->loadParams(root); |
---|
76 | |
---|
77 | // Model Loading |
---|
78 | LoadParamNEW(root, "model", this, WorldEntity, loadModel) |
---|
79 | .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
---|
80 | ->defaultValues(2, NULL, 1.0f); |
---|
81 | |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * loads a Model onto a WorldEntity |
---|
86 | * @param fileName the name of the model to load |
---|
87 | * @param scaling the Scaling of the model |
---|
88 | * |
---|
89 | * @todo fix this, so it only has one loadModel-Function. |
---|
90 | */ |
---|
91 | void WorldEntity::loadModel(const char* fileName, float scaling) |
---|
92 | { |
---|
93 | if (this->model) |
---|
94 | ResourceManager::getInstance()->unload(this->model, RP_LEVEL); |
---|
95 | if (fileName != NULL) |
---|
96 | { |
---|
97 | PRINTF(4)("fetching %s\n", fileName); |
---|
98 | if (scaling == 1.0) |
---|
99 | this->model = (Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN); |
---|
100 | else |
---|
101 | this->model = (Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, &scaling); |
---|
102 | |
---|
103 | this->buildObbTree(4); |
---|
104 | } |
---|
105 | else |
---|
106 | this->model = NULL; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * builds the obb-tree |
---|
111 | * @param depth the depth to calculate |
---|
112 | */ |
---|
113 | bool WorldEntity::buildObbTree(unsigned int depth) |
---|
114 | { |
---|
115 | if (this->obbTree) |
---|
116 | delete this->obbTree; |
---|
117 | |
---|
118 | if (this->model != NULL) |
---|
119 | { |
---|
120 | PRINTF(4)("creating obb tree\n"); |
---|
121 | this->obbTree = new OBBTree(depth, (sVec3D*)this->model->getVertexArray(), this->model->getVertexCount()); |
---|
122 | return true; |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | PRINTF(2)("could not create obb-tree, because no model was loaded yet\n"); |
---|
127 | this->obbTree = NULL; |
---|
128 | return false; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | /** |
---|
134 | * sets the character attributes of a worldentity |
---|
135 | * @param character attributes |
---|
136 | * |
---|
137 | * these attributes don't have to be set, only use them, if you need them |
---|
138 | */ |
---|
139 | //void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr) |
---|
140 | //{} |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | * this function is called, when two entities collide |
---|
145 | * @param entity: the world entity with whom it collides |
---|
146 | * |
---|
147 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
148 | */ |
---|
149 | void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location) |
---|
150 | { |
---|
151 | /** |
---|
152 | * THIS IS A DEFAULT COLLISION-Effect. |
---|
153 | * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT |
---|
154 | * USE:: |
---|
155 | * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; }; |
---|
156 | * |
---|
157 | * You can always define a default Action.... don't be affraid just test it :) |
---|
158 | */ |
---|
159 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | /** |
---|
164 | * this is called immediately after the Entity has been constructed, initialized and then Spawned into the World |
---|
165 | * |
---|
166 | */ |
---|
167 | void WorldEntity::postSpawn () |
---|
168 | { |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | /** |
---|
173 | * this method is called by the world if the WorldEntity leaves valid gamespace |
---|
174 | * |
---|
175 | * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a |
---|
176 | * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy). |
---|
177 | * |
---|
178 | * NOT YET IMPLEMENTED |
---|
179 | */ |
---|
180 | void WorldEntity::leftWorld () |
---|
181 | { |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | /** |
---|
186 | * this method is called every frame |
---|
187 | * @param time: the time in seconds that has passed since the last tick |
---|
188 | * |
---|
189 | * Handle all stuff that should update with time inside this method (movement, animation, etc.) |
---|
190 | */ |
---|
191 | void WorldEntity::tick(float time) |
---|
192 | { |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | /** |
---|
197 | * the entity is drawn onto the screen with this function |
---|
198 | * |
---|
199 | * This is a central function of an entity: call it to let the entity painted to the screen. |
---|
200 | * Just override this function with whatever you want to be drawn. |
---|
201 | */ |
---|
202 | void WorldEntity::draw() const |
---|
203 | { |
---|
204 | glMatrixMode(GL_MODELVIEW); |
---|
205 | glPushMatrix(); |
---|
206 | float matrix[4][4]; |
---|
207 | |
---|
208 | /* translate */ |
---|
209 | glTranslatef (this->getAbsCoor ().x, |
---|
210 | this->getAbsCoor ().y, |
---|
211 | this->getAbsCoor ().z); |
---|
212 | /* rotate */ // FIXME: devise a new Way to rotate this |
---|
213 | this->getAbsDir ().matrix (matrix); |
---|
214 | glMultMatrixf((float*)matrix); |
---|
215 | |
---|
216 | if (this->model) |
---|
217 | this->model->draw(); |
---|
218 | glPopMatrix(); |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * DEBUG-DRAW OF THE BV-Tree. |
---|
223 | * @param depth What depth to draw |
---|
224 | * @param drawMode the mode to draw this entity under |
---|
225 | */ |
---|
226 | void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const |
---|
227 | { |
---|
228 | glMatrixMode(GL_MODELVIEW); |
---|
229 | glPushMatrix(); |
---|
230 | /* translate */ |
---|
231 | glTranslatef (this->getAbsCoor ().x, |
---|
232 | this->getAbsCoor ().y, |
---|
233 | this->getAbsCoor ().z); |
---|
234 | /* rotate */ |
---|
235 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
236 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
237 | |
---|
238 | if (this->obbTree) |
---|
239 | this->obbTree->drawBV(depth, drawMode); |
---|
240 | glPopMatrix(); |
---|
241 | } |
---|