mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
Refactor server entity update flow to process entities individually, skipping those whose chunk is not loaded. System update methods now accept a single entity instead of iterating the full registry view, and `ServerWorld::get_chunk_ref_count` is added to determine if an entity's chunk is active.
41 lines
1.2 KiB
C++
41 lines
1.2 KiB
C++
#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.01;
|
|
constexpr double MOVE_PROBABILITY = 0.01;
|
|
} // namespace
|
|
|
|
namespace Cubed {
|
|
void WanderAISystem::update(entt::registry& registry, entt::entity e) {
|
|
if (!registry.all_of<AIBase, WanderAITag, BaseServerCreature, MoveBoost>(
|
|
e)) {
|
|
return;
|
|
}
|
|
|
|
auto [ai, creature, move_boost] =
|
|
registry.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.transform.direction.value = r.random_direction_horizontal();
|
|
}
|
|
if (r.random_bool(MOVE_PROBABILITY)) {
|
|
|
|
move_boost.duration = r.random_int(20, 40);
|
|
move_boost.count = 0;
|
|
}
|
|
}
|
|
|
|
} // namespace Cubed
|