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