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 0000000..2688620 Binary files /dev/null and b/assets/texture/item/spawn_egg/pig_spawn_egg.png differ diff --git a/include/Cubed/gameplay/item.hpp b/include/Cubed/gameplay/item.hpp index ae590fe..3a329ca 100644 --- a/include/Cubed/gameplay/item.hpp +++ b/include/Cubed/gameplay/item.hpp @@ -29,7 +29,7 @@ inline constexpr ItemKind get_item_kind(std::string_view kind) { if (kind == "block") { return ItemKind::BLOCK; } - if (kind == "SPAWN_EGG") { + if (kind == "spawn_egg") { return ItemKind::SPAWN_EGG; } return ItemKind::NONE; diff --git a/src/gameplay/item_manager.cpp b/src/gameplay/item_manager.cpp index 20280ec..ec33bf1 100644 --- a/src/gameplay/item_manager.cpp +++ b/src/gameplay/item_manager.cpp @@ -69,7 +69,13 @@ void ItemManager::add(const std::filesystem::path& path) { std::string type = doc["type"].GetString(); data.kind = get_item_kind(type); } - + if (data.kind == ItemKind::SPAWN_EGG) { + if (doc.HasMember("creature")) { + data.property = doc["creature"].GetString(); + } else { + data.property = "cubed:pig"; + } + } acc a; if (m_map.emplace(a, data.id, std::move(data))) { if (!m_id_map.emplace(a->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) {