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