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