| 1 |
#include "PlayerOnServer.h" |
| 2 |
|
| 3 |
#if defined(WITH_MULTIPLAYER) |
| 4 |
|
| 5 |
#include "../Weapons/ShotBase.h" |
| 6 |
#include "../Weapons/FreezerShot.h" |
| 7 |
#include "../../Multiplayer/MpLevelHandler.h" |
| 8 |
#include "../../../nCine/Base/FrameTimer.h" |
| 9 |
|
| 10 |
namespace Jazz2::Actors::Multiplayer |
| 11 |
{ |
| 12 |
PlayerOnServer::PlayerOnServer() |
| 13 |
: _lastAttackerTimeout(0.0f), _canTakeDamage(true), _justWarped(false) |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
void PlayerOnServer::OnUpdate(float timeMult) |
| 18 |
{ |
| 19 |
MpPlayer::OnUpdate(timeMult); |
| 20 |
|
| 21 |
if (_lastAttackerTimeout > 0.0f) { |
| 22 |
_lastAttackerTimeout -= timeMult; |
| 23 |
if (_lastAttackerTimeout <= 0.0f) { |
| 24 |
_lastAttacker = nullptr; |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
bool PlayerOnServer::OnHandleCollision(std::shared_ptr<ActorBase> other) |
| 30 |
{ |
| 31 |
// TODO: Check player special move here |
| 32 |
if (auto* weaponOwner = MpLevelHandler::GetWeaponOwner(other.get())) { |
| 33 |
auto* otherPlayerOnServer = static_cast<PlayerOnServer*>(weaponOwner); |
| 34 |
if (_health > 0 && GetPeerDescriptor()->Team != otherPlayerOnServer->GetPeerDescriptor()->Team) { |
| 35 |
bool otherIsPlayer = false; |
| 36 |
if (auto* anotherPlayer = runtime_cast<PlayerOnServer>(other.get())) { |
| 37 |
bool isAttacking = IsAttacking(); |
| 38 |
if (!isAttacking && !anotherPlayer->IsAttacking()) { |
| 39 |
return true; |
| 40 |
} |
| 41 |
if (isAttacking) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
otherIsPlayer = true; |
| 45 |
} |
| 46 |
|
| 47 |
_lastAttacker = otherPlayerOnServer->shared_from_this(); |
| 48 |
_lastAttackerTimeout = 300.0f; |
| 49 |
|
| 50 |
// Decrease remaining shield time by 5 secs |
| 51 |
if (_activeShieldTime > (5.0f * FrameTimer::FramesPerSecond)) { |
| 52 |
_activeShieldTime -= (5.0f * FrameTimer::FramesPerSecond); |
| 53 |
} else if (auto* freezerShot = runtime_cast<Weapons::FreezerShot>(other.get())) { |
| 54 |
Freeze(3.0f * FrameTimer::FramesPerSecond); |
| 55 |
} else { |
| 56 |
TakeDamage(1, 4.0f * (_pos.X > other->GetPos().X ? 1.0f : -1.0f)); |
| 57 |
} |
| 58 |
if (!otherIsPlayer) { |
| 59 |
other->DecreaseHealth(INT32_MAX); |
| 60 |
} |
| 61 |
return true; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return Player::OnHandleCollision(std::move(other)); |
| 66 |
} |
| 67 |
|
| 68 |
bool PlayerOnServer::CanCauseDamage(ActorBase* collider) |
| 69 |
{ |
| 70 |
if (_canTakeDamage && _health > 0) { |
| 71 |
if (auto* weaponOwner = MpLevelHandler::GetWeaponOwner(collider)) { |
| 72 |
return (static_cast<PlayerOnServer*>(weaponOwner)->GetPeerDescriptor()->Team != GetPeerDescriptor()->Team); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
bool PlayerOnServer::TakeDamage(std::int32_t amount, float pushForce, bool ignoreInvulnerable) |
| 80 |
{ |
| 81 |
if (!_canTakeDamage || !MpPlayer::TakeDamage(amount, pushForce, ignoreInvulnerable)) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
static_cast<Jazz2::Multiplayer::MpLevelHandler*>(_levelHandler)->HandlePlayerTakeDamage(this, amount, pushForce); |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
bool PlayerOnServer::AddLives(std::int32_t count) |
| 91 |
{ |
| 92 |
// TODO: OneUpCollectible are currently blocked in multiplayer |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
bool PlayerOnServer::MorphTo(PlayerType type) |
| 97 |
{ |
| 98 |
if (!MpPlayer::MorphTo(type)) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
static_cast<Jazz2::Multiplayer::MpLevelHandler*>(_levelHandler)->HandlePlayerMorphTo(this, type); |
| 103 |
|
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
bool PlayerOnServer::IsAttacking() const |
| 108 |
{ |
| 109 |
if (_currentSpecialMove == SpecialMoveType::Buttstomp && _currentTransition != nullptr && _sugarRushLeft <= 0.0f) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
return (_currentSpecialMove != SpecialMoveType::None || _sugarRushLeft > 0.0f); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#endif |