| [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)) { |
|---|
| 164 | Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon(); |
|---|
| 165 | WeaponManager* manager = this->getWeaponManager(); |
|---|
| [6710] | 166 | return manager->addWeapon(weapon); |
|---|
| [6547] | 167 | } |
|---|
| 168 | else if(powerUp->isA(CL_PARAM_POWER_UP)) { |
|---|
| 169 | ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp); |
|---|
| 170 | switch(ppu->getType()) { |
|---|
| 171 | case POWERUP_PARAM_HEALTH: |
|---|
| [6700] | 172 | this->increaseHealth(ppu->getValue()); |
|---|
| [6547] | 173 | return true; |
|---|
| 174 | case POWERUP_PARAM_MAX_HEALTH: |
|---|
| [6700] | 175 | this->increaseHealthMax(ppu->getValue()); |
|---|
| [6547] | 176 | return true; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | return false; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| [5896] | 182 | /** |
|---|
| [5898] | 183 | * add an event to the event list of events this Playable can capture |
|---|
| 184 | * @param eventType the Type of event to add |
|---|
| [5889] | 185 | */ |
|---|
| [5896] | 186 | void Playable::registerEvent(int eventType) |
|---|
| [5889] | 187 | { |
|---|
| 188 | this->events.push_back(eventType); |
|---|
| 189 | |
|---|
| [5896] | 190 | if (this->currentPlayer != NULL) |
|---|
| 191 | EventHandler::getInstance()->subscribe(this->currentPlayer, ES_GAME, eventType); |
|---|
| [5889] | 192 | } |
|---|
| 193 | |
|---|
| [5896] | 194 | /** |
|---|
| [5898] | 195 | * remove an event to the event list this Playable can capture. |
|---|
| 196 | * @param event the event to unregister. |
|---|
| [5896] | 197 | */ |
|---|
| 198 | void Playable::unregisterEvent(int eventType) |
|---|
| 199 | { |
|---|
| [5902] | 200 | this->events.remove(eventType); |
|---|
| [5889] | 201 | |
|---|
| [5896] | 202 | if (this->currentPlayer != NULL) |
|---|
| 203 | EventHandler::getInstance()->unsubscribe(ES_GAME, eventType); |
|---|
| 204 | } |
|---|
| [5889] | 205 | |
|---|
| [6804] | 206 | /** |
|---|
| 207 | * @brief ticks a Playable |
|---|
| 208 | * @param dt: the passed time since the last Tick |
|---|
| 209 | */ |
|---|
| 210 | void Playable::tick(float dt) |
|---|
| 211 | { |
|---|
| 212 | this->weaponMan->tick(dt); |
|---|
| 213 | if (this->bFire) |
|---|
| 214 | weaponMan->fire(); |
|---|
| 215 | } |
|---|
| [5896] | 216 | |
|---|
| [6804] | 217 | |
|---|
| 218 | /** |
|---|
| 219 | * @brief processes Playable events. |
|---|
| 220 | * @param event the Captured Event. |
|---|
| 221 | */ |
|---|
| 222 | void Playable::process(const Event &event) |
|---|
| 223 | { |
|---|
| 224 | if( event.type == KeyMapper::PEV_FIRE1) |
|---|
| 225 | this->bFire = event.bPressed; |
|---|
| 226 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
|---|
| 227 | { |
|---|
| 228 | this->nextWeaponConfig(); |
|---|
| 229 | } |
|---|
| 230 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
|---|
| 231 | this->previousWeaponConfig(); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| [6241] | 236 | void Playable::attachCamera() |
|---|
| 237 | { |
|---|
| [6444] | 238 | State::getCamera()->setParentSoft(this); |
|---|
| 239 | State::getCameraTarget()->setParentSoft(this); |
|---|
| [6241] | 240 | |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| [6804] | 244 | |
|---|
| 245 | |
|---|
| [6241] | 246 | void Playable::detachCamera() |
|---|
| 247 | { |
|---|
| 248 | } |
|---|
| [6868] | 249 | |
|---|
| 250 | #define FLAGS_bFire 1 |
|---|
| 251 | |
|---|
| 252 | int Playable::writeSync( const byte * data, int length, int sender ) |
|---|
| 253 | { |
|---|
| 254 | SYNCHELP_READ_BEGIN(); |
|---|
| [6871] | 255 | |
|---|
| [6868] | 256 | byte flags; |
|---|
| [6871] | 257 | |
|---|
| [6868] | 258 | SYNCHELP_READ_BYTE( flags, NWT_PL_FLAGS ); |
|---|
| [6871] | 259 | |
|---|
| [6868] | 260 | bFire = (flags & FLAGS_bFire) != 0; |
|---|
| [6871] | 261 | |
|---|
| [6868] | 262 | return SYNCHELP_READ_N; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | int Playable::readSync( byte * data, int maxLength ) |
|---|
| 266 | { |
|---|
| 267 | SYNCHELP_WRITE_BEGIN(); |
|---|
| 268 | byte flags = 0; |
|---|
| [6871] | 269 | |
|---|
| [6868] | 270 | if ( bFire ) |
|---|
| 271 | flags |= FLAGS_bFire; |
|---|
| 272 | |
|---|
| [6871] | 273 | |
|---|
| [6868] | 274 | SYNCHELP_WRITE_BYTE( flags, NWT_PL_FLAGS ); |
|---|
| 275 | oldFlags = flags; |
|---|
| 276 | |
|---|
| [6871] | 277 | |
|---|
| [6868] | 278 | return SYNCHELP_WRITE_N; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | bool Playable::needsReadSync( ) |
|---|
| 282 | { |
|---|
| 283 | byte flags = 0; |
|---|
| [6871] | 284 | |
|---|
| [6868] | 285 | if ( bFire ) |
|---|
| 286 | flags |= FLAGS_bFire; |
|---|
| [6871] | 287 | |
|---|
| [6868] | 288 | return flags!=oldFlags; |
|---|
| 289 | } |
|---|