Skip to content

Commit

Permalink
Multiplayer (part 5), debug overlay using ImGui
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Nov 17, 2023
1 parent df175ee commit 3fd6954
Show file tree
Hide file tree
Showing 13 changed files with 553 additions and 280 deletions.
21 changes: 4 additions & 17 deletions Sources/Jazz2/Actors/ActorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,10 @@ using namespace nCine;
namespace Jazz2::Actors
{
ActorBase::ActorBase()
:
_state(ActorState::None),
_levelHandler(nullptr),
_internalForceY(0.0f),
_elasticity(0.0f),
_friction(1.5f),
_unstuckCooldown(0.0f),
_frozenTimeLeft(0.0f),
_maxHealth(1),
_health(1),
_spawnFrames(0.0f),
_metadata(nullptr),
_renderer(this),
_currentAnimation(nullptr),
_currentTransition(nullptr),
_currentTransitionCancellable(false),
CollisionProxyID(Collisions::NullNode)
: _state(ActorState::None), _levelHandler(nullptr), _internalForceY(0.0f), _elasticity(0.0f), _friction(1.5f),
_unstuckCooldown(0.0f), _frozenTimeLeft(0.0f), _maxHealth(1), _health(1), _spawnFrames(0.0f), _metadata(nullptr),
_renderer(this), _currentAnimation(nullptr), _currentTransition(nullptr), _currentTransitionCancellable(false),
CollisionProxyID(Collisions::NullNode)
{
}

Expand Down
141 changes: 75 additions & 66 deletions Sources/Jazz2/Actors/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,69 +1298,10 @@ namespace Jazz2::Actors
TakeDamage(1, 4 * (_pos.X > enemy->GetPos().X ? 1.0f : -1.0f));
}
} else if (auto spring = dynamic_cast<Environment::Spring*>(other.get())) {
// Collide only with hitbox
// Collide only with hitbox here
if (_controllableExternal && (_currentTransition == nullptr || _currentTransition->State != AnimState::TransitionLedgeClimb) && _springCooldown <= 0.0f && spring->AABBInner.Overlaps(AABBInner)) {
Vector2 force = spring->Activate();
int sign = ((force.X + force.Y) > std::numeric_limits<float>::epsilon() ? 1 : -1);
if (std::abs(force.X) > 0.0f) {
removeSpecialMove = true;
_copterFramesLeft = 0.0f;
//speedX = force.X;
_speed.X = (1.0f + std::abs(force.X)) * sign;
_externalForce.X = force.X * 0.6f;
_springCooldown = 10.0f;
SetState(ActorState::CanJump, false);

_wasActivelyPushing = false;
_keepRunningTime = 100.0f;

if (!spring->KeepSpeedY) {
_speed.Y = 0.0f;
_externalForce.Y = 0.0f;
}

if (_inIdleTransition) {
_inIdleTransition = false;
CancelTransition();
}

SetPlayerTransition(AnimState::Dash | AnimState::Jump, true, false, SpecialMoveType::None);
_controllableTimeout = 2.0f;
} else if (std::abs(force.Y) > 0.0f) {
_copterFramesLeft = 0.0f;
_speed.Y = (4.0f + std::abs(force.Y)) * sign;
if (!GetState(ActorState::ApplyGravitation)) {
_externalForce.Y = force.Y * 0.14f;
} else if (_levelHandler->IsReforged()) {
_externalForce.Y = force.Y;
} else {
_externalForce.Y = force.Y * 0.8f;
}
_springCooldown = 10.0f;
SetState(ActorState::CanJump, false);

if (!spring->KeepSpeedX) {
_speed.X = 0.0f;
_externalForce.X = 0.0f;
_keepRunningTime = 0.0f;
}

if (_inIdleTransition) {
_inIdleTransition = false;
CancelTransition();
}

if (sign > 0) {
removeSpecialMove = false;
_currentSpecialMove = SpecialMoveType::Buttstomp;
SetAnimation(AnimState::Buttstomp);
} else {
removeSpecialMove = true;
_isSpring = true;
}

PlaySfx("Spring"_s);
}
Vector2f force = spring->Activate();
OnHitSpring(force, spring->KeepSpeedX, spring->KeepSpeedY, removeSpecialMove);
}

handled = true;
Expand Down Expand Up @@ -1393,6 +1334,74 @@ namespace Jazz2::Actors
return handled;
}

