feat: add hotbar system with ItemStack and RowLayout

This commit is contained in:
2026-07-21 14:51:03 +08:00
parent 51f36e3dfc
commit 3b529cb9a4
22 changed files with 214 additions and 55 deletions

View File

@@ -5,6 +5,7 @@
#include "Cubed/gameplay/chunk_pos.hpp"
#include "Cubed/gameplay/game_mode.hpp"
#include "Cubed/gameplay/game_time.hpp"
#include "Cubed/gameplay/item_stack.hpp"
#include "Cubed/gameplay/player.hpp"
#include "Cubed/input/event.hpp"
#include "Cubed/input/input.hpp"
@@ -18,6 +19,7 @@ namespace Cubed {
class ClientWorld;
class ClientPlayer {
public:
static constexpr size_t HOTBAR_SUM = 10;
static constexpr float WALK_SOUND_INTERVAL = 0.45f;
static constexpr float RUN_SOUND_INTERVAL = 0.3f;
using ChunkPosSet = absl::flat_hash_set<ChunkPos, ChunkPos::Hash>;
@@ -47,7 +49,6 @@ public:
void change_mode(GameMode mode);
void reload_config();
void set_player_pos(const glm::vec3& pos);
void set_place_block(unsigned id);
void update(float delta_time);
float& max_walk_speed();
@@ -58,7 +59,7 @@ public:
float& g();
float& fly_y_speed();
unsigned get_current_block() const;
const ItemStack& get_current_itemstack() const;
void set_gait(Gait gait);
GameMode& game_mode();
@@ -82,6 +83,8 @@ public:
void set_underwater(bool u);
void place_block(float dt);
std::span<const ItemStack, HOTBAR_SUM> get_hotbar() const;
private:
using enum GameMode;
float m_max_walk_speed = DEFAULT_MAX_WALK_SPEED;
@@ -89,12 +92,13 @@ private:
float m_acceleration = DEFAULT_ACCELERATION;
float m_deceleration = DEFAULT_DECELERATION;
float m_g = DEFAULT_G;
constexpr static float MAX_SPACE_ON_TIME = 0.3f;
constexpr static float PLACE_BLOCK_INTERVAL = 0.2f;
static constexpr float MAX_SPACE_ON_TIME = 0.3f;
static constexpr float PLACE_BLOCK_INTERVAL = 0.2f;
float m_place_time = PLACE_BLOCK_INTERVAL;
std::atomic<float> m_yaw = 0.0f;
std::atomic<float> m_pitch = 0.0f;
std::array<ItemStack, HOTBAR_SUM> m_hotbar;
float m_sensitivity = 0.15f;
float m_max_speed = m_max_walk_speed;
@@ -108,7 +112,7 @@ private:
float m_xz_speed = 0.0f;
unsigned m_place_block = 1;
int m_current_hotbar = 0;
bool m_moving = false;
bool m_sprinting = false;

View File

@@ -0,0 +1,8 @@
#pragma once
#include "Cubed/gameplay/block.hpp"
namespace Cubed {
struct ItemStack {
BlockType type = 0;
size_t sum = 0;
};
} // namespace Cubed

View File

@@ -48,7 +48,7 @@ public:
const Texture* get_cross_plane_array() const;
const Texture* get_image_texture(const std::string& path);
const Texture* get_pbr_texture() const;
const std::vector<std::unique_ptr<Texture>>& item_textures() const;
const std::vector<std::unique_ptr<Texture>>& get_item_textures() const;
const Texture* get_skin() const;
void init_texture();

View File

@@ -12,9 +12,6 @@ public:
ColumnLayout& operator=(ColumnLayout&&) = delete;
ColumnLayout(Widget* parent);
~ColumnLayout();
void update(float dt) override;
float width() const override;
float height() const override;
@@ -29,5 +26,7 @@ private:
float m_content_height = 0.0f;
float m_content_width = 0.0f;
ColumnLayoutAnchor m_layout_anchor = ColumnLayoutAnchor::CENTER;
void on_update(float dt) override;
};
} // namespace Cubed

View File

@@ -13,8 +13,13 @@ public:
Image& operator=(const Image&) = delete;
Image& operator=(Image&&) = delete;
Image(Widget* parent);
Image& set_image(const std::string& path, TextureManager& texture_manager);
// If you need to set width and height to the material's size, pass true,
// If you set fill parent, please pass false.
Image& set_image(const std::string& path, TextureManager& texture_manager,
bool change_size);
// If you need to set width and height to the material's size, pass true
// If you set fill parent, please pass false.
Image& set_texture(const Texture* texture, bool change_size);
float width() const override;
float height() const override;
const Texture* texture() const;

View File

@@ -0,0 +1,22 @@
#pragma once
#include "Cubed/ui/widget.hpp"
namespace Cubed {
class RowLayout : public Widget {
public:
RowLayout(Widget* parent);
~RowLayout();
float width() const override;
float height() const override;
RowLayout& set_spacing(int spacing);
void layout();
private:
int m_spacing = 0;
float m_content_height = 0.0f;
float m_content_width = 0.0f;
void on_update(float dt) override;
};
} // namespace Cubed

View File

@@ -2,6 +2,7 @@
#include "Cubed/ui/chat_box.hpp"
#include "Cubed/ui/image.hpp"
#include "Cubed/ui/row_layout.hpp"
#include "Cubed/ui/ui_manager.hpp"
#include "Cubed/ui/widget.hpp"
@@ -26,10 +27,14 @@ public:
private:
bool handle_key_event(const KeyEvent& e) override;
void update_hotbar();
WorldScene& m_scene;
ChatBox* m_chat_box = nullptr;
Image* m_mircophone = nullptr;
Label* m_disbable_voice = nullptr;
RowLayout* m_hotbar = nullptr;
std::vector<Image*> m_hotbar_items;
};
} // namespace Cubed