[5838] | 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: |
---|
[5841] | 12 | main-programmer: Silvan Nellen |
---|
| 13 | co-programmer: Benjamin Knecht |
---|
[5838] | 14 | */ |
---|
| 15 | |
---|
[5881] | 16 | |
---|
[5838] | 17 | #include "playable.h" |
---|
[5895] | 18 | |
---|
| 19 | #include "weapons/weapon_manager.h" |
---|
[5875] | 20 | #include "event_handler.h" |
---|
| 21 | #include "player.h" |
---|
[6241] | 22 | #include "state.h" |
---|
[5838] | 23 | |
---|
[6700] | 24 | #include "world_entities/projectiles/projectile.h" |
---|
| 25 | |
---|
[6547] | 26 | #include "power_ups/weapon_power_up.h" |
---|
| 27 | #include "power_ups/param_power_up.h" |
---|
[5872] | 28 | |
---|
[6547] | 29 | |
---|
[5838] | 30 | Playable::Playable() |
---|
| 31 | { |
---|
[6442] | 32 | this->setClassID(CL_PLAYABLE, "Playable"); |
---|
| 33 | PRINTF(4)("PLAYABLE INIT\n"); |
---|
| 34 | |
---|
| 35 | this->toList(OM_GROUP_01); |
---|
| 36 | this->weaponMan = new WeaponManager(this); |
---|
| 37 | |
---|
| 38 | // the reference to the Current Player is NULL, because we dont have one at the beginning. |
---|
| 39 | this->currentPlayer = NULL; |
---|
[6695] | 40 | |
---|
[6804] | 41 | this->bFire = false; |
---|
[6868] | 42 | this->oldFlags = 0; |
---|
[6804] | 43 | |
---|
[6695] | 44 | this->setSynchronized(true); |
---|
[5838] | 45 | } |
---|
| 46 | |
---|
[6695] | 47 | |
---|
| 48 | |
---|
[5875] | 49 | Playable::~Playable() |
---|
[5838] | 50 | { |
---|
[5881] | 51 | delete this->weaponMan; |
---|
[5895] | 52 | |
---|
| 53 | if (this->currentPlayer) |
---|
| 54 | { |
---|
| 55 | PRINTF(2)("There is Still a Player subscribed to this Playable (%s::%s)\n", this->getClassName(), this->getName()); |
---|
| 56 | |
---|
| 57 | } |
---|
[5875] | 58 | } |
---|
| 59 | |
---|
[5898] | 60 | |
---|
[6443] | 61 | void Playable::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
| 62 | { |
---|
[6676] | 63 | this->weaponMan->addWeapon(weapon, configID, slotID); |
---|
[6443] | 64 | |
---|
[6578] | 65 | this->weaponConfigChanged(); |
---|
[6443] | 66 | } |
---|
| 67 | |
---|
[6695] | 68 | |
---|
[6443] | 69 | void Playable::removeWeapon(Weapon* weapon) |
---|
| 70 | { |
---|
| 71 | this->weaponMan->removeWeapon(weapon); |
---|
| 72 | |
---|
[6568] | 73 | this->weaponConfigChanged(); |
---|
[6443] | 74 | } |
---|
| 75 | |
---|
[6695] | 76 | |
---|
[6444] | 77 | void Playable::nextWeaponConfig() |
---|
| 78 | { |
---|
| 79 | this->weaponMan->nextWeaponConfig(); |
---|
[6568] | 80 | this->weaponConfigChanged(); |
---|
[6444] | 81 | } |
---|
[6443] | 82 | |
---|
[6695] | 83 | |
---|
[6444] | 84 | void Playable::previousWeaponConfig() |
---|
| 85 | { |
---|
| 86 | this->weaponMan->previousWeaponConfig(); |
---|
[6568] | 87 | this->weaponConfigChanged(); |
---|
[6444] | 88 | } |
---|
| 89 | |
---|
[6695] | 90 | |
---|
[6568] | 91 | void Playable::weaponConfigChanged() |
---|
| 92 | { |
---|
[6578] | 93 | if (this->currentPlayer != NULL) |
---|
| 94 | this->currentPlayer->weaponConfigChanged(); |
---|
[6568] | 95 | } |
---|
[6444] | 96 | |
---|
[6695] | 97 | |
---|
[5872] | 98 | /** |
---|
[6436] | 99 | * @brief helps us colliding Playables |
---|
| 100 | */ |
---|
| 101 | void Playable::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 102 | { |
---|
| 103 | if (entity->isA(CL_PROJECTILE)) |
---|
[6700] | 104 | this->decreaseHealth(entity->getHealth()); |
---|
[6436] | 105 | |
---|
| 106 | // EXTREME HACK |
---|
[6700] | 107 | if (this->getHealth() == 0.0f) |
---|
[6436] | 108 | this->deactivateNode(); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
[5872] | 112 | * subscribe to all events the controllable needs |
---|
[5898] | 113 | * @param player the player that shall controll this Playable |
---|
[5872] | 114 | */ |
---|
[5895] | 115 | bool Playable::subscribePlayer(Player* player) |
---|
[5872] | 116 | { |
---|
[5895] | 117 | if (this->currentPlayer != NULL) |
---|
[5872] | 118 | { |
---|
[5895] | 119 | PRINTF(1)("Already registered Player:%s to this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
---|
| 120 | return false; |
---|
[5875] | 121 | } |
---|
[5895] | 122 | else |
---|
| 123 | { |
---|
| 124 | this->currentPlayer = player; |
---|
| 125 | /*EventHandler*/ |
---|
| 126 | EventHandler* evh = EventHandler::getInstance(); |
---|
| 127 | std::list<int>::iterator ev; |
---|
| 128 | for (ev = this->events.begin(); ev != events.end(); ev++) |
---|
| 129 | evh->subscribe(player, ES_GAME, (*ev)); |
---|
[6241] | 130 | this->enter(); |
---|
[5895] | 131 | return true; |
---|
| 132 | } |
---|
[5872] | 133 | } |
---|
[5889] | 134 | |
---|
| 135 | /** |
---|
[5898] | 136 | * unsubscribe from all events the controllable needs |
---|
| 137 | * @param player the Player, that controlled this Ship. |
---|
[5896] | 138 | */ |
---|
| 139 | bool Playable::unsubscribePlayer(Player* player) |
---|
| 140 | { |
---|
| 141 | if (this->currentPlayer != player) |
---|
| 142 | { |
---|
| 143 | PRINTF(1)("not the right Player to unregister from this Playable (%s:%s)\n", this->currentPlayer->getName(), this->getClassName(), this->getName()); |
---|
| 144 | return false; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | else |
---|
| 148 | { |
---|
| 149 | /*EventHandler*/ |
---|
| 150 | EventHandler* evh = EventHandler::getInstance(); |
---|
| 151 | std::list<int>::iterator ev; |
---|
| 152 | for (ev = this->events.begin(); ev != events.end(); ev++) |
---|
| 153 | evh->unsubscribe( ES_GAME, (*ev)); |
---|
| 154 | |
---|
[6241] | 155 | this->leave(); |
---|
[5896] | 156 | this->currentPlayer = NULL; |
---|
| 157 | return true; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
[6547] | 161 | bool Playable::pickup(PowerUp* powerUp) |
---|
| 162 | { |
---|
| 163 | if(powerUp->isA(CL_WEAPON_POWER_UP)) { |
---|
[6945] | 164 | return dynamic_cast<WeaponPowerUp*>(powerUp)->process(this->getWeaponManager()); |
---|
[6547] | 165 | } |
---|
| 166 | else if(powerUp->isA(CL_PARAM_POWER_UP)) { |
---|
| 167 | ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp); |
---|
| 168 | switch(ppu->getType()) { |
---|
| 169 | case POWERUP_PARAM_HEALTH: |
---|
[6700] | 170 | this->increaseHealth(ppu->getValue()); |
---|
[6547] | 171 | return true; |
---|
| 172 | case POWERUP_PARAM_MAX_HEALTH: |
---|
[6700] | 173 | this->increaseHealthMax(ppu->getValue()); |
---|
[6547] | 174 | return true; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | return false; |
---|
| 178 | } |
---|
| 179 | |
---|
[5896] | 180 | /** |
---|
[5898] | 181 | * add an event to the event list of events this Playable can capture |
---|
| 182 | * @param eventType the Type of event to add |
---|
[5889] | 183 | */ |
---|
[5896] | 184 | void Playable::registerEvent(int eventType) |
---|
[5889] | 185 | { |
---|
| 186 | this->events.push_back(eventType); |
---|
| 187 | |
---|
[5896] | 188 | if (this->currentPlayer != NULL) |
---|
| 189 | EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); |
---|
[5889] | 190 | } |
---|
| 191 | |
---|
[5896] | 192 | /** |
---|
[5898] | 193 | * remove an event to the event list this Playable can capture. |
---|
| 194 | * @param event the event to unregister. |
---|
[5896] | 195 | */ |
---|
| 196 | void Playable::unregisterEvent(int eventType) |
---|
| 197 | { |
---|
[5902] | 198 | this->events.remove(eventType); |
---|
[5889] | 199 | |
---|
[5896] | 200 | if (this->currentPlayer != NULL) |
---|
| 201 | EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); |
---|
| 202 | } |
---|
[5889] | 203 | |
---|
[6804] | 204 | /** |
---|
| 205 | * @brief ticks a Playable |
---|
| 206 | * @param dt: the passed time since the last Tick |
---|
| 207 | */ |
---|
| 208 | void Playable::tick(float dt) |
---|
| 209 | { |
---|
| 210 | this->weaponMan->tick(dt); |
---|
| 211 | if (this->bFire) |
---|
| 212 | weaponMan->fire(); |
---|
| 213 | } |
---|
[5896] | 214 | |
---|
[6804] | 215 | |
---|
| 216 | /** |
---|
| 217 | * @brief processes Playable events. |
---|
| 218 | * @param event the Captured Event. |
---|
| 219 | */ |
---|
| 220 | void Playable::process(const Event &event) |
---|
| 221 | { |
---|
| 222 | if( event.type == KeyMapper::PEV_FIRE1) |
---|
| 223 | this->bFire = event.bPressed; |
---|
| 224 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
| 225 | { |
---|
| 226 | this->nextWeaponConfig(); |
---|
| 227 | } |
---|
| 228 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
| 229 | this->previousWeaponConfig(); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | |
---|
[6241] | 234 | void Playable::attachCamera() |
---|
| 235 | { |
---|
[6444] | 236 | State::getCamera()->setParentSoft(this); |
---|
| 237 | State::getCameraTarget()->setParentSoft(this); |
---|
[6241] | 238 | |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | |
---|
[6804] | 242 | |
---|
| 243 | |
---|
[6241] | 244 | void Playable::detachCamera() |
---|
| 245 | { |
---|
| 246 | } |
---|
[6868] | 247 | |
---|
| 248 | #define FLAGS_bFire 1 |
---|
| 249 | |
---|
| 250 | int Playable::writeSync( const byte * data, int length, int sender ) |
---|
| 251 | { |
---|
| 252 | SYNCHELP_READ_BEGIN(); |
---|
[6871] | 253 | |
---|
[6868] | 254 | byte flags; |
---|
[6871] | 255 | |
---|
[6868] | 256 | SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS ); |
---|
[6871] | 257 | |
---|
[6868] | 258 | bFire = (flags & FLAGS_bFire) != 0; |
---|
[6871] | 259 | |
---|
[6868] | 260 | return SYNCHELP_READ_N; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | int Playable::readSync( byte * data, int maxLength ) |
---|
| 264 | { |
---|
| 265 | SYNCHELP_WRITE_BEGIN(); |
---|
| 266 | byte flags = 0; |
---|
[6871] | 267 | |
---|
[6868] | 268 | if ( bFire ) |
---|
| 269 | flags |= FLAGS_bFire; |
---|
| 270 | |
---|
[6871] | 271 | |
---|
[6868] | 272 | SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS ); |
---|
| 273 | oldFlags = flags; |
---|
| 274 | |
---|
[6871] | 275 | |
---|
[6868] | 276 | return SYNCHELP_WRITE_N; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | bool Playable::needsReadSync( ) |
---|
| 280 | { |
---|
| 281 | byte flags = 0; |
---|
[6871] | 282 | |
---|
[6868] | 283 | if ( bFire ) |
---|
| 284 | flags |= FLAGS_bFire; |
---|
[6871] | 285 | |
---|
[6868] | 286 | return flags!=oldFlags; |
---|
| 287 | } |
---|