feat: add world and player tab item

This commit is contained in:
2026-04-25 18:36:13 +08:00
parent 8b5717a655
commit a95ad796ce
10 changed files with 260 additions and 38 deletions

View File

@@ -2,6 +2,7 @@
#include <Cubed/app.hpp>
#include <Cubed/gameplay/player.hpp>
#include <Cubed/tools/log.hpp>
#include <Cubed/tools/perlin_noise.hpp>
#include <imgui.h>
#include <imgui_impl_glfw.h>
@@ -9,6 +10,21 @@
namespace Cubed {
static constexpr const char* THEMES[] = {"Dark", "Light"};
static constexpr const char* GAITS[] = {"Walk", "Run"};
static constexpr const char* GAME_MODES[] = {"Creative", "Spectator"};
static char perlin_noise_input_buffer[64];
static int filter_unsigned(ImGuiInputTextCallbackData* data) {
if (data->EventFlag == ImGuiInputTextFlags_CallbackCharFilter) {
char c = data->EventChar;
if (c < '0' || c > '9') {
return 1;
}
}
return 0;
}
DevPanel::DevPanel(App& app) :
m_app(app)
{
@@ -29,6 +45,7 @@ void DevPanel::init() {
if (m_theme != 1 && m_theme != 0) {
m_theme = 0;
}
update_player_profile();
}
void DevPanel::render() {
@@ -51,6 +68,8 @@ void DevPanel::render() {
ImGui::Text("This is a DevPanel to control the game\n");
if (ImGui::BeginTabBar("Bar")) {
show_settings_tab_item();
show_world_tab_item();
show_player_tab_item();
ImGui::EndTabBar();
}
ImGui::End();
@@ -67,7 +86,7 @@ void DevPanel::show_settings_tab_item() {
}
ImGui::SameLine();
if (ImGui::Button("default##1")) {
m_config.fov = NORMAL_FOV;
m_config.fov = DEFAULT_FOV;
Config::get().set("player.fov", static_cast<double>(m_config.fov));
m_app.renderer().hot_reload();
}
@@ -94,7 +113,7 @@ void DevPanel::show_settings_tab_item() {
Config::get().set("window.V-Sync", m_config.v_sync);
m_app.window().hot_reload();
}
constexpr const char* THEMES[] = {"Dark", "Light"};
if (ImGui::Combo("Theme", &m_theme, THEMES, IM_ARRAYSIZE(THEMES))) {
if (m_theme == 0) {
ImGui::StyleColorsDark();
@@ -112,4 +131,99 @@ void DevPanel::show_settings_tab_item() {
}
void DevPanel::show_world_tab_item() {
if (ImGui::BeginTabItem("world")) {
if (m_text_editing.perlin_seed) {
if (ImGui::InputText("Perlin Noise Seed", perlin_noise_input_buffer, sizeof(perlin_noise_input_buffer),
ImGuiInputTextFlags_CallbackCharFilter |
ImGuiInputTextFlags_EnterReturnsTrue,
filter_unsigned))
{
PerlinNoise::seed(static_cast<unsigned int>(std::strtoul(perlin_noise_input_buffer, nullptr, 10)));
m_text_editing.perlin_seed = false;
}
}
if (!m_text_editing.perlin_seed) {
ImGui::Text("Perlin Noise Seed: %u", PerlinNoise::seed());
if (ImGui::IsItemClicked()) {
m_text_editing.perlin_seed = true;
}
}
if (ImGui::Button("Rebuild World")) {
m_app.world().rebuild_world();
}
ImGui::EndTabItem();
}
}
void DevPanel::show_player_tab_item() {
if (!m_player) {
Logger::error("Player is Nullptr");
return;
}
if (ImGui::BeginTabItem("player")) {
if (ImGui::Combo("GameMode", &m_player_profile.game_mode, GAME_MODES, IM_ARRAYSIZE(GAME_MODES))) {
if (m_player_profile.game_mode == 0) {
m_player->change_mode(GameMode::CREATIVE);
} else if (m_player_profile.game_mode == 1) {
m_player->change_mode(GameMode::SPECTATOR);
} else {
ASSERT_MSG(false, "Unknown GameMode");
}
}
if (m_player->game_mode() == GameMode::CREATIVE) {
if (ImGui::Combo("Gait", &m_player_profile.gait, GAITS, IM_ARRAYSIZE(GAITS))) {
if (m_player_profile.gait == 0) {
m_player->gait() = Gait::WALK;
} else if (m_player_profile.gait == 1) {
m_player->gait() = Gait::RUN;
} else {
ASSERT_MSG(false, "Unknown Gait");
}
}
}
ImGui::SliderFloat("Acceleration", &m_player->acceleration(), 1.0f, 200.0f);
ImGui::SliderFloat("Deceleration", &m_player->deceleration(), 1.0f, 200.0f);
if (m_player->game_mode() == GameMode::CREATIVE) {
m_player_profile.game_mode = 0;
ImGui::SliderFloat("MaxWalkSpeed", &m_player->max_walk_speed(), 1.0f, 200.0f);
ImGui::SliderFloat("MaxRunSpeed", &m_player->max_run_speed(), 1.0f, 500.0f);
ImGui::SliderFloat("G", &m_player->g(), 1.0f, 200.0f);
} else if (m_player->game_mode() == GameMode::SPECTATOR) {
m_player_profile.game_mode = 1;
ImGui::SliderFloat("MaxSpeed", &m_player->max_speed(), 1.0f, 500.0f);
}
if (ImGui::Button("reset")) {
m_player->max_walk_speed() = DEFAULT_MAX_WALK_SPEED;
m_player->max_run_speed() = DEFAULT_MAX_RUN_SPEED;
m_player->acceleration() = DEFAULT_ACCELERATION;
m_player->deceleration() = DEFAULT_DECELERATION;
m_player->g() = DEFAULT_G;
m_player->change_mode(GameMode::CREATIVE);
m_player->gait() = Gait::WALK;
m_player_profile.game_mode = 0;
m_player_profile.gait = 0;
}
if (m_player->gait() == Gait::WALK) {
m_player_profile.gait = 0;
} else {
m_player_profile.gait = 1;
}
ImGui::EndTabItem();
}
}
void DevPanel::update_player_profile() {
if (!m_player) {
Logger::error("Player is Nullptr");
ASSERT(false);
return;
}
m_player_profile.gait = std::to_underlying(m_player->gait());
m_player_profile.game_mode = std::to_underlying(m_player->game_mode());
}
}