void Player::OnHitSpring(const Vector2f& force, bool keepSpeedX, bool keepSpeedY, bool& removeSpecialMove)
{
if (!_levelHandler->HandlePlayerSpring(this, force, keepSpeedX, keepSpeedY)) {
return;
}

std::int32_t sign = ((force.X + force.Y) > std::numeric_limits<float>::epsilon() ? 1 : -1);
if (std::abs(force.X) > 0.0f) {
removeSpecialMove = true;
_copterFramesLeft = 0.0f;
//speedX = force.X;
_speed.X = (1.0f + std::abs(force.X)) * sign;
_externalForce.X = force.X * 0.6f;
_springCooldown = 10.0f;
SetState(ActorState::CanJump, false);

_wasActivelyPushing = false;
_keepRunningTime = 100.0f;

if (!keepSpeedY) {
_speed.Y = 0.0f;
_externalForce.Y = 0.0f;
}

if (_inIdleTransition) {
_inIdleTransition = false;
CancelTransition();
}

SetPlayerTransition(AnimState::Dash | AnimState::Jump, true, false, SpecialMoveType::None);
_controllableTimeout = 2.0f;
} else if (std::abs(force.Y) > 0.0f) {
_copterFramesLeft = 0.0f;
_speed.Y = (4.0f + std::abs(force.Y)) * sign;
if (!GetState(ActorState::ApplyGravitation)) {
_externalForce.Y = force.Y * 0.14f;
} else if (_levelHandler->IsReforged()) {
_externalForce.Y = force.Y;
} else {
_externalForce.Y = force.Y * 0.8f;
}
_springCooldown = 10.0f;
SetState(ActorState::CanJump, false);

if (!keepSpeedX) {
_speed.X = 0.0f;
_externalForce.X = 0.0f;
_keepRunningTime = 0.0f;
}

if (_inIdleTransition) {
_inIdleTransition = false;
CancelTransition();
}

if (sign > 0) {
removeSpecialMove = false;
_currentSpecialMove = SpecialMoveType::Buttstomp;
SetAnimation(AnimState::Buttstomp);
} else {
removeSpecialMove = true;
_isSpring = true;
}

PlaySfx("Spring"_s);
}
}

