feat(ai): add wander AI system with move boost

Add AIBase and WanderAITag components, a WanderAISystem, and a MoveBoost component to control wandering behavior. Replace the old MoveState with MoveBoost and include a horizontal random direction helper.
This commit is contained in:
2026-07-31 19:59:33 +08:00
parent 8451921161
commit 412f88565a
7 changed files with 87 additions and 3 deletions

View File

@@ -0,0 +1,40 @@
#include "Cubed/gameplay/systems/wander_ai_system.hpp"
#include "Cubed/gameplay/ecs/ai_struct.hpp"
#include "Cubed/gameplay/ecs/server_entity.hpp"
#include "Cubed/tools/cubed_random.hpp"
namespace {
constexpr double DIRECTION_PROBABILITY = 0.1;
constexpr double MOVE_PROBABILITY = 0.1;
} // namespace
namespace Cubed {
void WanderAISystem::update(entt::registry& registry) {
auto view =
registry.view<AIBase, WanderAITag, BaseServerCreature, MoveBoost>();
for (auto e : view) {
auto [ai, creature, move_boost] =
view.get<AIBase, BaseServerCreature, MoveBoost>(e);
++ai.count;
if (ai.count >= ai.interval) {
ai.count = 0;
do_ai(creature, move_boost);
}
}
}
void WanderAISystem::do_ai(BaseServerCreature& creature,
MoveBoost& move_boost) {
thread_local Random r{std::random_device()()};
if (r.random_bool(DIRECTION_PROBABILITY)) {
creature.direction.value = r.random_direction_horizontal();
}
if (r.random_bool(MOVE_PROBABILITY)) {
move_boost.duration = r.random_int(5, 8);
move_boost.count = 0;
}
}
} // namespace Cubed