Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed out some warnings

File size: 4.8 KB
RevLine 
[4597]1/*
[2077]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:
[6113]12   main-programmer: Manuel Leuenberger
[2077]13   co-programmer: ...
14*/
15
[5439]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
[2077]17
[5439]18
[2077]19#include "power_up.h"
[6113]20#include "extendable.h"
21#include "primitive_model.h"
[2077]22
[7193]23#include "util/loading/resource_manager.h"
24#include "util/loading/load_param.h"
[6815]25
[2077]26using namespace std;
27
[6113]28PowerUp::PowerUp(float r, float g, float b)
29{
[6424]30  this->setClassID(CL_POWER_UP, "PowerUp");
31
[6973]32  this->respawnType = RESPAWN_TIME;
[6589]33  this->respawnStart = 10;
34  this->model = NULL;
[7221]35  /*  if(!PowerUp::sphereModel) {*/
[6282]36
37  Model* sphereModel = new PrimitiveModel(PRIM_SPHERE, 7, 5);
38
39  this->setModel(sphereModel);
40  this->buildObbTree( 4);
[6113]41  this->sphereMaterial = new Material;
[6973]42  this->sphereMaterial->setTransparency(.8);
[6113]43  this->sphereMaterial->setDiffuse(r, g, b);
[6150]44  this->toList(OM_COMMON);
[7065]45
46  this->soundSource.setSourceNode(this);
[7066]47  this->pickupBuffer = NULL;
48  this->respawnBuffer = NULL;
[7102]49
50  this->collider = NULL;
[6113]51}
[2077]52
[6113]53PowerUp::~PowerUp ()
[4597]54{
[6113]55  delete this->sphereMaterial;
[7066]56  if (this->pickupBuffer != NULL)
57    ResourceManager::getInstance()->unload(this->pickupBuffer);
58  if (this->respawnBuffer != NULL)
59    ResourceManager::getInstance()->unload(this->respawnBuffer);
[4597]60}
[2077]61
62
[6113]63void PowerUp::loadParams(const TiXmlElement* root)
64{
[6512]65  WorldEntity::loadParams(root);
[7065]66
[6973]67  LoadParam(root, "respawnType", this, PowerUp, setRespawnType);
[7065]68
[6973]69  LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime);
[7065]70
71  LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound);
[7066]72
73  LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound);
[6113]74}
[2077]75
76
[7221]77void PowerUp::loadPickupSound(const std::string& pickupSound)
[7065]78{
[7066]79  if (this->pickupBuffer != NULL)
80    ResourceManager::getInstance()->unload(this->pickupBuffer);
[7065]81
[7221]82  else if (!pickupSound.empty())
[7065]83  {
[7460]84    this->pickupBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(pickupSound, WAV);
[7221]85    if (this->pickupBuffer != NULL)
86    {
87      PRINTF(4)("Loaded sound %s to Pickup: %s.\n", pickupSound.c_str(), this->getName());
88    }
89    else
90    {
91      PRINTF(2)("Failed to load sound %s to pickup %s.\n.", pickupSound.c_str(), this->getName());
92    }
[7065]93  }
94  else
[7066]95    this->pickupBuffer = NULL;
[7065]96}
97
[7221]98void PowerUp::loadRespawnSound(const std::string& respawnSound)
[7066]99{
100  if (this->respawnBuffer != NULL)
101    ResourceManager::getInstance()->unload(this->respawnBuffer);
102
[7221]103  else if (!respawnSound.empty())
[7066]104  {
[7460]105    this->respawnBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(respawnSound, WAV);
[7066]106    if (this->respawnBuffer != NULL)
107    {
[7221]108      PRINTF(4)("Loaded sound %s to Pickup: %s.\n", respawnSound.c_str(), this->getName());
[7066]109    }
110    else
111    {
[7221]112      PRINTF(2)("Failed to load sound %s to respawn %s.\n.", respawnSound.c_str(), this->getName());
[7066]113    }
114  }
115  else
116    this->respawnBuffer = NULL;
117}
118
119
[6113]120void PowerUp::collidesWith (WorldEntity* entity, const Vector& location)
121{
[7103]122  if(this->collider != entity && entity->isA(CL_EXTENDABLE))
[6113]123  {
[7102]124    this->collider = entity;
[6113]125    if(dynamic_cast<Extendable*>(entity)->pickup(this))
126    {
[7066]127      if(pickupBuffer != NULL)
128        this->soundSource.play(this->pickupBuffer);
[7065]129
[7221]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;
[8350]139        default:
140          /* NOT HANDLED */
141          break;
[6973]142      }
[6113]143    }
144  }
145}
[2077]146
[7221]147void PowerUp::tick(float dt)
148{
149  if(this->getOMListNumber() != OM_COMMON)
150  {
[6589]151    this->respawnTime -= dt;
[7066]152    if(this->respawnTime <= 0)
153    {
[6589]154      this->toList(OM_COMMON);
[7102]155      this->collider = NULL;
[7077]156      if (likely(this->respawnBuffer != NULL))
157        this->soundSource.play(this->respawnBuffer);
[7066]158
[6589]159    }
160  }
161}
162
[6113]163void PowerUp::draw() const
[5434]164{
[7221]165  if(this->model != NULL)
166  {
[6589]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  }
[6547]177  this->sphereMaterial->select();
178  WorldEntity::draw();
[5434]179}
180
[7221]181const char* PowerUp::respawnTypes[] =
182  {
183    "none",
184    "time"
185  };
[6113]186
[7221]187
188void PowerUp::setRespawnType(const std::string& type)
[6113]189{
[7221]190  for(int i = 0; i < RESPAWN_size; ++i)
191  {
192    if(type == respawnTypes[i])
193    {
[6113]194      this->respawnType = (PowerUpRespawn)i;
195      break;
196    }
197  }
198}
199
[6973]200void PowerUp::setRespawnTime(const float respawnTime)
201{
202  this->respawnStart = respawnTime;
203}
[6498]204
205
Note: See TracBrowser for help on using the repository browser.