From b5739c678f217030607867f415659e718e16d539 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 9 Jan 2024 00:49:17 +0300 Subject: [PATCH] Items intruduced (part II) --- src/content/Content.cpp | 32 ++++++++++++++++++------------ src/content/Content.h | 3 ++- src/content/ContentLoader.cpp | 4 +++- src/definitions.cpp | 3 ++- src/files/WorldFiles.cpp | 3 +-- src/frontend/InventoryView.cpp | 2 +- src/frontend/WorldRenderer.cpp | 2 +- src/frontend/hud.cpp | 18 ++++++++++++----- src/{content => items}/ItemDef.cpp | 0 src/{content => items}/ItemDef.h | 8 +++----- src/logic/PlayerController.cpp | 17 +++++++++------- src/logic/scripting/scripting.cpp | 2 +- src/objects/Player.cpp | 2 +- src/objects/Player.h | 2 +- src/voxels/Block.h | 4 ++++ 15 files changed, 62 insertions(+), 40 deletions(-) rename src/{content => items}/ItemDef.cpp (100%) rename src/{content => items}/ItemDef.h (85%) diff --git a/src/content/Content.cpp b/src/content/Content.cpp index 50a74cb0..b8708183 100644 --- a/src/content/Content.cpp +++ b/src/content/Content.cpp @@ -1,14 +1,11 @@ #include "Content.h" +#include #include #include #include "../voxels/Block.h" -#include "../content/ItemDef.h" - -using glm::vec3; -using std::string; -using std::unordered_map; +#include "../items/ItemDef.h" ContentBuilder::~ContentBuilder() { } @@ -69,7 +66,7 @@ contenttype ContentBuilder::checkContentType(std::string id) { Content* ContentBuilder::build() { std::vector blockDefsIndices; DrawGroups* groups = new DrawGroups; - for (const string& name : blockIds) { + for (const std::string& name : blockIds) { Block* def = blockDefs[name]; // Generating runtime info @@ -93,7 +90,7 @@ Content* ContentBuilder::build() { } std::vector itemDefsIndices; - for (const string& name : itemIds) { + for (const std::string& name : itemIds) { ItemDef* def = itemDefs[name]; // Generating runtime info @@ -102,7 +99,14 @@ Content* ContentBuilder::build() { } auto indices = new ContentIndices(blockDefsIndices, itemDefsIndices); - return new Content(indices, groups, blockDefs); + std::unique_ptr content (new Content(indices, groups, blockDefs, itemDefs)); + + // Now, it's time to solve foreign keys + for (Block* def : blockDefsIndices) { + def->rt.pickingItem = content->requireItem(def->pickingItem)->rt.id; + } + + return content.release(); } ContentIndices::ContentIndices( @@ -113,8 +117,10 @@ ContentIndices::ContentIndices( } Content::Content(ContentIndices* indices, DrawGroups* drawGroups, - unordered_map blockDefs) + std::unordered_map blockDefs, + std::unordered_map itemDefs) : blockDefs(blockDefs), + itemDefs(itemDefs), indices(indices), drawGroups(drawGroups) { } @@ -124,7 +130,7 @@ Content::~Content() { delete drawGroups; } -Block* Content::findBlock(string id) const { +Block* Content::findBlock(std::string id) const { auto found = blockDefs.find(id); if (found == blockDefs.end()) { return nullptr; @@ -132,7 +138,7 @@ Block* Content::findBlock(string id) const { return found->second; } -Block* Content::requireBlock(string id) const { +Block* Content::requireBlock(std::string id) const { auto found = blockDefs.find(id); if (found == blockDefs.end()) { throw std::runtime_error("missing block "+id); @@ -140,7 +146,7 @@ Block* Content::requireBlock(string id) const { return found->second; } -ItemDef* Content::findItem(string id) const { +ItemDef* Content::findItem(std::string id) const { auto found = itemDefs.find(id); if (found == itemDefs.end()) { return nullptr; @@ -148,7 +154,7 @@ ItemDef* Content::findItem(string id) const { return found->second; } -ItemDef* Content::requireItem(string id) const { +ItemDef* Content::requireItem(std::string id) const { auto found = itemDefs.find(id); if (found == itemDefs.end()) { throw std::runtime_error("missing item "+id); diff --git a/src/content/Content.h b/src/content/Content.h index 6748dc19..db9aae43 100644 --- a/src/content/Content.h +++ b/src/content/Content.h @@ -97,7 +97,8 @@ public: DrawGroups* const drawGroups; Content(ContentIndices* indices, DrawGroups* drawGroups, - std::unordered_map blockDefs); + std::unordered_map blockDefs, + std::unordered_map itemDefs); ~Content(); Block* findBlock(std::string id) const; diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index ad2cd0f3..852b2829 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -7,7 +7,7 @@ #include #include "Content.h" -#include "ItemDef.h" +#include "../items/ItemDef.h" #include "../util/listutil.h" #include "../voxels/Block.h" #include "../files/files.h" @@ -175,6 +175,8 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) { root->flag("hidden", def->hidden); root->flag("sky-light-passing", def->skyLightPassing); root->num("draw-group", def->drawGroup); + + root->str("picking-item", def->pickingItem); } void ContentLoader::loadItem(ItemDef* def, std::string name, std::filesystem::path file) { diff --git a/src/definitions.cpp b/src/definitions.cpp index 63da9e79..a9243ee4 100644 --- a/src/definitions.cpp +++ b/src/definitions.cpp @@ -2,7 +2,7 @@ #include -#include "content/ItemDef.h" +#include "items/ItemDef.h" #include "content/Content.h" #include "window/Window.h" #include "window/Events.h" @@ -21,6 +21,7 @@ void setup_definitions(ContentBuilder* builder) { // Strange function, need to R block->obstacle = false; block->selectable = false; block->model = BlockModel::none; + block->pickingItem = "core:empty"; builder->add(block); ItemDef* item = builder->createItem("core:empty"); diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 254e3188..3896a67e 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -17,8 +17,7 @@ #include "../util/data_io.h" #include "../coders/json.h" #include "../constants.h" - -#include "../content/ItemDef.h" +#include "../items/ItemDef.h" #include #include diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index 26e545b6..30ad6a17 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -10,7 +10,7 @@ #include "../graphics/Batch2D.h" #include "../graphics/GfxContext.h" #include "../content/Content.h" -#include "../content/ItemDef.h" +#include "../items/ItemDef.h" #include "../maths/voxmaths.h" #include "../objects/Player.h" #include "../voxels/Block.h" diff --git a/src/frontend/WorldRenderer.cpp b/src/frontend/WorldRenderer.cpp index 179f9eb8..d2f450dd 100644 --- a/src/frontend/WorldRenderer.cpp +++ b/src/frontend/WorldRenderer.cpp @@ -168,7 +168,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){ shader->uniform3f("u_cameraPos", camera->position); shader->uniform1i("u_cubemap", 1); { - blockid_t id = level->player->chosenBlock; + blockid_t id = level->player->chosenItem; Block* block = contentIds->getBlockDef(id); assert(block != nullptr); float multiplier = 0.5f; diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 146210b7..eb55e13b 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -42,6 +42,7 @@ #include "LevelFrontend.h" #include "../engine.h" #include "../core_defs.h" +#include "../items/ItemDef.h" using glm::vec2; using glm::vec3; @@ -188,7 +189,7 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend) } contentAccess.reset(new InventoryView(8, content, frontend, items)); contentAccess->setSlotConsumer([=](blockid_t id) { - level->player->chosenBlock = id; + level->player->chosenItem = id; }); uicamera = new Camera(vec3(), 1); @@ -281,10 +282,17 @@ void HudRenderer::draw(const GfxContext& ctx){ GfxContext subctx = ctx.sub(); subctx.depthTest(true); subctx.cullFace(true); - - Block* cblock = contentIds->getBlockDef(player->chosenBlock); - assert(cblock != nullptr); - blocksPreview->draw(cblock, width - 56, uicamera->getFov() - 56, 48, vec4(1.0f)); + + ItemDef* item = contentIds->getItemDef(player->chosenItem); + switch (item->iconType) { + case item_icon_type::block: { + Block* cblock = content->findBlock(item->icon); + assert(cblock != nullptr); + blocksPreview->draw(cblock, width - 56, uicamera->getFov() - 56, 48, vec4(1.0f)); + break; + } + // TODO: handle other types + } } uishader->use(); batch->begin(); diff --git a/src/content/ItemDef.cpp b/src/items/ItemDef.cpp similarity index 100% rename from src/content/ItemDef.cpp rename to src/items/ItemDef.cpp diff --git a/src/content/ItemDef.h b/src/items/ItemDef.h similarity index 85% rename from src/content/ItemDef.h rename to src/items/ItemDef.h index a3f1f36c..54f0149e 100644 --- a/src/content/ItemDef.h +++ b/src/items/ItemDef.h @@ -1,5 +1,5 @@ -#ifndef CONTENT_ITEM_DEF_H_ -#define CONTENT_ITEM_DEF_H_ +#ifndef CONTENT_ITEMS_ITEM_DEF_H_ +#define CONTENT_ITEMS_ITEM_DEF_H_ #include #include @@ -7,8 +7,6 @@ #include "../graphics/UVRegion.h" #include "../typedefs.h" -#define BLOCK_ITEM_SUFFIX ".item" - struct item_funcs_set { bool init: 1; }; @@ -39,4 +37,4 @@ public: ItemDef(std::string name); }; -#endif //CONTENT_ITEM_DEF_H_ +#endif //CONTENT_ITEMS_ITEM_DEF_H_ diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 5bf1c785..8ef2be5d 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -12,6 +12,7 @@ #include "../window/Camera.h" #include "../window/Events.h" #include "../window/input.h" +#include "../items/ItemDef.h" #include "scripting/scripting.h" #include "BlocksController.h" @@ -174,7 +175,7 @@ void PlayerController::updateKeyboard() { // block choice for (int i = 1; i < 10; i++){ if (Events::jpressed(keycode::NUM_0+i)){ - player->chosenBlock = i; + player->chosenItem = i; } } } @@ -238,8 +239,10 @@ void PlayerController::updateInteraction(){ int z = iend.z; uint8_t states = 0; - Block* def = contentIds->getBlockDef(player->chosenBlock); - if (def->rotatable){ + ItemDef* item = contentIds->getItemDef(player->chosenItem); + + Block* def = level->content->findBlock(item->placingBlock); + if (def && def->rotatable){ const std::string& name = def->rotations.name; if (name == "pipe") { if (norm.x < 0.0f) states = BLOCK_DIR_WEST; @@ -265,7 +268,7 @@ void PlayerController::updateInteraction(){ if (lclick && block->breakable){ blocksController->breakBlock(player, block, x, y, z); } - if (rclick){ + if (def && rclick){ if (!input.shift && block->rt.funcsset.oninteract) { scripting::on_block_interact(player, block, x, y, z); return; @@ -276,11 +279,10 @@ void PlayerController::updateInteraction(){ z = (iend.z)+(norm.z); } vox = chunks->get(x, y, z); - int chosenBlock = player->chosenBlock; + blockid_t chosenBlock = def->rt.id; if (vox && (block = contentIds->getBlockDef(vox->id))->replaceable) { if (!level->physics->isBlockInside(x,y,z, player->hitbox) || !def->obstacle){ - Block* def = contentIds->getBlockDef(chosenBlock); if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) { chosenBlock = 0; } @@ -296,7 +298,8 @@ void PlayerController::updateInteraction(){ } } if (Events::jactive(BIND_PLAYER_PICK)){ - player->chosenBlock = chunks->get(x,y,z)->id; + Block* block = contentIds->getBlockDef(chunks->get(x,y,z)->id); + player->chosenItem = block->rt.pickingItem; } } else { selectedBlockId = -1; diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index d88c5d60..17bbd863 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -9,7 +9,7 @@ #include "../../util/timeutil.h" #include "../../world/Level.h" #include "../../voxels/Block.h" -#include "../../content/ItemDef.h" +#include "../../items/ItemDef.h" #include "api_lua.h" using namespace scripting; diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 18397709..5d104117 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -18,7 +18,7 @@ const float JUMP_FORCE = 8.0f; Player::Player(glm::vec3 position, float speed) : speed(speed), - chosenBlock(1) { + chosenItem(1) { camera = new Camera(position, glm::radians(90.0f)); currentViewCamera = camera; SPCamera = new Camera(position, glm::radians(90.0f)); diff --git a/src/objects/Player.h b/src/objects/Player.h index 7c90c36b..0f5eda0d 100644 --- a/src/objects/Player.h +++ b/src/objects/Player.h @@ -35,7 +35,7 @@ public: bool flight = false; bool noclip = false; bool debug = false; - int chosenBlock; + int chosenItem; voxel selectedVoxel {0, 0}; glm::vec2 cam = {}; diff --git a/src/voxels/Block.h b/src/voxels/Block.h index d32c30e1..36ba498e 100644 --- a/src/voxels/Block.h +++ b/src/voxels/Block.h @@ -9,6 +9,8 @@ #include "../maths/aabb.h" #include "../typedefs.h" +#define BLOCK_ITEM_SUFFIX ".item" + const uint FACE_MX = 0; const uint FACE_PX = 1; const uint FACE_MY = 2; @@ -89,6 +91,7 @@ public: bool hidden = false; AABB hitbox; BlockRotProfile rotations; + std::string pickingItem = name+BLOCK_ITEM_SUFFIX; struct { blockid_t id; @@ -96,6 +99,7 @@ public: bool emissive = false; AABB hitboxes[BlockRotProfile::MAX_COUNT]; block_funcs_set funcsset {}; + itemid_t pickingItem = 0; } rt; Block(std::string name);