Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/power_ups/power_up.cc @ 9853

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

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

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
27
28PowerUp::PowerUp(float r, float g, float b)
29{
30  this->setClassID(CL_POWER_UP, "PowerUp");
31
32  this->respawnType = RESPAWN_TIME;
33  this->respawnStart = 10;
34  this->model = NULL;
35  /*  if(!PowerUp::sphereModel) {*/
36
37  Model* sphereModel = new PrimitiveModel(PRIM_SPHERE, 7, 5);
38
39  this->setModel(sphereModel);
40  this->buildObbTree( 4);
41  this->sphereMaterial = new Material;
42  this->sphereMaterial->setTransparency(.8);
43  this->sphereMaterial->setDiffuse(r, g, b);
44  this->toList(OM_COMMON);
45
46  this->soundSource.setSourceNode(this);
47  this->pickupBuffer = NULL;
48  this->respawnBuffer = NULL;
49
50  this->collider = NULL;
51}
52
53PowerUp::~PowerUp ()
54{
55  delete this->sphereMaterial;
56  if (this->pickupBuffer != NULL)
57    ResourceManager::getInstance()->unload(this->pickupBuffer);
58  if (this->respawnBuffer != NULL)
59    ResourceManager::getInstance()->unload(this->respawnBuffer);
60}
61
62
63void PowerUp::loadParams(const TiXmlElement* root)
64{
65  WorldEntity::loadParams(root);
66
67  LoadParam(root, "respawnType", this, PowerUp, setRespawnType);
68
69  LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime);
70
71  LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound);
72
73  LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound);
74}
75
76
77void PowerUp::loadPickupSound(const std::string& pickupSound)
78{
79  if (this->pickupBuffer != NULL)
80    ResourceManager::getInstance()->unload(this->pickupBuffer);
81
82  else if (!pickupSound.empty())
83  {
84    this->pickupBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(pickupSound, WAV);
85    if (this->pickupBuffer != NULL)
86    {
87      PRINTF(4)("Loaded sound %s to Pickup: %s.\n", pickupSound.c_str(), this->getCName());
88    }
89    else
90    {
91      PRINTF(2)("Failed to load sound %s to pickup %s.\n.", pickupSound.c_str(), this->getCName());
92    }
93  }
94  else
95    this->pickupBuffer = NULL;
96}
97
98void PowerUp::loadRespawnSound(const std::string& respawnSound)
99{
100  if (this->respawnBuffer != NULL)
101    ResourceManager::getInstance()->unload(this->respawnBuffer);
102
103  else if (!respawnSound.empty())
104  {
105    this->respawnBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(respawnSound, WAV);
106    if (this->respawnBuffer != NULL)
107    {
108      PRINTF(4)("Loaded sound %s to Pickup: %s.\n", respawnSound.c_str(), this->getCName());
109    }
110    else
111    {
112      PRINTF(2)("Failed to load sound %s to respawn %s.\n.", respawnSound.c_str(), this->getCName());
113    }
114  }
115  else
116    this->respawnBuffer = NULL;
117}
118
119
120void PowerUp::collidesWith (WorldEntity* entity, const Vector& location)
121{
122  if(this->collider != entity && entity->isA(CL_EXTENDABLE))
123  {
124    this->collider = entity;
125    if(dynamic_cast<Extendable*>(entity)->pickup(this))
126    {
127      if(pickupBuffer != NULL)
128        this->soundSource.play(this->pickupBuffer);
129
130      switch(respawnType)
131      {
132      case RESPAWN_NONE:
133        this->toList(OM_DEAD);
134        break;
135      case RESPAWN_TIME:
136        this->toList(OM_DEAD_TICK);
137        this->respawnTime = this->respawnStart;
138        break;
139        default:
140          /* NOT HANDLED */
141          break;
142      }
143    }
144  }
145}
146
147void PowerUp::tick(float dt)
148{
149  if(this->getOMListNumber() != OM_COMMON)
150  {
151    this->respawnTime -= dt;
152    if(this->respawnTime <= 0)
153    {
154      this->toList(OM_COMMON);
155      this->collider = NULL;
156      if (likely(this->respawnBuffer != NULL))
157        this->soundSource.play(this->respawnBuffer);
158
159    }
160  }
161}
162
163void PowerUp::draw() const
164{
165  if(this->model != NULL)
166  {
167    glMatrixMode(GL_MODELVIEW);
168    glPushMatrix();
169    glTranslatef (this->getAbsCoor ().x,
170                  this->getAbsCoor ().y,
171                  this->getAbsCoor ().z);
172    Vector tmpRot = this->getAbsDir().getSpacialAxis();
173    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
174    this->model->draw();
175    glPopMatrix();
176  }
177  this->sphereMaterial->select();
178  WorldEntity::draw();
179}
180
181const char* PowerUp::respawnTypes[] =
182  {
183    "none",
184    "time"
185  };
186
187
188void PowerUp::setRespawnType(const std::string& type)
189{
190  for(int i = 0; i < RESPAWN_size; ++i)
191  {
192    if(type == respawnTypes[i])
193    {
194      this->respawnType = (PowerUpRespawn)i;
195      break;
196    }
197  }
198}
199
200void PowerUp::setRespawnTime(const float respawnTime)
201{
202  this->respawnStart = respawnTime;
203}
204
205
Note: See TracBrowser for help on using the repository browser.