Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/power_ups/power_up.cc @ 9727

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

orxonox/new_class_id: new Executor construct, that is much more typesafe, faster, and easier to extend…

Also changed the LoadParam process, and adapted ScriptEngine calls

Then at the end, some missing headers appeared, and appended them to all the cc-files again.

File size: 4.8 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Manuel Leuenberger
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
18
19#include "power_up.h"
20#include "extendable.h"
21#include "primitive_model.h"
22
23#include "util/loading/resource_manager.h"
24#include "util/loading/load_param.h"
25
26#include "debug.h"
27
28ObjectListDefinition(PowerUp);
29
30PowerUp::PowerUp(float r, float g, float b)
31{
32  this->registerObject(this, PowerUp::_objectList);
33
34  this->respawnType = RESPAWN_TIME;
35  this->respawnStart = 10;
36  this->model = NULL;
37  /*  if(!PowerUp::sphereModel) {*/
38
39  Model* sphereModel = new PrimitiveModel(PRIM_SPHERE, 7, 5);
40
41  this->setModel(sphereModel);
42  this->buildObbTree( 4);
43  this->sphereMaterial = new Material;
44  this->sphereMaterial->setTransparency(.8);
45  this->sphereMaterial->setDiffuse(r, g, b);
46  this->toList(OM_COMMON);
47
48  this->soundSource.setSourceNode(this);
49  this->pickupBuffer = NULL;
50  this->respawnBuffer = NULL;
51
52  this->collider = NULL;
53}
54
55PowerUp::~PowerUp ()
56{
57  delete this->sphereMaterial;
58  if (this->pickupBuffer != NULL)
59    ResourceManager::getInstance()->unload(this->pickupBuffer);
60  if (this->respawnBuffer != NULL)
61    ResourceManager::getInstance()->unload(this->respawnBuffer);
62}
63
64
65void PowerUp::loadParams(const TiXmlElement* root)
66{
67  WorldEntity::loadParams(root);
68
69  LoadParam(root, "respawnType", this, PowerUp, setRespawnType);
70
71  LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime);
72
73  LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound);
74
75  LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound);
76}
77
78
79void PowerUp::loadPickupSound(const std::string& pickupSound)
80{
81  if (this->pickupBuffer != NULL)
82    ResourceManager::getInstance()->unload(this->pickupBuffer);
83
84  else if (!pickupSound.empty())
85  {
86    this->pickupBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(pickupSound, WAV);
87    if (this->pickupBuffer != NULL)
88    {
89      PRINTF(4)("Loaded sound %s to Pickup: %s.\n", pickupSound.c_str(), this->getCName());
90    }
91    else
92    {
93      PRINTF(2)("Failed to load sound %s to pickup %s.\n.", pickupSound.c_str(), this->getCName());
94    }
95  }
96  else
97    this->pickupBuffer = NULL;
98}
99
100void PowerUp::loadRespawnSound(const std::string& respawnSound)
101{
102  if (this->respawnBuffer != NULL)
103    ResourceManager::getInstance()->unload(this->respawnBuffer);
104
105  else if (!respawnSound.empty())
106  {
107    this->respawnBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(respawnSound, WAV);
108    if (this->respawnBuffer != NULL)
109    {
110      PRINTF(4)("Loaded sound %s to Pickup: %s.\n", respawnSound.c_str(), this->getCName());
111    }
112    else
113    {
114      PRINTF(2)("Failed to load sound %s to respawn %s.\n.", respawnSound.c_str(), this->getCName());
115    }
116  }
117  else
118    this->respawnBuffer = NULL;
119}
120
121
122void PowerUp::collidesWith (WorldEntity* entity, const Vector& location)
123{
124  if(this->collider != entity && entity->isA(Extendable::classID()))
125  {
126    this->collider = entity;
127    if(dynamic_cast<Extendable*>(entity)->pickup(this))
128    {
129      if(pickupBuffer != NULL)
130        this->soundSource.play(this->pickupBuffer);
131
132      switch(respawnType)
133      {
134      case RESPAWN_NONE:
135        this->toList(OM_DEAD);
136        break;
137      case RESPAWN_TIME:
138        this->toList(OM_DEAD_TICK);
139        this->respawnTime = this->respawnStart;
140        break;
141        default:
142          /* NOT HANDLED */
143          break;
144      }
145    }
146  }
147}
148
149void PowerUp::tick(float dt)
150{
151  if(this->getOMListNumber() != OM_COMMON)
152  {
153    this->respawnTime -= dt;
154    if(this->respawnTime <= 0)
155    {
156      this->toList(OM_COMMON);
157      this->collider = NULL;
158      if (likely(this->respawnBuffer != NULL))
159        this->soundSource.play(this->respawnBuffer);
160
161    }
162  }
163}
164
165void PowerUp::draw() const
166{
167  if(this->model != NULL)
168  {
169    glMatrixMode(GL_MODELVIEW);
170    glPushMatrix();
171    glTranslatef (this->getAbsCoor ().x,
172                  this->getAbsCoor ().y,
173                  this->getAbsCoor ().z);
174    Vector tmpRot = this->getAbsDir().getSpacialAxis();
175    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
176    this->model->draw();
177    glPopMatrix();
178  }
179  this->sphereMaterial->select();
180  WorldEntity::draw();
181}
182
183const char* PowerUp::respawnTypes[] =
184  {
185    "none",
186    "time"
187  };
188
189
190void PowerUp::setRespawnType(const std::string& type)
191{
192  for(int i = 0; i < RESPAWN_size; ++i)
193  {
194    if(type == respawnTypes[i])
195    {
196      this->respawnType = (PowerUpRespawn)i;
197      break;
198    }
199  }
200}
201
202void PowerUp::setRespawnTime(const float respawnTime)
203{
204  this->respawnStart = respawnTime;
205}
206
207
Note: See TracBrowser for help on using the repository browser.