feat: move players to world instead of main

This commit is contained in:
2026-03-13 16:35:36 +08:00
parent 32d617b440
commit 98967275ff
6 changed files with 40 additions and 18 deletions

View File

@@ -2,6 +2,8 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <string>
struct MoveState { struct MoveState {
bool forward = false; bool forward = false;
bool back = false; bool back = false;
@@ -11,12 +13,11 @@ struct MoveState {
bool up = false; bool up = false;
}; };
class World;
class Player { class Player {
private: private:
bool** m_block_present;
float m_yaw; float m_yaw;
float m_pitch; float m_pitch;
@@ -29,14 +30,17 @@ private:
glm::vec3 m_right; glm::vec3 m_right;
MoveState m_move_state; MoveState m_move_state;
std::string m_name;
const World& m_world;
public: public:
Player(); Player(const World& world, const std::string& name);
const glm::vec3& get_front() const; const glm::vec3& get_front() const;
const glm::vec3& get_player_pos() const; const glm::vec3& get_player_pos() const;
const MoveState& get_move_state() const; const MoveState& get_move_state() const;
void init(bool** block_present);
void set_player_pos(const glm::vec3& pos); void set_player_pos(const glm::vec3& pos);
void update(float delta_time); void update(float delta_time);
void update_front_vec(float offset_x, float offset_y); void update_front_vec(float offset_x, float offset_y);

View File

@@ -3,22 +3,23 @@
#include <Cubed/gameplay/chunk.hpp> #include <Cubed/gameplay/chunk.hpp>
class Player;
class World { class World {
private: private:
BlockRenderData m_block_render_data; BlockRenderData m_block_render_data;
std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks; std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks;
std::unordered_map<std::size_t, Player> m_players;
public: public:
World(); World();
~World(); ~World();
const BlockRenderData& get_block_render_data(int x, int y ,int z); const BlockRenderData& get_block_render_data(int x, int y ,int z);
Player& get_player(const std::string& name);
void init_world(); void init_world();
void render(); void render();
void update(float delta_time);
}; };

View File

@@ -3,7 +3,7 @@
namespace Assert { namespace Assert {
inline void msg(const char* condition, const char* file, inline void msg(const char* condition, const char* file,
int line, const char* func, int line, const char* func,
const std::string& message const std::string& message = ""
) { ) {
LOG::error("Assertion failed: {} at {}: {} in function {}", LOG::error("Assertion failed: {} at {}: {} in function {}",

View File

@@ -2,7 +2,10 @@
#include <Cubed/gameplay/player.hpp> #include <Cubed/gameplay/player.hpp>
Player::Player() { Player::Player(const World& world, const std::string& name) :
m_world(world),
m_name(name)
{
} }
@@ -18,10 +21,6 @@ const MoveState& Player::get_move_state() const {
return m_move_state; return m_move_state;
} }
void Player::init(bool** block_present) {
m_block_present = block_present;
}
void Player::set_player_pos(const glm::vec3& pos) { void Player::set_player_pos(const glm::vec3& pos) {
m_player_pos = pos; m_player_pos = pos;
} }

View File

@@ -1,5 +1,7 @@
#include <Cubed/gameplay/player.hpp>
#include <Cubed/gameplay/world.hpp> #include <Cubed/gameplay/world.hpp>
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/cubed_hash.hpp>
World::World() { World::World() {
} }
@@ -209,6 +211,16 @@ const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,in
return m_block_render_data; return m_block_render_data;
} }
Player& World::get_player(const std::string& name){
auto it = m_players.find(HASH::str(name));
if (it == m_players.end()) {
LOG::error("Can't find player {}", name);
CUBED_ASSERT(0);
}
return it->second;
}
void World::init_world() { void World::init_world() {
for (int s = 0; s < DISTANCE; s++) { for (int s = 0; s < DISTANCE; s++) {
@@ -235,7 +247,8 @@ void World::init_world() {
chunk.gen_vertex_data(); chunk.gen_vertex_data();
} }
// init players
m_players.emplace(HASH::str("TestPlayer"), Player(*this, "TestPlayer"));
} }
void World::render() { void World::render() {
@@ -255,4 +268,10 @@ void World::render() {
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
//LOG::info("Chunk {} {} render finished", pos.x, pos.z); //LOG::info("Chunk {} {} render finished", pos.x, pos.z);
} }
}
void World::update(float delta_time) {
for (auto& player : m_players) {
player.second.update(delta_time);
}
} }

View File

@@ -33,7 +33,6 @@ float tf = 0.0f;
double last_time = 0.0f; double last_time = 0.0f;
double delta_time = 0.0f; double delta_time = 0.0f;
GLuint texture_array; GLuint texture_array;
Player player;
Camera camera; Camera camera;
TextureManager texture_manager; TextureManager texture_manager;
World world; World world;
@@ -105,7 +104,7 @@ void init(GLFWwindow* window) {
mv_loc = glGetUniformLocation(rendering_program, "mv_matrix"); mv_loc = glGetUniformLocation(rendering_program, "mv_matrix");
proj_loc = glGetUniformLocation(rendering_program, "proj_matrix"); proj_loc = glGetUniformLocation(rendering_program, "proj_matrix");
camera.camera_init(&player); camera.camera_init(&world.get_player("TestPlayer"));
glfwGetFramebufferSize(window, &width, &height); glfwGetFramebufferSize(window, &width, &height);
aspect = (float)width / (float)height; aspect = (float)width / (float)height;
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
@@ -144,7 +143,7 @@ void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
void display(GLFWwindow* window, double current_time) { void display(GLFWwindow* window, double current_time) {
delta_time = current_time - last_time; delta_time = current_time - last_time;
last_time = current_time; last_time = current_time;
player.update(delta_time); world.update(delta_time);
camera.update_move_camera(); camera.update_move_camera();
@@ -183,7 +182,7 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
} }
player.update_player_move_state(key, action); world.get_player("TestPlayer").update_player_move_state(key, action);
} }