Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5996 was 5996, checked in by patrick, 18 years ago

orxonox/trunk: merged network branche into trunk with command svn merge -r 5824:HEAD

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