| 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 |  | 
|---|
| 18 | #include <iostream> | 
|---|
| 19 |  | 
|---|
| 20 | #include "world_entity.h" | 
|---|
| 21 | #include "shell_command.h" | 
|---|
| 22 |  | 
|---|
| 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(1, "models/ships/reaplow.obj"); | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | /** | 
|---|
| 37 |  *  Loads the WordEntity-specific Part of any derived Class | 
|---|
| 38 | */ | 
|---|
| 39 | WorldEntity::WorldEntity(const TiXmlElement* root) | 
|---|
| 40 | { | 
|---|
| 41 |   this->setClassID(CL_WORLD_ENTITY, "WorldEntity"); | 
|---|
| 42 |  | 
|---|
| 43 |   this->model = NULL; | 
|---|
| 44 |   this->obbTree = NULL; | 
|---|
| 45 |  | 
|---|
| 46 |   if (root) | 
|---|
| 47 |     this->loadParams(root); | 
|---|
| 48 |  | 
|---|
| 49 |   this->setVisibiliy(true); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | /** | 
|---|
| 53 |  *  standard destructor | 
|---|
| 54 | */ | 
|---|
| 55 | WorldEntity::~WorldEntity () | 
|---|
| 56 | { | 
|---|
| 57 |   // if( collisioncluster != NULL) delete collisioncluster; | 
|---|
| 58 |   if (this->model) | 
|---|
| 59 |     ResourceManager::getInstance()->unload(this->model); | 
|---|
| 60 |   if( this->obbTree) | 
|---|
| 61 |     delete this->obbTree; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 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 |  | 
|---|
| 72 | /** | 
|---|
| 73 |  * loads a Model onto a WorldEntity | 
|---|
| 74 |  * @param fileName the name of the model to load | 
|---|
| 75 |  * @param scaling the Scaling of the model | 
|---|
| 76 | */ | 
|---|
| 77 | void WorldEntity::loadModelWithScale(const char* fileName, float scaling) | 
|---|
| 78 | { | 
|---|
| 79 |   if (this->model) | 
|---|
| 80 |     ResourceManager::getInstance()->unload(this->model, RP_LEVEL); | 
|---|
| 81 |   if (fileName != NULL) | 
|---|
| 82 |   { | 
|---|
| 83 |     PRINTF(4)("fetching %s\n", fileName); | 
|---|
| 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 |  | 
|---|
| 89 |     this->buildObbTree(4); | 
|---|
| 90 |   } | 
|---|
| 91 |   else | 
|---|
| 92 |     this->model = NULL; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | /** | 
|---|
| 96 |  * builds the obb-tree | 
|---|
| 97 |  * @param depth the depth to calculate | 
|---|
| 98 |  */ | 
|---|
| 99 | bool WorldEntity::buildObbTree(unsigned int depth) | 
|---|
| 100 | { | 
|---|
| 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 |   } | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 | /** | 
|---|
| 120 |  * sets the character attributes of a worldentity | 
|---|
| 121 |  * @param character attributes | 
|---|
| 122 |  * | 
|---|
| 123 |  * these attributes don't have to be set, only use them, if you need them | 
|---|
| 124 | */ | 
|---|
| 125 | void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr) | 
|---|
| 126 | {} | 
|---|
| 127 |  | 
|---|
| 128 |  | 
|---|
| 129 | /** | 
|---|
| 130 |  * gets the Character attributes of this worldentity | 
|---|
| 131 |  * @returns character attributes | 
|---|
| 132 | */ | 
|---|
| 133 | CharacterAttributes* WorldEntity::getCharacterAttributes() | 
|---|
| 134 | {} | 
|---|
| 135 |  | 
|---|
| 136 |  | 
|---|
| 137 | /** | 
|---|
| 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 | { | 
|---|
| 145 | //  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | /** | 
|---|
| 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 | 
|---|
| 152 |  * | 
|---|
| 153 |  * calculate the damage depending | 
|---|
| 154 | */ | 
|---|
| 155 | void WorldEntity::hit(WorldEntity* weapon, Vector* loc) {} | 
|---|
| 156 |  | 
|---|
| 157 |  | 
|---|
| 158 | /** | 
|---|
| 159 |  *  this is called immediately after the Entity has been constructed and initialized | 
|---|
| 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. | 
|---|
| 163 | */ | 
|---|
| 164 | void WorldEntity::postSpawn () | 
|---|
| 165 | { | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 | /** | 
|---|
| 170 |  *  this method is called by the world if the WorldEntity leaves valid gamespace | 
|---|
| 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). | 
|---|
| 174 | */ | 
|---|
| 175 | void WorldEntity::leftWorld () | 
|---|
| 176 | { | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 |  | 
|---|
| 180 | /** | 
|---|
| 181 |  *  this method is called every frame | 
|---|
| 182 |  * @param time: the time in seconds that has passed since the last tick | 
|---|
| 183 |  * | 
|---|
| 184 |  * Handle all stuff that should update with time inside this method (movement, animation, etc.) | 
|---|
| 185 | */ | 
|---|
| 186 | void WorldEntity::tick(float time) | 
|---|
| 187 | { | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | /** | 
|---|
| 191 |  *  the entity is drawn onto the screen with this function | 
|---|
| 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. | 
|---|
| 195 | */ | 
|---|
| 196 | void WorldEntity::draw() | 
|---|
| 197 | { | 
|---|
| 198 |   glMatrixMode(GL_MODELVIEW); | 
|---|
| 199 |   glPushMatrix(); | 
|---|
| 200 |   float matrix[4][4]; | 
|---|
| 201 |  | 
|---|
| 202 |   /* translate */ | 
|---|
| 203 |   glTranslatef (this->getAbsCoor ().x, | 
|---|
| 204 |                 this->getAbsCoor ().y, | 
|---|
| 205 |                 this->getAbsCoor ().z); | 
|---|
| 206 |   /* rotate */ | 
|---|
| 207 |   this->getAbsDir ().matrix (matrix); | 
|---|
| 208 |   glMultMatrixf((float*)matrix); | 
|---|
| 209 |  | 
|---|
| 210 |   if (this->model) | 
|---|
| 211 |     this->model->draw(); | 
|---|
| 212 |   glPopMatrix(); | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 |  | 
|---|
| 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 */ | 
|---|
| 225 |   Vector tmpRot = this->getAbsDir().getSpacialAxis(); | 
|---|
| 226 |   glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); | 
|---|
| 227 |  | 
|---|
| 228 |   if (this->obbTree) | 
|---|
| 229 |     this->obbTree->drawBV(depth, drawMode); | 
|---|
| 230 |   glPopMatrix(); | 
|---|
| 231 | } | 
|---|