mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
Compare commits
2 Commits
2e4a32fed7
...
a54858e4c6
| Author | SHA1 | Date | |
|---|---|---|---|
| a54858e4c6 | |||
| 18e308bd03 |
@@ -46,6 +46,7 @@ private:
|
|||||||
void send_all_entities(std::shared_ptr<Session>& session);
|
void send_all_entities(std::shared_ptr<Session>& session);
|
||||||
void update_ai();
|
void update_ai();
|
||||||
void update_move();
|
void update_move();
|
||||||
|
void update_send();
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
EntityID create_entity_in_factory(Args&&... args) {
|
EntityID create_entity_in_factory(Args&&... args) {
|
||||||
auto entity = m_registry.create();
|
auto entity = m_registry.create();
|
||||||
|
|||||||
@@ -20,23 +20,44 @@ void ServerEntityManager::init() {
|
|||||||
m_factories.try_emplace("cubed:pig", [this]() {
|
m_factories.try_emplace("cubed:pig", [this]() {
|
||||||
BaseServerCreature c;
|
BaseServerCreature c;
|
||||||
c.hitbox = HitboxManager::instance().get_hitbox_id("cubed:pig");
|
c.hitbox = HitboxManager::instance().get_hitbox_id("cubed:pig");
|
||||||
return create_entity_in_factory(Entity{m_next},
|
return create_entity_in_factory(
|
||||||
EntityInfo{"cubed:pig", ""},
|
Entity{m_next}, EntityInfo{"cubed:pig", ""}, std::move(c), PigTag{},
|
||||||
std::move(c), PigTag{});
|
AIBase{}, WanderAITag{}, MoveBoost{});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
void ServerEntityManager::update() {
|
void ServerEntityManager::update() {
|
||||||
handle_task();
|
handle_task();
|
||||||
update_ai();
|
update_ai();
|
||||||
update_move();
|
update_move();
|
||||||
|
|
||||||
|
update_send();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerEntityManager::update_ai() { WanderAISystem::update(m_registry); }
|
void ServerEntityManager::update_ai() { WanderAISystem::update(m_registry); }
|
||||||
|
|
||||||
void ServerEntityManager::update_move() {
|
void ServerEntityManager::update_move() {
|
||||||
SpeedSystem::update(
|
SpeedSystem::update(
|
||||||
static_cast<float>(m_world.get_per_tick_time()) / 1000.0f, m_registry);
|
static_cast<float>(m_world.get_per_tick_time()) / 1000.0f, m_registry);
|
||||||
PhysicalSystem::update(m_world, m_registry);
|
PhysicalSystem::update(m_world, m_registry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ServerEntityManager::update_send() {
|
||||||
|
auto sessions = m_world.get_all_session();
|
||||||
|
auto view = m_registry.view<Entity, BaseServerCreature>();
|
||||||
|
|
||||||
|
for (auto& e : view) {
|
||||||
|
auto [entity, creature] = view.get<Entity, BaseServerCreature>(e);
|
||||||
|
Arena arena;
|
||||||
|
auto* p = Arena::Create<S2CEntityUpdate>(&arena);
|
||||||
|
p->set_id(entity.id);
|
||||||
|
Tools::set_net_pos(p, creature.transform.position.value);
|
||||||
|
|
||||||
|
for (auto& s : sessions) {
|
||||||
|
s->send(make_packet(p));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ServerEntityManager::handle_task() {
|
void ServerEntityManager::handle_task() {
|
||||||
TaskPair pair;
|
TaskPair pair;
|
||||||
while (m_tasks.try_pop(pair)) {
|
while (m_tasks.try_pop(pair)) {
|
||||||
|
|||||||
@@ -14,11 +14,24 @@ void SpeedSystem::update(float dt, entt::registry& registry) {
|
|||||||
++moveboost.count;
|
++moveboost.count;
|
||||||
creature.velocity.value +=
|
creature.velocity.value +=
|
||||||
creature.direction.value * creature.movement.acceleration;
|
creature.direction.value * creature.movement.acceleration;
|
||||||
} else {
|
} else if (glm::dot(creature.velocity.value, creature.velocity.value) >=
|
||||||
|
0.001f) {
|
||||||
creature.velocity.value -=
|
creature.velocity.value -=
|
||||||
creature.direction.value * creature.movement.deceleration;
|
creature.direction.value * creature.movement.deceleration;
|
||||||
|
} else {
|
||||||
|
creature.velocity.value = glm::vec3(0.0f);
|
||||||
}
|
}
|
||||||
creature.velocity.value.y = creature.gravity.value * dt;
|
creature.velocity.value.y += -creature.gravity.value * dt;
|
||||||
|
auto v_clamp = [](float& v, float max) {
|
||||||
|
auto temp = std::abs(v);
|
||||||
|
max = std::abs(max);
|
||||||
|
temp = std::clamp(temp, 0.0f, max);
|
||||||
|
|
||||||
|
v = temp;
|
||||||
|
};
|
||||||
|
v_clamp(creature.velocity.value.x, creature.velocity.max.x);
|
||||||
|
v_clamp(creature.velocity.value.y, creature.velocity.max.y);
|
||||||
|
v_clamp(creature.velocity.value.z, creature.velocity.max.z);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user