mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
refactor(gameplay): integrate model and hitbox ID system into Entity
- Introduce HitboxID, EntityID, and ModelID types for safer ID-based lookups - Replace AABB struct with Hitbox (includes HitboxID) - Convert ModelManager and HitboxManager to singletons with Handle structs - Update Entity to store IDs for model and hitbox, removing direct references - Reorganize creature model assets into subdirectories per entity type - Add player model (player.glb) and collision data for pig - Remove ModelManager dependency from App and Renderer - Add namespace parsing utility for asset paths - Mark sparse_vector::insert() with [[nodiscard]]
This commit is contained in:
@@ -20,8 +20,7 @@ App::App()
|
||||
|
||||
: m_game_config(ASSETS_PATH "config.toml"), m_window(m_game_config),
|
||||
m_texture_manager(m_game_config), m_audio(m_game_config),
|
||||
m_renderer(m_texture_manager, m_game_config, m_model_manager),
|
||||
m_scene_manager(*this) {}
|
||||
m_renderer(m_texture_manager, m_game_config), m_scene_manager(*this) {}
|
||||
|
||||
App::~App() {
|
||||
stop_text_input();
|
||||
@@ -86,7 +85,6 @@ void App::init(int argc, char** argv) {
|
||||
Logger::info("Renderer Init Success");
|
||||
// MapTable::init_map();
|
||||
m_texture_manager.init_texture();
|
||||
m_model_manager.init();
|
||||
Logger::info("Texture Load Success");
|
||||
|
||||
m_scene_manager.request_push(SceneType::MAIN_MENU);
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
namespace {} // namespace
|
||||
|
||||
namespace Cubed {
|
||||
ClientPlayer::ClientPlayer(ClientWorld& world) : m_world(world) {}
|
||||
ClientPlayer::ClientPlayer(ClientWorld& world)
|
||||
: Entity(0, "cubed:player"), m_world(world) {}
|
||||
ClientPlayer::~ClientPlayer() {}
|
||||
|
||||
const glm::vec3& ClientPlayer::get_front() const { return m_front; }
|
||||
@@ -312,10 +313,10 @@ void ClientPlayer::place_block(float dt) {
|
||||
if (type != 0) {
|
||||
glm::ivec3 near_pos = m_look_block->pos + m_look_block->normal;
|
||||
if (!m_world.is_solid(near_pos)) {
|
||||
AABB block_box = ClientWorld::get_block_aabb(near_pos);
|
||||
AABB player_box = HitboxManager::aabb("cubed:player");
|
||||
player_box.center += get_player_pos();
|
||||
if (!player_box.intersects(block_box)) {
|
||||
Hitbox block_box = ClientWorld::get_block_aabb(near_pos);
|
||||
auto player_box = HitboxManager::hitbox("cubed:player");
|
||||
player_box.box.center += get_player_pos();
|
||||
if (!player_box.box.intersects(block_box)) {
|
||||
m_world.report_block_change(near_pos, type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,14 +295,14 @@ void ClientWorld::push_delete_vao(std::unique_ptr<VertexArray>& vao) {
|
||||
void ClientWorld::report_block_change(const glm::ivec3& pos,
|
||||
unsigned id) const {
|
||||
if (id != 0) {
|
||||
AABB block_box = get_block_aabb(pos);
|
||||
Hitbox block_box = get_block_aabb(pos);
|
||||
std::shared_lock lock(m_players_date_mutex);
|
||||
|
||||
for (const auto& player : m_players_data) {
|
||||
|
||||
AABB box = HitboxManager::aabb("cubed:player");
|
||||
box.center += player.pos.value;
|
||||
if (box.intersects(block_box)) {
|
||||
auto box = HitboxManager::hitbox("cubed:player");
|
||||
box.box.center += player.pos.value;
|
||||
if (box.box.intersects(block_box)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,18 @@
|
||||
#include "Cubed/gameplay/entity.hpp"
|
||||
|
||||
#include "Cubed/gameplay/hitbox_manager.hpp"
|
||||
#include "Cubed/render/model_manager.hpp"
|
||||
namespace Cubed {
|
||||
|
||||
Entity::Entity(EntityID id, const std::string& name) {
|
||||
m_info.id = id;
|
||||
m_info.name = name;
|
||||
auto h = ModelManager::model(name);
|
||||
m_info.model = h.id;
|
||||
auto b = HitboxManager::hitbox(name);
|
||||
m_info.hitbox = b.id;
|
||||
}
|
||||
|
||||
glm::vec3& Entity::max_speed() { return m_velocity.max; }
|
||||
float& Entity::acceleration() { return m_movement.acceleration; }
|
||||
float& Entity::deceleration() { return m_movement.deceleration; }
|
||||
@@ -20,6 +32,7 @@ Movement& Entity::movement() { return m_movement; }
|
||||
Gravity& Entity::gravity() { return m_gravity; }
|
||||
MoveState& Entity::move_state() { return m_move_state; }
|
||||
Direction& Entity::direction() { return m_direction; }
|
||||
EntityInfo& Entity::info() { return m_info; }
|
||||
|
||||
const Velocity& Entity::velocity() const { return m_velocity; }
|
||||
const Position& Entity::pos() const { return m_pos; }
|
||||
@@ -29,5 +42,6 @@ const Movement& Entity::movement() const { return m_movement; }
|
||||
const Gravity& Entity::gravity() const { return m_gravity; }
|
||||
const MoveState& Entity::move_state() const { return m_move_state; }
|
||||
const Direction& Entity::direction() const { return m_direction; }
|
||||
const EntityInfo& Entity::info() const { return m_info; }
|
||||
|
||||
} // namespace Cubed
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "Cubed/gameplay/hitbox_manager.hpp"
|
||||
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
#include "Cubed/tools/name_space.hpp"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
namespace fs = std::filesystem;
|
||||
@@ -12,7 +14,13 @@ HitboxManager::HitboxManager() {
|
||||
glm::vec3 half = PLAYER_SIZE * 0.5f;
|
||||
|
||||
glm::vec3 center{0.0f, half.y, 0.0f};
|
||||
m_hitboxes.emplace("cubed:player", AABB{center, half});
|
||||
|
||||
HitboxMap::accessor a;
|
||||
if (m_hitboxes.insert(
|
||||
a, std::pair<HitboxID, Hitbox>{m_next++, Hitbox{center, half}})) {
|
||||
m_name_map.emplace(a->first, "cubed:player");
|
||||
m_id_map.emplace("cubed:player", a->first);
|
||||
}
|
||||
}
|
||||
|
||||
HitboxManager::~HitboxManager() {}
|
||||
@@ -22,23 +30,56 @@ HitboxManager& HitboxManager::instance() {
|
||||
return inst;
|
||||
}
|
||||
|
||||
AABB HitboxManager::aabb(const std::string& key) {
|
||||
return instance().get_aabb(key);
|
||||
HitboxManager::Handle HitboxManager::hitbox(const std::string& key) {
|
||||
return instance().get_hitbox(key);
|
||||
}
|
||||
|
||||
AABB HitboxManager::get_aabb(const std::string& key) {
|
||||
HitboxManager::Handle HitboxManager::hitbox(HitboxID id) {
|
||||
return instance().get_hitbox(id);
|
||||
}
|
||||
HitboxID HitboxManager::get_hitbox_id(const std::string& name) {
|
||||
IDMap::const_accessor cacc;
|
||||
if (m_id_map.find(cacc, name)) {
|
||||
return cacc->second;
|
||||
}
|
||||
return load(name).id;
|
||||
}
|
||||
const std::string& HitboxManager::get_hitbox_name(HitboxID id) {
|
||||
NameMap::const_accessor cacc;
|
||||
if (m_name_map.find(cacc, id)) {
|
||||
return cacc->second;
|
||||
}
|
||||
ASSERT_MSG(false, std::format("ModelManager: Can't find {}", id));
|
||||
static std::string n = "";
|
||||
return n;
|
||||
}
|
||||
|
||||
HitboxManager::Handle HitboxManager::get_hitbox(HitboxID id) {
|
||||
{
|
||||
cacc c;
|
||||
if (m_hitboxes.find(c, key)) {
|
||||
return c->second;
|
||||
HitboxMap::const_accessor c;
|
||||
if (m_hitboxes.find(c, id)) {
|
||||
return {c->second, c->first};
|
||||
}
|
||||
}
|
||||
|
||||
return load(key);
|
||||
return load(get_hitbox_name(id));
|
||||
}
|
||||
HitboxManager::Handle HitboxManager::get_hitbox(const std::string& name) {
|
||||
return get_hitbox(get_hitbox_id(name));
|
||||
}
|
||||
HitboxManager::Handle HitboxManager::load(std::string_view name) {
|
||||
|
||||
auto space = parse_namespace(name);
|
||||
ASSERT(space.size() >= 2);
|
||||
fs::path p;
|
||||
if (space[0] == "cubed") {
|
||||
p = std::format("{}model/creature/{}/collision.json", ASSETS_PATH,
|
||||
space[1]);
|
||||
} else {
|
||||
p = std::format("{}/model/creature/{}/collision.json", space[0],
|
||||
space[1]);
|
||||
}
|
||||
|
||||
AABB HitboxManager::load(const std::string& path) {
|
||||
fs::path p = ASSETS_PATH + path;
|
||||
try {
|
||||
glm::vec3 center;
|
||||
glm::vec3 half;
|
||||
@@ -56,16 +97,24 @@ AABB HitboxManager::load(const std::string& path) {
|
||||
half.z = j["boxes"]["half"].at(2).get<float>();
|
||||
}
|
||||
}
|
||||
acc a;
|
||||
if (m_hitboxes.insert(
|
||||
a, std::pair<std::string, AABB>{path, AABB{center, half}})) {
|
||||
return a->second;
|
||||
{
|
||||
HitboxMap::accessor a;
|
||||
if (m_hitboxes.insert(a, std::pair<HitboxID, Hitbox>{
|
||||
m_next++, Hitbox{center, half}})) {
|
||||
m_name_map.emplace(a->first, name);
|
||||
m_id_map.emplace(name, a->first);
|
||||
return {a->second, a->first};
|
||||
}
|
||||
}
|
||||
HitboxMap::const_accessor cacc;
|
||||
if (m_hitboxes.find(cacc, get_hitbox_id(std::string(name)))) {
|
||||
return {cacc->second, cacc->first};
|
||||
}
|
||||
} 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)};
|
||||
Logger::error("Load hitbox {} Failed", name);
|
||||
return {Hitbox{glm::vec3(0.0f), glm::vec3(0.0f)}, 0};
|
||||
}
|
||||
|
||||
} // namespace Cubed
|
||||
@@ -6,10 +6,10 @@ namespace Cubed {
|
||||
namespace {
|
||||
|
||||
bool update_x(const glm::vec3& pos, const glm::vec3& distance, World& world,
|
||||
const AABB& box) {
|
||||
const Hitbox& box) {
|
||||
glm::vec3 p = pos;
|
||||
p.x += distance.x;
|
||||
AABB b = box;
|
||||
Hitbox b = box;
|
||||
b.center += p;
|
||||
glm::vec3 min = b.min();
|
||||
glm::vec3 max = b.max();
|
||||
@@ -25,7 +25,7 @@ bool update_x(const glm::vec3& pos, const glm::vec3& distance, World& world,
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
glm::ivec3 block_pos{x, y, z};
|
||||
if (!world.can_pass_block(block_pos)) {
|
||||
AABB block_box = World::get_block_aabb(block_pos);
|
||||
Hitbox block_box = World::get_block_aabb(block_pos);
|
||||
if (b.intersects(block_box)) {
|
||||
return false;
|
||||
}
|
||||
@@ -37,10 +37,10 @@ bool update_x(const glm::vec3& pos, const glm::vec3& distance, World& world,
|
||||
}
|
||||
|
||||
bool update_y(const glm::vec3& pos, const glm::vec3& distance, World& world,
|
||||
const AABB& box) {
|
||||
const Hitbox& box) {
|
||||
glm::vec3 p = pos;
|
||||
p.y += distance.y;
|
||||
AABB b = box;
|
||||
Hitbox b = box;
|
||||
b.center += p;
|
||||
glm::vec3 min = b.min();
|
||||
glm::vec3 max = b.max();
|
||||
@@ -56,7 +56,7 @@ bool update_y(const glm::vec3& pos, const glm::vec3& distance, World& world,
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
glm::ivec3 block_pos{x, y, z};
|
||||
if (!world.can_pass_block(block_pos)) {
|
||||
AABB block_box = World::get_block_aabb(block_pos);
|
||||
Hitbox block_box = World::get_block_aabb(block_pos);
|
||||
if (b.intersects(block_box)) {
|
||||
return false;
|
||||
}
|
||||
@@ -68,10 +68,10 @@ bool update_y(const glm::vec3& pos, const glm::vec3& distance, World& world,
|
||||
}
|
||||
|
||||
bool update_z(const glm::vec3& pos, const glm::vec3& distance, World& world,
|
||||
const AABB& box) {
|
||||
const Hitbox& box) {
|
||||
glm::vec3 p = pos;
|
||||
p.z += distance.z;
|
||||
AABB b = box;
|
||||
Hitbox b = box;
|
||||
b.center += p;
|
||||
glm::vec3 min = b.min();
|
||||
glm::vec3 max = b.max();
|
||||
@@ -87,7 +87,7 @@ bool update_z(const glm::vec3& pos, const glm::vec3& distance, World& world,
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
glm::ivec3 block_pos{x, y, z};
|
||||
if (!world.can_pass_block(block_pos)) {
|
||||
AABB block_box = World::get_block_aabb(block_pos);
|
||||
Hitbox block_box = World::get_block_aabb(block_pos);
|
||||
if (b.intersects(block_box)) {
|
||||
return false;
|
||||
}
|
||||
@@ -113,18 +113,18 @@ std::tuple<bool, bool, bool> PhysicalSystem::update(float dt, Entity& e,
|
||||
auto distance = get_move_distance(dt, e);
|
||||
auto& m_velocity = e.velocity();
|
||||
auto& m_move_state = e.move_state();
|
||||
AABB box = HitboxManager::aabb("cubed:player");
|
||||
auto box = HitboxManager::hitbox(e.info().hitbox);
|
||||
bool x = false;
|
||||
bool y = false;
|
||||
bool z = false;
|
||||
if (update_x(moved_pos, distance, world, box)) {
|
||||
if (update_x(moved_pos, distance, world, box.box)) {
|
||||
moved_pos.x += distance.x;
|
||||
x = true;
|
||||
} else {
|
||||
m_velocity.value.x = 0.0f;
|
||||
}
|
||||
|
||||
if (update_y(moved_pos, distance, world, box)) {
|
||||
if (update_y(moved_pos, distance, world, box.box)) {
|
||||
moved_pos.y += distance.y;
|
||||
y = true;
|
||||
} else {
|
||||
@@ -135,7 +135,7 @@ std::tuple<bool, bool, bool> PhysicalSystem::update(float dt, Entity& e,
|
||||
}
|
||||
}
|
||||
|
||||
if (update_z(moved_pos, distance, world, box)) {
|
||||
if (update_z(moved_pos, distance, world, box.box)) {
|
||||
moved_pos.z += distance.z;
|
||||
z = true;
|
||||
} else {
|
||||
|
||||
@@ -2,9 +2,24 @@
|
||||
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
#include "Cubed/tools/name_space.hpp"
|
||||
namespace Cubed {
|
||||
|
||||
ModelManager::ModelManager() {}
|
||||
ModelManager& ModelManager::instance() {
|
||||
static ModelManager inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
ModelManager::Handle ModelManager::model(const std::string& model_name) {
|
||||
return instance().get_model(model_name);
|
||||
}
|
||||
[[nodiscard]]
|
||||
ModelManager::Handle ModelManager::model(ModelID id) {
|
||||
return instance().get_model(id);
|
||||
}
|
||||
|
||||
ModelManager::ModelManager() { init(); }
|
||||
|
||||
ModelManager::~ModelManager() {}
|
||||
|
||||
@@ -12,15 +27,15 @@ void ModelManager::init() {
|
||||
|
||||
};
|
||||
|
||||
const ModelNode& ModelManager::get_model(const std::string& model_name) {
|
||||
ModelManager::Handle ModelManager::get_model(const std::string& model_name) {
|
||||
return get_model(get_model_id(model_name));
|
||||
}
|
||||
|
||||
const ModelNode& ModelManager::get_model(ModelID id) {
|
||||
ModelManager::Handle ModelManager::get_model(ModelID id) {
|
||||
|
||||
ModelMap::const_accessor cacc;
|
||||
if (!m_models.find(cacc, id)) {
|
||||
return cacc->second;
|
||||
return {cacc->second, cacc->first};
|
||||
}
|
||||
return load_model(get_model_name(id));
|
||||
}
|
||||
@@ -32,8 +47,7 @@ ModelID ModelManager::get_model_id(const std::string& name) {
|
||||
if (m_id_map.find(cacc, name)) {
|
||||
return cacc->second;
|
||||
}
|
||||
ASSERT_MSG(false, std::format("ModelManager: Can't find {}", name));
|
||||
return 0;
|
||||
return load_model(name).id;
|
||||
}
|
||||
|
||||
const std::string& ModelManager::get_model_name(ModelID id) {
|
||||
@@ -46,23 +60,24 @@ const std::string& ModelManager::get_model_name(ModelID id) {
|
||||
return n;
|
||||
}
|
||||
|
||||
const ModelNode& ModelManager::load_model(std::string_view model_name) {
|
||||
ModelManager::Handle ModelManager::load_model(std::string_view model_name) {
|
||||
auto p = model_name.find(':');
|
||||
if (p == std::string::npos) {
|
||||
Logger::error("Can't Parse Model name {}", model_name);
|
||||
ASSERT(false);
|
||||
}
|
||||
auto name = model_name.substr(p + 1);
|
||||
auto space = model_name.substr(0, p);
|
||||
if (name.empty()) {
|
||||
auto space = parse_namespace(model_name);
|
||||
if (space.size() < 2) {
|
||||
Logger::error("Can't Parse Model name {}", model_name);
|
||||
ASSERT(false);
|
||||
}
|
||||
std::string path;
|
||||
if (space == "cubed") {
|
||||
path = std::format("{}model/creature/{}", ASSETS_PATH, name);
|
||||
if (space[0] == "cubed") {
|
||||
path = std::format("{}model/creature/{}/{}.gbl", ASSETS_PATH, space[1],
|
||||
space[1]);
|
||||
} else {
|
||||
path = std::format("./{}/model/creature/{}", space, name);
|
||||
path = std::format("./{}/model/creature/{}/{}.gbl", space[0], space[1],
|
||||
space[1]);
|
||||
}
|
||||
auto model = m_loader.load(path);
|
||||
ModelMap::accessor acc;
|
||||
@@ -70,11 +85,14 @@ const ModelNode& ModelManager::load_model(std::string_view model_name) {
|
||||
acc->second = std::move(model);
|
||||
} else {
|
||||
Logger::error("Can't Insert Model {}", model_name);
|
||||
ASSERT(false);
|
||||
ModelMap::const_accessor cacc;
|
||||
if (m_models.find(cacc, get_model_id(std::string(model_name)))) {
|
||||
return {cacc->second, cacc->first};
|
||||
}
|
||||
}
|
||||
m_id_map.emplace(model_name, acc->first);
|
||||
m_name_map.emplace(acc->first, model_name);
|
||||
return acc->second;
|
||||
return {acc->second, acc->first};
|
||||
}
|
||||
|
||||
} // namespace Cubed
|
||||
@@ -6,10 +6,9 @@
|
||||
namespace Cubed {
|
||||
ModelRender::ModelRender(Renderer& renderer) : m_renderer(renderer) {}
|
||||
|
||||
void ModelRender::render_model(const std::string& name, const glm::vec3& pos,
|
||||
void ModelRender::render_model(ModelID id, const glm::vec3& pos,
|
||||
Camera& camera) {
|
||||
auto& model_manager = m_renderer.model_manager();
|
||||
auto& root = model_manager.get_model(name);
|
||||
auto& root = ModelManager::model(id).node;
|
||||
glm::mat4 transform = glm::translate(glm::mat4(1.0f), pos);
|
||||
auto& shader = m_renderer.get_shader("model_shader");
|
||||
glm::mat4 view = camera.get_camera_lookat();
|
||||
@@ -17,10 +16,9 @@ void ModelRender::render_model(const std::string& name, const glm::vec3& pos,
|
||||
render_node(root, transform, view, shader, false);
|
||||
}
|
||||
|
||||
void ModelRender::shadow_pass(const std::string& name, const glm::vec3& pos,
|
||||
void ModelRender::shadow_pass(ModelID id, const glm::vec3& pos,
|
||||
Camera& camera) {
|
||||
auto& model_manager = m_renderer.model_manager();
|
||||
auto& root = model_manager.get_model(name);
|
||||
auto& root = ModelManager::model(id).node;
|
||||
glm::mat4 transform = glm::translate(glm::mat4(1.0f), pos);
|
||||
auto& shader = m_renderer.get_shader("depth_model");
|
||||
glm::mat4 view = camera.get_camera_lookat();
|
||||
|
||||
@@ -15,10 +15,9 @@
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
Renderer::Renderer(TextureManager& texture_manager, Config& config,
|
||||
ModelManager& model_manager)
|
||||
: m_texture_manager(texture_manager), m_model_manager(model_manager),
|
||||
m_world_renderer(*this), m_model_renderer(*this), m_config(config) {}
|
||||
Renderer::Renderer(TextureManager& texture_manager, Config& config)
|
||||
: m_texture_manager(texture_manager), m_world_renderer(*this),
|
||||
m_model_renderer(*this), m_config(config) {}
|
||||
|
||||
Renderer::~Renderer() {
|
||||
if (m_init) {
|
||||
@@ -341,6 +340,5 @@ float Renderer::frame_height() const { return m_frame_height; }
|
||||
float Renderer::frame_width() const { return m_frame_width; }
|
||||
const glm::mat4& Renderer::p_mat() const { return m_world_proj_matrix; }
|
||||
const std::vector<VertexArray>& Renderer::vao() const { return m_vao; }
|
||||
ModelManager& Renderer::model_manager() { return m_model_manager; }
|
||||
ModelRender& Renderer::model_renderer() { return m_model_renderer; }
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user