Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

renamed newclassid to classid and newobjectlist to objectlist

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