mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
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.
This commit is contained in:
8
assets/item/pig_spawn_egg.json
Normal file
8
assets/item/pig_spawn_egg.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
BIN
assets/texture/item/spawn_egg/pig_spawn_egg.png
Normal file
BIN
assets/texture/item/spawn_egg/pig_spawn_egg.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 174 B |
@@ -29,7 +29,7 @@ inline constexpr ItemKind get_item_kind(std::string_view kind) {
|
|||||||
if (kind == "block") {
|
if (kind == "block") {
|
||||||
return ItemKind::BLOCK;
|
return ItemKind::BLOCK;
|
||||||
}
|
}
|
||||||
if (kind == "SPAWN_EGG") {
|
if (kind == "spawn_egg") {
|
||||||
return ItemKind::SPAWN_EGG;
|
return ItemKind::SPAWN_EGG;
|
||||||
}
|
}
|
||||||
return ItemKind::NONE;
|
return ItemKind::NONE;
|
||||||
|
|||||||
@@ -69,7 +69,13 @@ void ItemManager::add(const std::filesystem::path& path) {
|
|||||||
std::string type = doc["type"].GetString();
|
std::string type = doc["type"].GetString();
|
||||||
data.kind = get_item_kind(type);
|
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;
|
acc a;
|
||||||
if (m_map.emplace(a, data.id, std::move(data))) {
|
if (m_map.emplace(a, data.id, std::move(data))) {
|
||||||
if (!m_id_map.emplace(a->second.name, a->first)) {
|
if (!m_id_map.emplace(a->second.name, a->first)) {
|
||||||
|
|||||||
@@ -424,6 +424,14 @@ void LocalPlayer::place_block(float dt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (data.kind == ItemKind::SPAWN_EGG) {
|
||||||
|
auto* name = std::get_if<std::string>(&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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ namespace {
|
|||||||
constexpr int BLOCK_SIZE = 16;
|
constexpr int BLOCK_SIZE = 16;
|
||||||
constexpr int BLOCK_NORMAL_SIZE = 128;
|
constexpr int BLOCK_NORMAL_SIZE = 128;
|
||||||
constexpr int CROSS_PLANE_SIZE = 16;
|
constexpr int CROSS_PLANE_SIZE = 16;
|
||||||
constexpr int BLOCK_ITEM_SIZE = 512;
|
|
||||||
constexpr int BLOCK_STATUS_SIZE = 16;
|
constexpr int BLOCK_STATUS_SIZE = 16;
|
||||||
constexpr int SKIN_SIZE = 64;
|
constexpr int SKIN_SIZE = 64;
|
||||||
|
|
||||||
@@ -133,8 +132,8 @@ void TextureManager::init_item_texture() {
|
|||||||
std::unique_ptr<Texture> texture =
|
std::unique_ptr<Texture> texture =
|
||||||
std::make_unique<Texture>(TextureType::TEXTURE_2D);
|
std::make_unique<Texture>(TextureType::TEXTURE_2D);
|
||||||
texture->tex_image_2d(TextureFormat::RGBA8, TextureFormat::RGBA,
|
texture->tex_image_2d(TextureFormat::RGBA8, TextureFormat::RGBA,
|
||||||
GL_UNSIGNED_BYTE, data.data, BLOCK_ITEM_SIZE,
|
GL_UNSIGNED_BYTE, data.data, data.width,
|
||||||
BLOCK_ITEM_SIZE);
|
data.height);
|
||||||
|
|
||||||
texture->set_nearest();
|
texture->set_nearest();
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,11 @@
|
|||||||
#include "Cubed/scene/world_scene.hpp"
|
#include "Cubed/scene/world_scene.hpp"
|
||||||
#include "Cubed/ui/column_layout.hpp"
|
#include "Cubed/ui/column_layout.hpp"
|
||||||
#include "Cubed/ui/image.hpp"
|
#include "Cubed/ui/image.hpp"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
constexpr int SELECTED_ITEM_SIZE = 128;
|
||||||
|
}
|
||||||
|
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
InventoryUI::InventoryUI(WorldScene& scene) : m_scene(scene) {}
|
InventoryUI::InventoryUI(WorldScene& scene) : m_scene(scene) {}
|
||||||
|
|
||||||
@@ -80,8 +85,10 @@ void InventoryUI::init() {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
auto& image = back->add_child<Image>();
|
auto& image = back->add_child<Image>();
|
||||||
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_anchor(Anchor::FOLLOW_MOUSE).set_offset({0, 0});
|
||||||
image.set_visible(false);
|
image.set_visible(false);
|
||||||
m_selected_image = ℑ
|
m_selected_image = ℑ
|
||||||
@@ -137,7 +144,7 @@ bool InventoryUI::handle_mouse_button_event(const MouseButtonEvent& e) {
|
|||||||
if (m_selected_id != 0) {
|
if (m_selected_id != 0) {
|
||||||
auto it = item_textures.find(m_selected_id);
|
auto it = item_textures.find(m_selected_id);
|
||||||
ASSERT(it != item_textures.end());
|
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);
|
m_selected_image->set_visible(true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -150,7 +157,7 @@ bool InventoryUI::handle_mouse_button_event(const MouseButtonEvent& e) {
|
|||||||
if (m_selected_id != 0) {
|
if (m_selected_id != 0) {
|
||||||
auto it = item_textures.find(m_selected_id);
|
auto it = item_textures.find(m_selected_id);
|
||||||
ASSERT(it != item_textures.end());
|
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);
|
m_hotbar[pos]->set_item(0, nullptr);
|
||||||
auto& player = m_scene.client_world().get_player();
|
auto& player = m_scene.client_world().get_player();
|
||||||
player.set_hotbar(pos, {0, 0});
|
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()) {
|
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);
|
m_selected_image->set_visible(false);
|
||||||
auto [slot, pos] = get_hovered_hotbar_slot();
|
auto [slot, pos] = get_hovered_hotbar_slot();
|
||||||
if (slot) {
|
if (slot) {
|
||||||
|
|||||||
Reference in New Issue
Block a user