[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 | |
---|
| 22 | #include "resource_manager.h" |
---|
| 23 | #include "load_param.h" |
---|
[3608] | 24 | #include "list.h" |
---|
[3803] | 25 | #include "vector.h" |
---|
[4682] | 26 | #include "obb_tree.h" |
---|
[3608] | 27 | |
---|
[2036] | 28 | using namespace std; |
---|
| 29 | |
---|
[5208] | 30 | SHELL_COMMAND(model, WorldEntity, loadModel) |
---|
| 31 | ->describe("sets the Model of the WorldEntity") |
---|
| 32 | ->defaultValues(1, "models/ships/reaplow.obj"); |
---|
| 33 | |
---|
| 34 | |
---|
[2043] | 35 | /** |
---|
[4836] | 36 | * Loads the WordEntity-specific Part of any derived Class |
---|
[2043] | 37 | */ |
---|
[4261] | 38 | WorldEntity::WorldEntity(const TiXmlElement* root) |
---|
[2190] | 39 | { |
---|
[4320] | 40 | this->setClassID(CL_WORLD_ENTITY, "WorldEntity"); |
---|
[4597] | 41 | |
---|
[4261] | 42 | this->model = NULL; |
---|
[4682] | 43 | this->obbTree = NULL; |
---|
[4261] | 44 | |
---|
| 45 | if (root) |
---|
| 46 | this->loadParams(root); |
---|
| 47 | |
---|
[4885] | 48 | this->setVisibiliy(true); |
---|
[2190] | 49 | } |
---|
[2043] | 50 | |
---|
| 51 | /** |
---|
[4836] | 52 | * standard destructor |
---|
[2043] | 53 | */ |
---|
[2190] | 54 | WorldEntity::~WorldEntity () |
---|
[2036] | 55 | { |
---|
[3474] | 56 | // if( collisioncluster != NULL) delete collisioncluster; |
---|
[5302] | 57 | if (likely(this->model != NULL)) |
---|
[3672] | 58 | ResourceManager::getInstance()->unload(this->model); |
---|
[5302] | 59 | if( this->obbTree != NULL) |
---|
[4814] | 60 | delete this->obbTree; |
---|
[3531] | 61 | } |
---|
| 62 | |
---|
[4436] | 63 | void WorldEntity::loadParams(const TiXmlElement* root) |
---|
| 64 | { |
---|
| 65 | static_cast<PNode*>(this)->loadParams(root); |
---|
| 66 | // Model Loading |
---|
| 67 | LoadParam<WorldEntity>(root, "model", this, &WorldEntity::loadModel) |
---|
| 68 | .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)") ; |
---|
| 69 | } |
---|
| 70 | |
---|
[3531] | 71 | /** |
---|
[4885] | 72 | * loads a Model onto a WorldEntity |
---|
[4836] | 73 | * @param fileName the name of the model to load |
---|
[5057] | 74 | * @param scaling the Scaling of the model |
---|
[4261] | 75 | */ |
---|
[5057] | 76 | void WorldEntity::loadModelWithScale(const char* fileName, float scaling) |
---|
[4261] | 77 | { |
---|
| 78 | if (this->model) |
---|
| 79 | ResourceManager::getInstance()->unload(this->model, RP_LEVEL); |
---|
[4732] | 80 | if (fileName != NULL) |
---|
| 81 | { |
---|
[5066] | 82 | PRINTF(4)("fetching %s\n", fileName); |
---|
[5057] | 83 | if (scaling == 1.0) |
---|
| 84 | this->model = (Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN); |
---|
| 85 | else |
---|
| 86 | this->model = (Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, &scaling); |
---|
| 87 | |
---|
[5061] | 88 | this->buildObbTree(4); |
---|
[4732] | 89 | } |
---|
| 90 | else |
---|
| 91 | this->model = NULL; |
---|
[4261] | 92 | } |
---|
| 93 | |
---|
[5061] | 94 | /** |
---|
| 95 | * builds the obb-tree |
---|
| 96 | * @param depth the depth to calculate |
---|
| 97 | */ |
---|
| 98 | bool WorldEntity::buildObbTree(unsigned int depth) |
---|
| 99 | { |
---|
[5428] | 100 | if (this->obbTree) |
---|
| 101 | delete this->obbTree; |
---|
| 102 | |
---|
| 103 | if (this->model) |
---|
| 104 | { |
---|
| 105 | PRINTF(4)("creating obb tree\n"); |
---|
| 106 | this->obbTree = new OBBTree(depth, (sVec3D*)this->model->getVertexArray(), this->model->getVertexCount()); |
---|
| 107 | return true; |
---|
| 108 | } |
---|
| 109 | else |
---|
| 110 | { |
---|
| 111 | PRINTF(2)("could not create obb-tree, because no model was loaded yet\n"); |
---|
| 112 | this->obbTree = NULL; |
---|
| 113 | return false; |
---|
| 114 | } |
---|
[5061] | 115 | } |
---|
[5057] | 116 | |
---|
[5061] | 117 | |
---|
[4261] | 118 | /** |
---|
[4885] | 119 | * sets the character attributes of a worldentity |
---|
[4836] | 120 | * @param character attributes |
---|
[4885] | 121 | * |
---|
| 122 | * these attributes don't have to be set, only use them, if you need them |
---|
[2043] | 123 | */ |
---|
[3583] | 124 | void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr) |
---|
| 125 | {} |
---|
[2036] | 126 | |
---|
[3583] | 127 | |
---|
[2043] | 128 | /** |
---|
[4885] | 129 | * gets the Character attributes of this worldentity |
---|
[4836] | 130 | * @returns character attributes |
---|
[2043] | 131 | */ |
---|
[3583] | 132 | CharacterAttributes* WorldEntity::getCharacterAttributes() |
---|
| 133 | {} |
---|
| 134 | |
---|
| 135 | |
---|
[2043] | 136 | /** |
---|
[5029] | 137 | * this function is called, when two entities collide |
---|
| 138 | * @param entity: the world entity with whom it collides |
---|
| 139 | * |
---|
| 140 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 141 | */ |
---|
| 142 | void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 143 | { |
---|
[5257] | 144 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
[5029] | 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
[4836] | 148 | * this function is called, when the ship is hit by a waepon |
---|
| 149 | * @param weapon: the laser/rocket/shoot that hits. |
---|
| 150 | * @param loc: place where it is hit |
---|
[4885] | 151 | * |
---|
| 152 | * calculate the damage depending |
---|
[2043] | 153 | */ |
---|
[3578] | 154 | void WorldEntity::hit(WorldEntity* weapon, Vector* loc) {} |
---|
[2043] | 155 | |
---|
| 156 | |
---|
| 157 | /** |
---|
[4836] | 158 | * this is called immediately after the Entity has been constructed and initialized |
---|
[4885] | 159 | * |
---|
| 160 | * Put any initialisation code that requires knowledge of location (placement if the Entity is free) and owner of the entity here. |
---|
| 161 | * DO NOT place such code in the constructor, those variables are set AFTER the entity is constucted. |
---|
[2043] | 162 | */ |
---|
[3229] | 163 | void WorldEntity::postSpawn () |
---|
[2190] | 164 | { |
---|
| 165 | } |
---|
[2043] | 166 | |
---|
[3583] | 167 | |
---|
[2043] | 168 | /** |
---|
[4836] | 169 | * this method is called by the world if the WorldEntity leaves valid gamespace |
---|
[4885] | 170 | * |
---|
| 171 | * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a |
---|
| 172 | * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy). |
---|
[2190] | 173 | */ |
---|
[3583] | 174 | void WorldEntity::leftWorld () |
---|
[2190] | 175 | { |
---|
| 176 | } |
---|
[2043] | 177 | |
---|
[3583] | 178 | |
---|
[2190] | 179 | /** |
---|
[4836] | 180 | * this method is called every frame |
---|
| 181 | * @param time: the time in seconds that has passed since the last tick |
---|
[4885] | 182 | * |
---|
| 183 | * Handle all stuff that should update with time inside this method (movement, animation, etc.) |
---|
[2043] | 184 | */ |
---|
[4570] | 185 | void WorldEntity::tick(float time) |
---|
[2190] | 186 | { |
---|
| 187 | } |
---|
[3583] | 188 | |
---|
| 189 | /** |
---|
[4836] | 190 | * the entity is drawn onto the screen with this function |
---|
[4885] | 191 | * |
---|
| 192 | * This is a central function of an entity: call it to let the entity painted to the screen. |
---|
| 193 | * Just override this function with whatever you want to be drawn. |
---|
[3365] | 194 | */ |
---|
[4570] | 195 | void WorldEntity::draw() |
---|
[3803] | 196 | { |
---|
| 197 | glMatrixMode(GL_MODELVIEW); |
---|
| 198 | glPushMatrix(); |
---|
| 199 | float matrix[4][4]; |
---|
[4570] | 200 | |
---|
[3803] | 201 | /* translate */ |
---|
[4570] | 202 | glTranslatef (this->getAbsCoor ().x, |
---|
| 203 | this->getAbsCoor ().y, |
---|
| 204 | this->getAbsCoor ().z); |
---|
[5435] | 205 | /* rotate */ // FIXME: devise a new Way to rotate this |
---|
[3803] | 206 | this->getAbsDir ().matrix (matrix); |
---|
| 207 | glMultMatrixf((float*)matrix); |
---|
[2043] | 208 | |
---|
[3803] | 209 | if (this->model) |
---|
| 210 | this->model->draw(); |
---|
| 211 | glPopMatrix(); |
---|
| 212 | } |
---|
[3583] | 213 | |
---|
[3803] | 214 | |
---|
[5431] | 215 | void WorldEntity::drawBVTree(unsigned int depth, int drawMode) |
---|
[4684] | 216 | { |
---|
| 217 | glMatrixMode(GL_MODELVIEW); |
---|
| 218 | glPushMatrix(); |
---|
| 219 | /* translate */ |
---|
| 220 | glTranslatef (this->getAbsCoor ().x, |
---|
| 221 | this->getAbsCoor ().y, |
---|
| 222 | this->getAbsCoor ().z); |
---|
| 223 | /* rotate */ |
---|
[4998] | 224 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 225 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[4684] | 226 | |
---|
| 227 | if (this->obbTree) |
---|
| 228 | this->obbTree->drawBV(depth, drawMode); |
---|
| 229 | glPopMatrix(); |
---|
| 230 | } |
---|