mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(gameplay): implement entity movement system with collision detection
- Add Velocity and HitBoxes components to entity - Introduce HitboxManager for loading per-entity collision AABBs from JSON - Create MoveSystem with per-axis collision handling - Move get_block_aabb to base World class and add virtual get_per_tick_time - Remove static get_block_aabb from ClientWorld; use member m_per_tick_time for tick duration
This commit is contained in:
63
src/gameplay/hitbox_manager.cpp
Normal file
63
src/gameplay/hitbox_manager.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "Cubed/gameplay/hitbox_manager.hpp"
|
||||
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
namespace fs = std::filesystem;
|
||||
using nlohmann::json;
|
||||
namespace Cubed {
|
||||
HitboxManager::HitboxManager() {}
|
||||
HitboxManager::~HitboxManager() {}
|
||||
|
||||
HitboxManager& HitboxManager::instance() {
|
||||
static HitboxManager inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
AABB HitboxManager::aabb(const std::string& key) {
|
||||
return instance().get_aabb(key);
|
||||
}
|
||||
|
||||
AABB HitboxManager::get_aabb(const std::string& key) {
|
||||
{
|
||||
cacc c;
|
||||
if (m_hitboxes.find(c, key)) {
|
||||
return c->second;
|
||||
}
|
||||
}
|
||||
|
||||
return load(key);
|
||||
}
|
||||
|
||||
AABB HitboxManager::load(const std::string& path) {
|
||||
fs::path p = ASSETS_PATH + path;
|
||||
try {
|
||||
glm::vec3 center;
|
||||
glm::vec3 half;
|
||||
std::ifstream s{p};
|
||||
json j = json::parse(s);
|
||||
if (j.contains("boxes")) {
|
||||
if (j["boxes"].contains("center")) {
|
||||
center.x = j["boxes"]["center"].at(0).get<float>();
|
||||
center.y = j["boxes"]["center"].at(1).get<float>();
|
||||
center.z = j["boxes"]["center"].at(2).get<float>();
|
||||
}
|
||||
if (j["boxes"].contains("half")) {
|
||||
half.x = j["boxes"]["half"].at(0).get<float>();
|
||||
half.y = j["boxes"]["half"].at(1).get<float>();
|
||||
half.z = j["boxes"]["half"].at(2).get<float>();
|
||||
}
|
||||
}
|
||||
acc a;
|
||||
if (m_hitboxes.insert(a, path)) {
|
||||
a->second = AABB{center, half};
|
||||
return a->second;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
Logger::error("Load Hitbox error {}", e.what());
|
||||
}
|
||||
Logger::error("Load hitbox {} Failed", path);
|
||||
return AABB{glm::vec3(0.0f), glm::vec3(0.0f)};
|
||||
}
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user