From 198a13fa1004534538122579dcdac4cf311b8603 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 2 Mar 2021 19:21:36 -0500 Subject: Handled case where a pathfollowing sprite overshoots its destination This may happen due to lag or due to debugging with lldb. What happens is that 60 times a second, the engine will check whether the direction toward the current endpoint is equal to the direction in the pathfinding instruction, and if it isn't (because the sprite has gone past the endpoint) then it sets the sprite's position to the endpoint instantly so that the pathfinding can continue. --- src/behaviour_system.cpp | 15 +++++++++++++++ src/behaviour_system.h | 1 + 2 files changed, 16 insertions(+) (limited to 'src') diff --git a/src/behaviour_system.cpp b/src/behaviour_system.cpp index a05912c..b68cd6d 100644 --- a/src/behaviour_system.cpp +++ b/src/behaviour_system.cpp @@ -40,6 +40,21 @@ void BehaviourSystem::tick(double dt) { } } + overcorrectionTimer_.accumulate(dt); + while (overcorrectionTimer_.step()) { + for (int spriteId : game_.getSprites()) { + Sprite& sprite = game_.getSprite(spriteId); + if (!sprite.paused && + sprite.behaviourType == BehaviourType::Path && + !sprite.path.empty() && + sprite.loc != sprite.path.front().endpoint) { + if (directionFacingPoint(sprite.path.front().endpoint - sprite.loc) != sprite.path.front().dir) { + game_.getSystem().moveSprite(spriteId, sprite.path.front().endpoint); + } + } + } + } + for (int spriteId : game_.getSprites()) { Sprite& sprite = game_.getSprite(spriteId); if (!sprite.paused && sprite.behaviourType == BehaviourType::Path) { diff --git a/src/behaviour_system.h b/src/behaviour_system.h index 526a09b..1ddce4a 100644 --- a/src/behaviour_system.h +++ b/src/behaviour_system.h @@ -31,6 +31,7 @@ private: Game& game_; Timer timer_ { 500 }; + Timer overcorrectionTimer_ { 1000/60 }; }; #endif /* end of include guard: BEHAVIOUR_SYSTEM_H_FC908ABE */ -- cgit 1.4.1