Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/world_entity.cc @ 5498

Last change on this file since 5498 was 5498, checked in by bensch, 18 years ago

orxonox/trunk: redocumented the WorldEntity and Weapon classes

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