From abf1f4db7dd897a866d0b1769938aca007b7072f Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Tue, 4 Aug 2026 18:00:08 +0800 Subject: [PATCH] feat: add pig spawn egg item Add pig spawn egg asset and texture, parse spawn egg item kind and creature property, and spawn the configured entity when used on an empty block. Also fix item texture loading to use actual image dimensions and update selected item UI sizing. --- assets/item/pig_spawn_egg.json | 8 ++++++++ .../texture/item/spawn_egg/pig_spawn_egg.png | Bin 0 -> 174 bytes include/Cubed/gameplay/item.hpp | 2 +- src/gameplay/item_manager.cpp | 8 +++++++- src/gameplay/local_player.cpp | 8 ++++++++ src/texture_manager.cpp | 5 ++--- src/ui/inventory_ui.cpp | 17 ++++++++++++----- 7 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 assets/item/pig_spawn_egg.json create mode 100644 assets/texture/item/spawn_egg/pig_spawn_egg.png diff --git a/assets/item/pig_spawn_egg.json b/assets/item/pig_spawn_egg.json new file mode 100644 index 0000000..6df5ead --- /dev/null +++ b/assets/item/pig_spawn_egg.json @@ -0,0 +1,8 @@ +{ + "id": 10, + "name": "pig_spawn_egg", + "description": "", + "texture": "texture/item/spawn_egg/pig_spawn_egg.png", + "type": "spawn_egg", + "creature": "cubed:pig" +} \ No newline at end of file diff --git a/assets/texture/item/spawn_egg/pig_spawn_egg.png b/assets/texture/item/spawn_egg/pig_spawn_egg.png new file mode 100644 index 0000000000000000000000000000000000000000..2688620a9e837a2a91a260529b56b253e6a35f82 GIT binary patch literal 174 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|GCW-zLo9le z6C^SYba+3yew6wD#Gg68_#&<*iMm|xIWNG&7&EI-@=%NK#iw_7cM2UYU{t)lac2|j z?56Ve{=};qDcP-xZCANpbZyL3$za&Xu$=M4y#r5G*second.name, a->first)) { diff --git a/src/gameplay/local_player.cpp b/src/gameplay/local_player.cpp index 6e51387..702b999 100644 --- a/src/gameplay/local_player.cpp +++ b/src/gameplay/local_player.cpp @@ -424,6 +424,14 @@ void LocalPlayer::place_block(float dt) { } } } + if (data.kind == ItemKind::SPAWN_EGG) { + auto* name = std::get_if(&data.property); + ASSERT(name); + glm::ivec3 near_pos = m_look_block->pos + m_look_block->normal; + if (!m_world.is_solid(near_pos)) { + m_world.entity_manager().create(*name, near_pos); + } + } } } diff --git a/src/texture_manager.cpp b/src/texture_manager.cpp index d42f4e6..c49d7a8 100644 --- a/src/texture_manager.cpp +++ b/src/texture_manager.cpp @@ -11,7 +11,6 @@ namespace { constexpr int BLOCK_SIZE = 16; constexpr int BLOCK_NORMAL_SIZE = 128; constexpr int CROSS_PLANE_SIZE = 16; -constexpr int BLOCK_ITEM_SIZE = 512; constexpr int BLOCK_STATUS_SIZE = 16; constexpr int SKIN_SIZE = 64; @@ -133,8 +132,8 @@ void TextureManager::init_item_texture() { std::unique_ptr texture = std::make_unique(TextureType::TEXTURE_2D); texture->tex_image_2d(TextureFormat::RGBA8, TextureFormat::RGBA, - GL_UNSIGNED_BYTE, data.data, BLOCK_ITEM_SIZE, - BLOCK_ITEM_SIZE); + GL_UNSIGNED_BYTE, data.data, data.width, + data.height); texture->set_nearest(); diff --git a/src/ui/inventory_ui.cpp b/src/ui/inventory_ui.cpp index 282522c..8ab7d60 100644 --- a/src/ui/inventory_ui.cpp +++ b/src/ui/inventory_ui.cpp @@ -6,6 +6,11 @@ #include "Cubed/scene/world_scene.hpp" #include "Cubed/ui/column_layout.hpp" #include "Cubed/ui/image.hpp" + +namespace { +constexpr int SELECTED_ITEM_SIZE = 128; +} + namespace Cubed { InventoryUI::InventoryUI(WorldScene& scene) : m_scene(scene) {} @@ -80,8 +85,10 @@ void InventoryUI::init() { } { auto& image = back->add_child(); - image.set_texture(nullptr, true); - image.set_scale(0.25f); + + image.set_width(SELECTED_ITEM_SIZE); + image.set_height(SELECTED_ITEM_SIZE); + image.set_anchor(Anchor::FOLLOW_MOUSE).set_offset({0, 0}); image.set_visible(false); m_selected_image = ℑ @@ -137,7 +144,7 @@ bool InventoryUI::handle_mouse_button_event(const MouseButtonEvent& e) { if (m_selected_id != 0) { auto it = item_textures.find(m_selected_id); ASSERT(it != item_textures.end()); - m_selected_image->set_texture(it->second.get(), true); + m_selected_image->set_texture(it->second.get(), false); m_selected_image->set_visible(true); return true; } @@ -150,7 +157,7 @@ bool InventoryUI::handle_mouse_button_event(const MouseButtonEvent& e) { if (m_selected_id != 0) { auto it = item_textures.find(m_selected_id); ASSERT(it != item_textures.end()); - m_selected_image->set_texture(it->second.get(), true); + m_selected_image->set_texture(it->second.get(), false); m_hotbar[pos]->set_item(0, nullptr); auto& player = m_scene.client_world().get_player(); player.set_hotbar(pos, {0, 0}); @@ -161,7 +168,7 @@ bool InventoryUI::handle_mouse_button_event(const MouseButtonEvent& e) { } } if (m_selected_image->has_texture()) { - m_selected_image->set_texture(nullptr, true); + m_selected_image->set_texture(nullptr, false); m_selected_image->set_visible(false); auto [slot, pos] = get_hovered_hotbar_slot(); if (slot) {