Items intruduced (part II)

This commit is contained in:
MihailRis 2024-01-09 00:49:17 +03:00
parent eae00dbb11
commit b5739c678f
15 changed files with 62 additions and 40 deletions

View File

@ -1,14 +1,11 @@
#include "Content.h"
#include <memory>
#include <stdexcept>
#include <glm/glm.hpp>
#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<Block*> 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<ItemDef*> 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> 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<string, Block*> blockDefs)
std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, ItemDef*> 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);

View File

@ -97,7 +97,8 @@ public:
DrawGroups* const drawGroups;
Content(ContentIndices* indices, DrawGroups* drawGroups,
std::unordered_map<std::string, Block*> blockDefs);
std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, ItemDef*> itemDefs);
~Content();
Block* findBlock(std::string id) const;

View File

@ -7,7 +7,7 @@
#include <glm/glm.hpp>
#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) {

View File

@ -2,7 +2,7 @@
#include <glm/glm.hpp>
#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");

View File

@ -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 <cassert>
#include <iostream>

View File

@ -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"

View File

@ -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;

View File

@ -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();

View File

@ -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 <string>
#include <glm/glm.hpp>
@ -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_

View File

@ -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;

View File

@ -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;

View File

@ -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));

View File

@ -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 = {};

View File

@ -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);