void Player::OnHitFloor(float timeMult)
{
if (_levelHandler->TileMap()->IsTileHurting(_pos.X, _pos.Y + 24)) {
Expand Down Expand Up @@ -2324,7 +2333,7 @@ namespace Jazz2::Actors

SetAnimation(AnimState::Idle);

if (_levelHandler->HandlePlayerDied(std::static_pointer_cast<Player>(shared_from_this()))) {
if (_levelHandler->HandlePlayerDied(this)) {
// Reset health
_health = _maxHealth;

Expand Down Expand Up @@ -2564,7 +2573,7 @@ namespace Jazz2::Actors

uint16_t ammoDecrease = 256;

if (!_levelHandler->HandlePlayerFireWeapon(std::static_pointer_cast<Player>(shared_from_this()), weaponType, ammoDecrease)) {
if (!_levelHandler->HandlePlayerFireWeapon(this, weaponType, ammoDecrease)) {
return false;
}

Expand Down Expand Up @@ -2915,7 +2924,7 @@ namespace Jazz2::Actors
if (hideTrail) {
_trailLastPos = _pos;
}
_levelHandler->HandlePlayerWarped(std::static_pointer_cast<Player>(shared_from_this()), posPrev, true);
_levelHandler->HandlePlayerWarped(this, posPrev, true);
} else {
EndDamagingMove();
SetState(ActorState::IsInvulnerable, true);
Expand Down Expand Up @@ -2943,7 +2952,7 @@ namespace Jazz2::Actors
_trailLastPos = _pos;
PlayPlayerSfx("WarpOut"_s);

_levelHandler->HandlePlayerWarped(std::static_pointer_cast<Player>(shared_from_this()), posPrev, false);
_levelHandler->HandlePlayerWarped(this, posPrev, false);

_isFreefall |= CanFreefall();
SetPlayerTransition(_isFreefall ? AnimState::TransitionWarpOutFreefall : AnimState::TransitionWarpOut, false, true, SpecialMoveType::None, [this]() {
Expand Down
1 change: 1 addition & 0 deletions Sources/Jazz2/Actors/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ namespace Jazz2::Actors
void OnEmitLights(SmallVectorImpl<LightEmitter>& lights) override;

bool OnHandleCollision(std::shared_ptr<ActorBase> other) override;
void OnHitSpring(const Vector2f& force, bool keepSpeedX, bool keepSpeedY, bool& removeSpecialMove);
void OnHitFloor(float timeMult) override;
void OnHitCeiling(float timeMult) override;
void OnHitWall(float timeMult) override;
Expand Down
11 changes: 8 additions & 3 deletions Sources/Jazz2/Actors/RemoteActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace Jazz2::Actors

Task<bool> RemoteActor::OnActivatedAsync(const ActorActivationDetails& details)
{
SetState(ActorState::ForceDisableCollisions | ActorState::PreserveOnRollback | ActorState::ExcludeSimilar, true);
SetState(ActorState::CanBeFrozen | ActorState::CollideWithTileset | ActorState::CollideWithOtherActors | ActorState::ApplyGravitation, false);
SetState(ActorState::PreserveOnRollback, true);
SetState(ActorState::CanBeFrozen | ActorState::CollideWithTileset | ActorState::ApplyGravitation, false);

Clock& c = nCine::clock();
std::uint64_t now = c.now() * 1000 / c.frequency();
Expand Down Expand Up @@ -67,10 +67,15 @@ namespace Jazz2::Actors
ActorBase::OnUpdate(timeMult);
}

void RemoteActor::AssignMetadata(const StringView& path, AnimState anim)
void RemoteActor::AssignMetadata(const StringView& path, AnimState anim, ActorState state)
{
constexpr ActorState RemotedFlags = ActorState::Illuminated | ActorState::IsInvulnerable | ActorState::TriggersTNT |
ActorState::CollideWithOtherActors | ActorState::CollideWithSolidObjects | ActorState::IsSolidObject |
ActorState::CollideWithTilesetReduced | ActorState::CollideWithSolidObjectsBelow | ActorState::ExcludeSimilar;

RequestMetadata(path);
SetAnimation(anim);
SetState((GetState() & ~RemotedFlags) | (state & RemotedFlags));
}

void RemoteActor::SyncWithServer(const Vector2f& pos, AnimState anim, bool isVisible, bool isFacingLeft)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/Actors/RemoteActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Jazz2::Actors
public:
RemoteActor();

void AssignMetadata(const StringView& path, AnimState anim);
void AssignMetadata(const StringView& path, AnimState anim, ActorState state);
void SyncWithServer(const Vector2f& pos, AnimState anim, bool isVisible, bool isFacingLeft);

protected:
Expand Down
9 changes: 5 additions & 4 deletions Sources/Jazz2/ILevelHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Jazz2

virtual std::shared_ptr<AudioBufferPlayer> PlaySfx(Actors::ActorBase* self, const StringView& identifier, AudioBuffer* buffer, const Vector3f& pos, bool sourceRelative, float gain = 1.0f, float pitch = 1.0f) = 0;
virtual std::shared_ptr<AudioBufferPlayer> PlayCommonSfx(const StringView& identifier, const Vector3f& pos, float gain = 1.0f, float pitch = 1.0f) = 0;
virtual void WarpCameraToTarget(const std::shared_ptr<Actors::ActorBase>& actor, bool fast = false) = 0;
virtual void WarpCameraToTarget(Actors::ActorBase* actor, bool fast = false) = 0;
virtual bool IsPositionEmpty(Actors::ActorBase* self, const AABBf& aabb, Tiles::TileCollisionParams& params, Actors::ActorBase** collider) = 0;

bool IsPositionEmpty(Actors::ActorBase* self, const AABBf& aabb, Tiles::TileCollisionParams& params)
Expand All @@ -73,9 +73,10 @@ namespace Jazz2
virtual void BroadcastTriggeredEvent(Actors::ActorBase* initiator, EventType eventType, uint8_t* eventParams) = 0;
virtual void BeginLevelChange(ExitType exitType, const StringView& nextLevel) = 0;
virtual void HandleGameOver() = 0;
virtual bool HandlePlayerDied(std::shared_ptr<Actors::Player> player) = 0;
virtual bool HandlePlayerFireWeapon(std::shared_ptr<Actors::Player> player, WeaponType& weaponType, std::uint16_t& ammoDecrease) = 0;
virtual void HandlePlayerWarped(std::shared_ptr<Actors::Player> player, const Vector2f& prevPos, bool fast) = 0;
virtual bool HandlePlayerDied(Actors::Player* player) = 0;
virtual bool HandlePlayerFireWeapon(Actors::Player* player, WeaponType& weaponType, std::uint16_t& ammoDecrease) = 0;
virtual bool HandlePlayerSpring(Actors::Player* player, const Vector2f& force, bool keepSpeedX, bool keepSpeedY) = 0;
virtual void HandlePlayerWarped(Actors::Player* player, const Vector2f& prevPos, bool fast) = 0;
virtual void SetCheckpoint(const Vector2f& pos) = 0;
virtual void RollbackToCheckpoint() = 0;
virtual void ActivateSugarRush() = 0;
Expand Down
Loading

0 comments on commit 3fd6954

Please sign in to comment.