Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

health

File size: 5.4 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 "resource_manager.h"
24#include "load_param.h"
25
26using namespace std;
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
51PowerUp::~PowerUp ()
52{
53  delete this->sphereMaterial;
54  if (this->pickupBuffer != NULL)
55    ResourceManager::getInstance()->unload(this->pickupBuffer);
56  if (this->respawnBuffer != NULL)
57    ResourceManager::getInstance()->unload(this->respawnBuffer);
58}
59
60
61void PowerUp::loadParams(const TiXmlElement* root)
62{
63  WorldEntity::loadParams(root);
64
65  LoadParam(root, "respawnType", this, PowerUp, setRespawnType);
66
67  LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime);
68
69  LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound);
70
71  LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound);
72}
73
74
75void PowerUp::loadPickupSound(const char* pickupSound)
76{
77  if (this->pickupBuffer != NULL)
78    ResourceManager::getInstance()->unload(this->pickupBuffer);
79
80  else if (pickupSound != NULL)
81{
82  this->pickupBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(pickupSound, WAV);
83  if (this->pickupBuffer != NULL)
84  {
85    PRINTF(4)("Loaded sound %s to Pickup: %s.\n", pickupSound, this->getName());
86  }
87  else
88  {
89    PRINTF(2)("Failed to load sound %s to pickup %s.\n.", pickupSound, this->getName());
90  }
91}
92  else
93    this->pickupBuffer = NULL;
94}
95
96void PowerUp::loadRespawnSound(const char* respawnSound)
97{
98  if (this->respawnBuffer != NULL)
99    ResourceManager::getInstance()->unload(this->respawnBuffer);
100
101  else if (respawnSound != NULL)
102  {
103    this->respawnBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(respawnSound, WAV);
104    if (this->respawnBuffer != NULL)
105    {
106      PRINTF(4)("Loaded sound %s to Pickup: %s.\n", respawnSound, this->getName());
107    }
108    else
109    {
110      PRINTF(2)("Failed to load sound %s to respawn %s.\n.", respawnSound, this->getName());
111    }
112  }
113  else
114    this->respawnBuffer = NULL;
115}
116
117
118void PowerUp::collidesWith (WorldEntity* entity, const Vector& location)
119{
120  if(entity->isA(CL_EXTENDABLE))
121  {
122    if(dynamic_cast<Extendable*>(entity)->pickup(this))
123    {
124      if(pickupBuffer != NULL)
125        this->soundSource.play(this->pickupBuffer);
126
127      switch(respawnType) {
128        case RESPAWN_NONE:
129          this->toList(OM_DEAD);
130          break;
131        case RESPAWN_TIME:
132          this->toList(OM_DEAD_TICK);
133          this->respawnTime = this->respawnStart;
134          break;
135      }
136    }
137  }
138}
139
140void PowerUp::tick(float dt) {
141  if(this->getOMListNumber() != OM_COMMON) {
142    this->respawnTime -= dt;
143    if(this->respawnTime <= 0)
144    {
145      this->toList(OM_COMMON);
146      if (likely(this->respawnBuffer != NULL))
147        this->soundSource.play(this->respawnBuffer);
148
149    }
150  }
151}
152
153void PowerUp::draw() const
154{
155  if(this->model != NULL) {
156    glMatrixMode(GL_MODELVIEW);
157    glPushMatrix();
158    glTranslatef (this->getAbsCoor ().x,
159                  this->getAbsCoor ().y,
160                  this->getAbsCoor ().z);
161    Vector tmpRot = this->getAbsDir().getSpacialAxis();
162    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
163    this->model->draw();
164    glPopMatrix();
165  }
166  this->sphereMaterial->select();
167  WorldEntity::draw();
168}
169
170const char* PowerUp::respawnTypes[] = {
171  "none",
172  "time"
173};
174
175void PowerUp::setRespawnType(const char* type)
176{
177  for(int i = 0; i < RESPAWN_size; ++i) {
178    if(!strcmp(type, respawnTypes[i])) {
179      this->respawnType = (PowerUpRespawn)i;
180      break;
181    }
182  }
183}
184
185void PowerUp::setRespawnTime(const float respawnTime)
186{
187  this->respawnStart = respawnTime;
188}
189
190
191/********************************************************************************************
192 NETWORK STUFF
193 ********************************************************************************************/
194
195
196/**
197 * data copied in data will bee sent to another host
198 * @param data pointer to data
199 * @param maxLength max length of data
200 * @return the number of bytes writen
201 */
202int PowerUp::readState( byte * data, int maxLength )
203{
204  SYNCHELP_WRITE_BEGIN();
205  SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_PU_WE_STATE );
206  return SYNCHELP_WRITE_N;
207}
208
209
210/**
211 * Writes data from network containing information about the state
212 * @param data pointer to data
213 * @param length length of data
214 * @param sender hostID of sender
215 */
216int PowerUp::writeState( const byte * data, int length, int sender )
217{
218  SYNCHELP_READ_BEGIN();
219  SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_PU_WE_STATE );
220  return SYNCHELP_READ_N;
221}
222
Note: See TracBrowser for help on using the repository browser.