mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 09:47:01 +08:00
feat(gameplay): add server-side entity movement and AI
Refactor LocalPlayer::update_physical to operate on a passed position, and run speed, physical, and wander AI systems in ServerEntityManager.
This commit is contained in:
@@ -156,7 +156,7 @@ private:
|
||||
Gait compute_gait() const;
|
||||
|
||||
void update_speed(float dt);
|
||||
std::tuple<bool, bool, bool> update_physical(float dt);
|
||||
std::tuple<bool, bool, bool> update_physical(float dt, glm::vec3& pos);
|
||||
glm::vec3 get_move_distance(float dt);
|
||||
};
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -45,6 +45,7 @@ private:
|
||||
void handle_task();
|
||||
void send_all_entities(std::shared_ptr<Session>& session);
|
||||
void update_ai();
|
||||
void update_move();
|
||||
template <typename... Args>
|
||||
EntityID create_entity_in_factory(Args&&... args) {
|
||||
auto entity = m_registry.create();
|
||||
|
||||
@@ -473,7 +473,7 @@ void LocalPlayer::update_move(float dt) {
|
||||
if (m_game_mode == SPECTATOR) {
|
||||
player_pos += get_move_distance(dt);
|
||||
} else {
|
||||
auto [x, y, z] = update_physical(dt);
|
||||
auto [x, y, z] = update_physical(dt, player_pos);
|
||||
if (!x || !z) {
|
||||
m_sprinting = false;
|
||||
}
|
||||
@@ -736,22 +736,23 @@ void LocalPlayer::update_speed(float dt) {
|
||||
}
|
||||
}
|
||||
|
||||
std::tuple<bool, bool, bool> LocalPlayer::update_physical(float dt) {
|
||||
std::tuple<bool, bool, bool> LocalPlayer::update_physical(float dt,
|
||||
glm::vec3& pos) {
|
||||
|
||||
auto distance = get_move_distance(dt);
|
||||
auto box = HitboxManager::hitbox(m_hitbox);
|
||||
bool x = false;
|
||||
bool y = false;
|
||||
bool z = false;
|
||||
if (update_x(m_pos.value, distance, m_world, box.box)) {
|
||||
m_pos.value.x += distance.x;
|
||||
if (update_x(pos, distance, m_world, box.box)) {
|
||||
pos.x += distance.x;
|
||||
x = true;
|
||||
} else {
|
||||
m_velocity.value.x = 0.0f;
|
||||
}
|
||||
|
||||
if (update_y(m_pos.value, distance, m_world, box.box)) {
|
||||
m_pos.value.y += distance.y;
|
||||
if (update_y(pos, distance, m_world, box.box)) {
|
||||
pos.y += distance.y;
|
||||
y = true;
|
||||
} else {
|
||||
m_velocity.value.y = 0.0f;
|
||||
@@ -761,8 +762,8 @@ std::tuple<bool, bool, bool> LocalPlayer::update_physical(float dt) {
|
||||
}
|
||||
}
|
||||
|
||||
if (update_z(m_pos.value, distance, m_world, box.box)) {
|
||||
m_pos.value.z += distance.z;
|
||||
if (update_z(pos, distance, m_world, box.box)) {
|
||||
pos.z += distance.z;
|
||||
z = true;
|
||||
} else {
|
||||
m_velocity.value.z = 0.0f;
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
#include "Cubed/gameplay/hitbox_manager.hpp"
|
||||
#include "Cubed/gameplay/server_world.hpp"
|
||||
#include "Cubed/gameplay/session.hpp"
|
||||
#include "Cubed/gameplay/systems/physical_system.hpp"
|
||||
#include "Cubed/gameplay/systems/speed_system.hpp"
|
||||
#include "Cubed/gameplay/systems/wander_ai_system.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/net_utils.hpp"
|
||||
using namespace google::protobuf;
|
||||
@@ -25,8 +28,15 @@ void ServerEntityManager::init() {
|
||||
void ServerEntityManager::update() {
|
||||
handle_task();
|
||||
update_ai();
|
||||
update_move();
|
||||
}
|
||||
|
||||
void ServerEntityManager::update_ai() { WanderAISystem::update(m_registry); }
|
||||
void ServerEntityManager::update_move() {
|
||||
SpeedSystem::update(
|
||||
static_cast<float>(m_world.get_per_tick_time()) / 1000.0f, m_registry);
|
||||
PhysicalSystem::update(m_world, m_registry);
|
||||
}
|
||||
void ServerEntityManager::handle_task() {
|
||||
TaskPair pair;
|
||||
while (m_tasks.try_pop(pair)) {
|
||||
|
||||
Reference in New Issue
Block a user