/* 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: Silvan Nellen co-programmer: Benjamin Knecht */ #include "playable.h" #include "weapons/weapon_manager.h" #include "event_handler.h" #include "player.h" #include "state.h" #include "world_entities/projectiles/projectile.h" #include "power_ups/weapon_power_up.h" #include "power_ups/param_power_up.h" #include "dot_emitter.h" #include "sprite_particles.h" Playable::Playable() { this->setClassID(CL_PLAYABLE, "Playable"); PRINTF(4)("PLAYABLE INIT\n"); this->toList(OM_GROUP_01); this->weaponMan = new WeaponManager(this); // the reference to the Current Player is NULL, because we dont have one at the beginning. this->currentPlayer = NULL; this->bFire = false; this->oldFlags = 0; this->setSynchronized(true); this->score = 0; this->oldScore = 0; this->emitter = new DotEmitter(100, 5, M_2_PI); this->emitter->setParent(this); this->emitter->setSpread(M_PI, M_PI); this->emitter->setEmissionRate(300.0); this->emitter->setEmissionVelocity(50.0); this->explosionParticles = new SpriteParticles(1000); this->explosionParticles->setName("LaserExplosionParticles"); this->explosionParticles->setLifeSpan(.5, .3); this->explosionParticles->setRadius(0.0, 10.0); this->explosionParticles->setRadius(.5, 6.0); this->explosionParticles->setRadius(1.0, 3.0); this->explosionParticles->setColor(0.0, 1,1,0,.9); this->explosionParticles->setColor(0.5, .8,.8,0,.5); this->explosionParticles->setColor(1.0, .8,.8,.7,.0); } Playable::~Playable() { delete this->weaponMan; if (this->currentPlayer) { PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName()); } } void Playable::addWeapon(Weapon* weapon, int configID, int slotID) { this->weaponMan->addWeapon(weapon, configID, slotID); this->weaponConfigChanged(); } void Playable::removeWeapon(Weapon* weapon) { this->weaponMan->removeWeapon(weapon); this->weaponConfigChanged(); } void Playable::nextWeaponConfig() { this->weaponMan->nextWeaponConfig(); this->weaponConfigChanged(); } void Playable::previousWeaponConfig() { this->weaponMan->previousWeaponConfig(); this->weaponConfigChanged(); } void Playable::weaponConfigChanged() { if (this->currentPlayer != NULL) this->currentPlayer->weaponConfigChanged(); } /** * @brief helps us colliding Playables */ void Playable::collidesWith(WorldEntity* entity, const Vector& location) { if (entity->isA(CL_PROJECTILE) && !State::isOnline() ) this->decreaseHealth(entity->getHealth()); // EXTREME HACK if (this->getHealth() == 0.0f) { //this->deactivateNode(); this->emitter->setSystem(explosionParticles); this->setAbsCoor(0, 0, 0); //this->setAbsDir(Vector(1,0,0), 0); this->emitter->setSystem(NULL); } } /** * subscribe to all events the controllable needs * @param player the player that shall controll this Playable */ bool Playable::subscribePlayer(Player* player) { if (this->currentPlayer != NULL) { PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); return false; } else { this->currentPlayer = player; /*EventHandler*/ EventHandler* evh = EventHandler::getInstance(); std::list::iterator ev; for (ev = this->events.begin(); ev != events.end(); ev++) evh->subscribe(player, ES_GAME, (*ev)); this->enter(); return true; } } /** * unsubscribe from all events the controllable needs * @param player the Player, that controlled this Ship. */ bool Playable::unsubscribePlayer(Player* player) { if (this->currentPlayer != player) { PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); return false; } else { /*EventHandler*/ EventHandler* evh = EventHandler::getInstance(); std::list::iterator ev; for (ev = this->events.begin(); ev != events.end(); ev++) evh->unsubscribe( ES_GAME, (*ev)); this->leave(); this->currentPlayer = NULL; return true; } } bool Playable::pickup(PowerUp* powerUp) { if(powerUp->isA(CL_WEAPON_POWER_UP)) { Weapon* weapon = dynamic_cast(powerUp)->getWeapon(); WeaponManager* manager = this->getWeaponManager(); return manager->addWeapon(weapon); } else if(powerUp->isA(CL_PARAM_POWER_UP)) { ParamPowerUp* ppu = dynamic_cast(powerUp); switch(ppu->getType()) { case POWERUP_PARAM_HEALTH: this->increaseHealth(ppu->getValue()); return true; case POWERUP_PARAM_MAX_HEALTH: this->increaseHealthMax(ppu->getValue()); return true; } } return false; } /** * add an event to the event list of events this Playable can capture * @param eventType the Type of event to add */ void Playable::registerEvent(int eventType) { this->events.push_back(eventType); if (this->currentPlayer != NULL) EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); } /** * remove an event to the event list this Playable can capture. * @param event the event to unregister. */ void Playable::unregisterEvent(int eventType) { this->events.remove(eventType); if (this->currentPlayer != NULL) EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); } /** * @brief ticks a Playable * @param dt: the passed time since the last Tick */ void Playable::tick(float dt) { this->weaponMan->tick(dt); if (this->bFire) weaponMan->fire(); } /** * @brief processes Playable events. * @param event the Captured Event. */ void Playable::process(const Event &event) { if( event.type == KeyMapper::PEV_FIRE1) this->bFire = event.bPressed; else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) { this->nextWeaponConfig(); } else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) this->previousWeaponConfig(); } void Playable::attachCamera() { State::getCamera()->setParentSoft(this); State::getCameraTarget()->setParentSoft(this); } void Playable::detachCamera() { } #define DATA_FLAGS 1 #define DATA_SCORE 2 #define FLAGS_bFire 1 int Playable::writeSync( const byte * data, int length, int sender ) { SYNCHELP_READ_BEGIN(); byte b; SYNCHELP_READ_BYTE( b, NWT_PL_B ); byte flags; if ( b == DATA_FLAGS ) { SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS ); bFire = (flags & FLAGS_bFire) != 0; return SYNCHELP_READ_N; } if ( b == DATA_SCORE ) { int newScore; SYNCHELP_READ_BYTE( newScore, NWT_PL_SCORE ); setScore( newScore ); return SYNCHELP_READ_N; } return SYNCHELP_READ_N; } int Playable::readSync( byte * data, int maxLength ) { SYNCHELP_WRITE_BEGIN(); if ( score != oldScore && isServer() ) { SYNCHELP_WRITE_BYTE( DATA_SCORE, NWT_PL_B); SYNCHELP_WRITE_INT( score, NWT_PL_SCORE ); oldScore = score; return SYNCHELP_WRITE_N; } byte flags = 0; if ( bFire ) flags |= FLAGS_bFire; SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS ); oldFlags = flags; return SYNCHELP_WRITE_N; } bool Playable::needsReadSync( ) { if ( score != oldScore && isServer() ) return true; byte flags = 0; if ( bFire ) flags |= FLAGS_bFire; return flags!=oldFlags; }