Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/world_entity.cc @ 5972

Last change on this file since 5972 was 5829, checked in by patrick, 19 years ago

network: much work on multiplayability, does not yet work

File size: 6.3 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->model = NULL;
48  this->obbTree = NULL;
49
50  if (root)
51    this->loadParams(root);
52
53  this->setVisibiliy(true);
54}
55
56/**
57 *  standard destructor
58*/
59WorldEntity::~WorldEntity ()
60{
61  // Delete the model (unregister it with the ResourceManager)
62  if (likely(this->model != NULL))
63    ResourceManager::getInstance()->unload(this->model);
64  // Delete the obbTree
65  if( this->obbTree != NULL)
66    delete this->obbTree;
67}
68
69/**
70 * loads the WorldEntity Specific Parameters.
71 * @param root: the XML-Element to load the Data From
72 */
73void WorldEntity::loadParams(const TiXmlElement* root)
74{
75  // Do the PNode loading stuff
76  static_cast<PNode*>(this)->loadParams(root);
77
78  // Model Loading
79  LoadParam(root, "model", this, WorldEntity, loadModel)
80      .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)")
81      .defaultValues(2, NULL, 1.0f);
82
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::loadModel(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
123
124    this->obbTree = new OBBTree(depth, (sVec3D*)this->model->getVertexArray(), this->model->getVertexCount());
125    return true;
126  }
127  else
128  {
129    PRINTF(2)("could not create obb-tree, because no model was loaded yet\n");
130    this->obbTree = NULL;
131    return false;
132  }
133}
134
135
136/**
137 * sets the character attributes of a worldentity
138 * @param character attributes
139 *
140 * these attributes don't have to be set, only use them, if you need them
141*/
142//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
143//{}
144
145
146/**
147 *  this function is called, when two entities collide
148 * @param entity: the world entity with whom it collides
149 *
150 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
151 */
152void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location)
153{
154  /**
155   * THIS IS A DEFAULT COLLISION-Effect.
156   * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT
157   * USE::
158   * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; };
159   *
160   * You can always define a default Action.... don't be affraid just test it :)
161   */
162//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
163}
164
165
166/**
167 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
168 *
169 */
170void WorldEntity::postSpawn ()
171{
172}
173
174
175/**
176 *  this method is called by the world if the WorldEntity leaves valid gamespace
177 *
178 * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a
179 * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy).
180 *
181 * NOT YET IMPLEMENTED
182 */
183void WorldEntity::leftWorld ()
184{
185}
186
187
188/**
189 *  this method is called every frame
190 * @param time: the time in seconds that has passed since the last tick
191 *
192 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
193*/
194void WorldEntity::tick(float time)
195{
196}
197
198
199/**
200 *  the entity is drawn onto the screen with this function
201 *
202 * This is a central function of an entity: call it to let the entity painted to the screen.
203 * Just override this function with whatever you want to be drawn.
204*/
205void WorldEntity::draw() const
206{
207  glMatrixMode(GL_MODELVIEW);
208  glPushMatrix();
209  float matrix[4][4];
210
211  /* translate */
212  glTranslatef (this->getAbsCoor ().x,
213                this->getAbsCoor ().y,
214                this->getAbsCoor ().z);
215  /* rotate */ // FIXME: devise a new Way to rotate this
216  this->getAbsDir ().matrix (matrix);
217  glMultMatrixf((float*)matrix);
218
219  if (this->model)
220    this->model->draw();
221  glPopMatrix();
222}
223
224/**
225 * DEBUG-DRAW OF THE BV-Tree.
226 * @param depth What depth to draw
227 * @param drawMode the mode to draw this entity under
228 */
229void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
230{
231  glMatrixMode(GL_MODELVIEW);
232  glPushMatrix();
233  /* translate */
234  glTranslatef (this->getAbsCoor ().x,
235                this->getAbsCoor ().y,
236                this->getAbsCoor ().z);
237  /* rotate */
238  Vector tmpRot = this->getAbsDir().getSpacialAxis();
239  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
240
241  if (this->obbTree)
242    this->obbTree->drawBV(depth, drawMode);
243  glPopMatrix();
244}
Note: See TracBrowser for help on using the repository browser.