/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Manuel Leuenberger co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY #include "power_up.h" #include "extendable.h" #include "primitive_model.h" #include "util/loading/resource_manager.h" #include "util/loading/load_param.h" using namespace std; PowerUp::PowerUp(float r, float g, float b) { this->setClassID(CL_POWER_UP, "PowerUp"); this->respawnType = RESPAWN_TIME; this->respawnStart = 10; this->model = NULL; /* if(!PowerUp::sphereModel) {*/ Model* sphereModel = new PrimitiveModel(PRIM_SPHERE, 7, 5); this->setModel(sphereModel); this->buildObbTree( 4); this->sphereMaterial = new Material; this->sphereMaterial->setTransparency(.8); this->sphereMaterial->setDiffuse(r, g, b); this->toList(OM_COMMON); this->soundSource.setSourceNode(this); this->pickupBuffer = NULL; this->respawnBuffer = NULL; this->collider = NULL; } PowerUp::~PowerUp () { delete this->sphereMaterial; if (this->pickupBuffer != NULL) ResourceManager::getInstance()->unload(this->pickupBuffer); if (this->respawnBuffer != NULL) ResourceManager::getInstance()->unload(this->respawnBuffer); } void PowerUp::loadParams(const TiXmlElement* root) { WorldEntity::loadParams(root); LoadParam(root, "respawnType", this, PowerUp, setRespawnType); LoadParam(root, "respawnTime", this, PowerUp, setRespawnTime); LoadParam(root, "pickup-sound", this, PowerUp, loadPickupSound); LoadParam(root, "respawn-sound", this, PowerUp, loadRespawnSound); } void PowerUp::loadPickupSound(const std::string& pickupSound) { if (this->pickupBuffer != NULL) ResourceManager::getInstance()->unload(this->pickupBuffer); else if (!pickupSound.empty()) { this->pickupBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(pickupSound, WAV); if (this->pickupBuffer != NULL) { PRINTF(4)("Loaded sound %s to Pickup: %s.\n", pickupSound.c_str(), this->getName()); } else { PRINTF(2)("Failed to load sound %s to pickup %s.\n.", pickupSound.c_str(), this->getName()); } } else this->pickupBuffer = NULL; } void PowerUp::loadRespawnSound(const std::string& respawnSound) { if (this->respawnBuffer != NULL) ResourceManager::getInstance()->unload(this->respawnBuffer); else if (!respawnSound.empty()) { this->respawnBuffer = (SoundBuffer*)ResourceManager::getInstance()->load(respawnSound, WAV); if (this->respawnBuffer != NULL) { PRINTF(4)("Loaded sound %s to Pickup: %s.\n", respawnSound.c_str(), this->getName()); } else { PRINTF(2)("Failed to load sound %s to respawn %s.\n.", respawnSound.c_str(), this->getName()); } } else this->respawnBuffer = NULL; } void PowerUp::collidesWith (WorldEntity* entity, const Vector& location) { if(this->collider != entity && entity->isA(CL_EXTENDABLE)) { this->collider = entity; if(dynamic_cast(entity)->pickup(this)) { if(pickupBuffer != NULL) this->soundSource.play(this->pickupBuffer); switch(respawnType) { case RESPAWN_NONE: this->toList(OM_DEAD); break; case RESPAWN_TIME: this->toList(OM_DEAD_TICK); this->respawnTime = this->respawnStart; break; } } } } void PowerUp::tick(float dt) { if(this->getOMListNumber() != OM_COMMON) { this->respawnTime -= dt; if(this->respawnTime <= 0) { this->toList(OM_COMMON); this->collider = NULL; if (likely(this->respawnBuffer != NULL)) this->soundSource.play(this->respawnBuffer); } } } void PowerUp::draw() const { if(this->model != NULL) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); Vector tmpRot = this->getAbsDir().getSpacialAxis(); glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); this->model->draw(); glPopMatrix(); } this->sphereMaterial->select(); WorldEntity::draw(); } const char* PowerUp::respawnTypes[] = { "none", "time" }; void PowerUp::setRespawnType(const std::string& type) { for(int i = 0; i < RESPAWN_size; ++i) { if(type == respawnTypes[i]) { this->respawnType = (PowerUpRespawn)i; break; } } } void PowerUp::setRespawnTime(const float respawnTime) { this->respawnStart = respawnTime; } /******************************************************************************************** NETWORK STUFF ********************************************************************************************/ /** * data copied in data will bee sent to another host * @param data pointer to data * @param maxLength max length of data * @return the number of bytes writen */ int PowerUp::readState( byte * data, int maxLength ) { SYNCHELP_WRITE_BEGIN(); SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_PU_WE_STATE ); return SYNCHELP_WRITE_N; } /** * Writes data from network containing information about the state * @param data pointer to data * @param length length of data * @param sender hostID of sender */ int PowerUp::writeState( const byte * data, int length, int sender ) { SYNCHELP_READ_BEGIN(); SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_PU_WE_STATE ); return SYNCHELP_READ_N; }