Items intruduced (part II)
This commit is contained in:
+19
-13
@@ -1,14 +1,11 @@
|
|||||||
#include "Content.h"
|
#include "Content.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
#include "../voxels/Block.h"
|
#include "../voxels/Block.h"
|
||||||
#include "../content/ItemDef.h"
|
#include "../items/ItemDef.h"
|
||||||
|
|
||||||
using glm::vec3;
|
|
||||||
using std::string;
|
|
||||||
using std::unordered_map;
|
|
||||||
|
|
||||||
ContentBuilder::~ContentBuilder() {
|
ContentBuilder::~ContentBuilder() {
|
||||||
}
|
}
|
||||||
@@ -69,7 +66,7 @@ contenttype ContentBuilder::checkContentType(std::string id) {
|
|||||||
Content* ContentBuilder::build() {
|
Content* ContentBuilder::build() {
|
||||||
std::vector<Block*> blockDefsIndices;
|
std::vector<Block*> blockDefsIndices;
|
||||||
DrawGroups* groups = new DrawGroups;
|
DrawGroups* groups = new DrawGroups;
|
||||||
for (const string& name : blockIds) {
|
for (const std::string& name : blockIds) {
|
||||||
Block* def = blockDefs[name];
|
Block* def = blockDefs[name];
|
||||||
|
|
||||||
// Generating runtime info
|
// Generating runtime info
|
||||||
@@ -93,7 +90,7 @@ Content* ContentBuilder::build() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<ItemDef*> itemDefsIndices;
|
std::vector<ItemDef*> itemDefsIndices;
|
||||||
for (const string& name : itemIds) {
|
for (const std::string& name : itemIds) {
|
||||||
ItemDef* def = itemDefs[name];
|
ItemDef* def = itemDefs[name];
|
||||||
|
|
||||||
// Generating runtime info
|
// Generating runtime info
|
||||||
@@ -102,7 +99,14 @@ Content* ContentBuilder::build() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto indices = new ContentIndices(blockDefsIndices, itemDefsIndices);
|
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(
|
ContentIndices::ContentIndices(
|
||||||
@@ -113,8 +117,10 @@ ContentIndices::ContentIndices(
|
|||||||
}
|
}
|
||||||
|
|
||||||
Content::Content(ContentIndices* indices, DrawGroups* drawGroups,
|
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),
|
: blockDefs(blockDefs),
|
||||||
|
itemDefs(itemDefs),
|
||||||
indices(indices),
|
indices(indices),
|
||||||
drawGroups(drawGroups) {
|
drawGroups(drawGroups) {
|
||||||
}
|
}
|
||||||
@@ -124,7 +130,7 @@ Content::~Content() {
|
|||||||
delete drawGroups;
|
delete drawGroups;
|
||||||
}
|
}
|
||||||
|
|
||||||
Block* Content::findBlock(string id) const {
|
Block* Content::findBlock(std::string id) const {
|
||||||
auto found = blockDefs.find(id);
|
auto found = blockDefs.find(id);
|
||||||
if (found == blockDefs.end()) {
|
if (found == blockDefs.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -132,7 +138,7 @@ Block* Content::findBlock(string id) const {
|
|||||||
return found->second;
|
return found->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
Block* Content::requireBlock(string id) const {
|
Block* Content::requireBlock(std::string id) const {
|
||||||
auto found = blockDefs.find(id);
|
auto found = blockDefs.find(id);
|
||||||
if (found == blockDefs.end()) {
|
if (found == blockDefs.end()) {
|
||||||
throw std::runtime_error("missing block "+id);
|
throw std::runtime_error("missing block "+id);
|
||||||
@@ -140,7 +146,7 @@ Block* Content::requireBlock(string id) const {
|
|||||||
return found->second;
|
return found->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemDef* Content::findItem(string id) const {
|
ItemDef* Content::findItem(std::string id) const {
|
||||||
auto found = itemDefs.find(id);
|
auto found = itemDefs.find(id);
|
||||||
if (found == itemDefs.end()) {
|
if (found == itemDefs.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -148,7 +154,7 @@ ItemDef* Content::findItem(string id) const {
|
|||||||
return found->second;
|
return found->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemDef* Content::requireItem(string id) const {
|
ItemDef* Content::requireItem(std::string id) const {
|
||||||
auto found = itemDefs.find(id);
|
auto found = itemDefs.find(id);
|
||||||
if (found == itemDefs.end()) {
|
if (found == itemDefs.end()) {
|
||||||
throw std::runtime_error("missing item "+id);
|
throw std::runtime_error("missing item "+id);
|
||||||
|
|||||||
@@ -97,7 +97,8 @@ public:
|
|||||||
DrawGroups* const drawGroups;
|
DrawGroups* const drawGroups;
|
||||||
|
|
||||||
Content(ContentIndices* indices, DrawGroups* 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();
|
~Content();
|
||||||
|
|
||||||
Block* findBlock(std::string id) const;
|
Block* findBlock(std::string id) const;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
#include "Content.h"
|
#include "Content.h"
|
||||||
#include "ItemDef.h"
|
#include "../items/ItemDef.h"
|
||||||
#include "../util/listutil.h"
|
#include "../util/listutil.h"
|
||||||
#include "../voxels/Block.h"
|
#include "../voxels/Block.h"
|
||||||
#include "../files/files.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("hidden", def->hidden);
|
||||||
root->flag("sky-light-passing", def->skyLightPassing);
|
root->flag("sky-light-passing", def->skyLightPassing);
|
||||||
root->num("draw-group", def->drawGroup);
|
root->num("draw-group", def->drawGroup);
|
||||||
|
|
||||||
|
root->str("picking-item", def->pickingItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContentLoader::loadItem(ItemDef* def, std::string name, std::filesystem::path file) {
|
void ContentLoader::loadItem(ItemDef* def, std::string name, std::filesystem::path file) {
|
||||||
|
|||||||
+2
-1
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
#include "content/ItemDef.h"
|
#include "items/ItemDef.h"
|
||||||
#include "content/Content.h"
|
#include "content/Content.h"
|
||||||
#include "window/Window.h"
|
#include "window/Window.h"
|
||||||
#include "window/Events.h"
|
#include "window/Events.h"
|
||||||
@@ -21,6 +21,7 @@ void setup_definitions(ContentBuilder* builder) { // Strange function, need to R
|
|||||||
block->obstacle = false;
|
block->obstacle = false;
|
||||||
block->selectable = false;
|
block->selectable = false;
|
||||||
block->model = BlockModel::none;
|
block->model = BlockModel::none;
|
||||||
|
block->pickingItem = "core:empty";
|
||||||
builder->add(block);
|
builder->add(block);
|
||||||
|
|
||||||
ItemDef* item = builder->createItem("core:empty");
|
ItemDef* item = builder->createItem("core:empty");
|
||||||
|
|||||||
@@ -17,8 +17,7 @@
|
|||||||
#include "../util/data_io.h"
|
#include "../util/data_io.h"
|
||||||
#include "../coders/json.h"
|
#include "../coders/json.h"
|
||||||
#include "../constants.h"
|
#include "../constants.h"
|
||||||
|
#include "../items/ItemDef.h"
|
||||||
#include "../content/ItemDef.h"
|
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
#include "../graphics/Batch2D.h"
|
#include "../graphics/Batch2D.h"
|
||||||
#include "../graphics/GfxContext.h"
|
#include "../graphics/GfxContext.h"
|
||||||
#include "../content/Content.h"
|
#include "../content/Content.h"
|
||||||
#include "../content/ItemDef.h"
|
#include "../items/ItemDef.h"
|
||||||
#include "../maths/voxmaths.h"
|
#include "../maths/voxmaths.h"
|
||||||
#include "../objects/Player.h"
|
#include "../objects/Player.h"
|
||||||
#include "../voxels/Block.h"
|
#include "../voxels/Block.h"
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){
|
|||||||
shader->uniform3f("u_cameraPos", camera->position);
|
shader->uniform3f("u_cameraPos", camera->position);
|
||||||
shader->uniform1i("u_cubemap", 1);
|
shader->uniform1i("u_cubemap", 1);
|
||||||
{
|
{
|
||||||
blockid_t id = level->player->chosenBlock;
|
blockid_t id = level->player->chosenItem;
|
||||||
Block* block = contentIds->getBlockDef(id);
|
Block* block = contentIds->getBlockDef(id);
|
||||||
assert(block != nullptr);
|
assert(block != nullptr);
|
||||||
float multiplier = 0.5f;
|
float multiplier = 0.5f;
|
||||||
|
|||||||
+13
-5
@@ -42,6 +42,7 @@
|
|||||||
#include "LevelFrontend.h"
|
#include "LevelFrontend.h"
|
||||||
#include "../engine.h"
|
#include "../engine.h"
|
||||||
#include "../core_defs.h"
|
#include "../core_defs.h"
|
||||||
|
#include "../items/ItemDef.h"
|
||||||
|
|
||||||
using glm::vec2;
|
using glm::vec2;
|
||||||
using glm::vec3;
|
using glm::vec3;
|
||||||
@@ -188,7 +189,7 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
|||||||
}
|
}
|
||||||
contentAccess.reset(new InventoryView(8, content, frontend, items));
|
contentAccess.reset(new InventoryView(8, content, frontend, items));
|
||||||
contentAccess->setSlotConsumer([=](blockid_t id) {
|
contentAccess->setSlotConsumer([=](blockid_t id) {
|
||||||
level->player->chosenBlock = id;
|
level->player->chosenItem = id;
|
||||||
});
|
});
|
||||||
|
|
||||||
uicamera = new Camera(vec3(), 1);
|
uicamera = new Camera(vec3(), 1);
|
||||||
@@ -281,10 +282,17 @@ void HudRenderer::draw(const GfxContext& ctx){
|
|||||||
GfxContext subctx = ctx.sub();
|
GfxContext subctx = ctx.sub();
|
||||||
subctx.depthTest(true);
|
subctx.depthTest(true);
|
||||||
subctx.cullFace(true);
|
subctx.cullFace(true);
|
||||||
|
|
||||||
Block* cblock = contentIds->getBlockDef(player->chosenBlock);
|
ItemDef* item = contentIds->getItemDef(player->chosenItem);
|
||||||
assert(cblock != nullptr);
|
switch (item->iconType) {
|
||||||
blocksPreview->draw(cblock, width - 56, uicamera->getFov() - 56, 48, vec4(1.0f));
|
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();
|
uishader->use();
|
||||||
batch->begin();
|
batch->begin();
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef CONTENT_ITEM_DEF_H_
|
#ifndef CONTENT_ITEMS_ITEM_DEF_H_
|
||||||
#define CONTENT_ITEM_DEF_H_
|
#define CONTENT_ITEMS_ITEM_DEF_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
@@ -7,8 +7,6 @@
|
|||||||
#include "../graphics/UVRegion.h"
|
#include "../graphics/UVRegion.h"
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
|
|
||||||
#define BLOCK_ITEM_SUFFIX ".item"
|
|
||||||
|
|
||||||
struct item_funcs_set {
|
struct item_funcs_set {
|
||||||
bool init: 1;
|
bool init: 1;
|
||||||
};
|
};
|
||||||
@@ -39,4 +37,4 @@ public:
|
|||||||
ItemDef(std::string name);
|
ItemDef(std::string name);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //CONTENT_ITEM_DEF_H_
|
#endif //CONTENT_ITEMS_ITEM_DEF_H_
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
#include "../window/Camera.h"
|
#include "../window/Camera.h"
|
||||||
#include "../window/Events.h"
|
#include "../window/Events.h"
|
||||||
#include "../window/input.h"
|
#include "../window/input.h"
|
||||||
|
#include "../items/ItemDef.h"
|
||||||
#include "scripting/scripting.h"
|
#include "scripting/scripting.h"
|
||||||
#include "BlocksController.h"
|
#include "BlocksController.h"
|
||||||
|
|
||||||
@@ -174,7 +175,7 @@ void PlayerController::updateKeyboard() {
|
|||||||
// block choice
|
// block choice
|
||||||
for (int i = 1; i < 10; i++){
|
for (int i = 1; i < 10; i++){
|
||||||
if (Events::jpressed(keycode::NUM_0+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;
|
int z = iend.z;
|
||||||
uint8_t states = 0;
|
uint8_t states = 0;
|
||||||
|
|
||||||
Block* def = contentIds->getBlockDef(player->chosenBlock);
|
ItemDef* item = contentIds->getItemDef(player->chosenItem);
|
||||||
if (def->rotatable){
|
|
||||||
|
Block* def = level->content->findBlock(item->placingBlock);
|
||||||
|
if (def && def->rotatable){
|
||||||
const std::string& name = def->rotations.name;
|
const std::string& name = def->rotations.name;
|
||||||
if (name == "pipe") {
|
if (name == "pipe") {
|
||||||
if (norm.x < 0.0f) states = BLOCK_DIR_WEST;
|
if (norm.x < 0.0f) states = BLOCK_DIR_WEST;
|
||||||
@@ -265,7 +268,7 @@ void PlayerController::updateInteraction(){
|
|||||||
if (lclick && block->breakable){
|
if (lclick && block->breakable){
|
||||||
blocksController->breakBlock(player, block, x, y, z);
|
blocksController->breakBlock(player, block, x, y, z);
|
||||||
}
|
}
|
||||||
if (rclick){
|
if (def && rclick){
|
||||||
if (!input.shift && block->rt.funcsset.oninteract) {
|
if (!input.shift && block->rt.funcsset.oninteract) {
|
||||||
scripting::on_block_interact(player, block, x, y, z);
|
scripting::on_block_interact(player, block, x, y, z);
|
||||||
return;
|
return;
|
||||||
@@ -276,11 +279,10 @@ void PlayerController::updateInteraction(){
|
|||||||
z = (iend.z)+(norm.z);
|
z = (iend.z)+(norm.z);
|
||||||
}
|
}
|
||||||
vox = chunks->get(x, y, 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 (vox && (block = contentIds->getBlockDef(vox->id))->replaceable) {
|
||||||
if (!level->physics->isBlockInside(x,y,z, player->hitbox)
|
if (!level->physics->isBlockInside(x,y,z, player->hitbox)
|
||||||
|| !def->obstacle){
|
|| !def->obstacle){
|
||||||
Block* def = contentIds->getBlockDef(chosenBlock);
|
|
||||||
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
|
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
|
||||||
chosenBlock = 0;
|
chosenBlock = 0;
|
||||||
}
|
}
|
||||||
@@ -296,7 +298,8 @@ void PlayerController::updateInteraction(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Events::jactive(BIND_PLAYER_PICK)){
|
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 {
|
} else {
|
||||||
selectedBlockId = -1;
|
selectedBlockId = -1;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#include "../../util/timeutil.h"
|
#include "../../util/timeutil.h"
|
||||||
#include "../../world/Level.h"
|
#include "../../world/Level.h"
|
||||||
#include "../../voxels/Block.h"
|
#include "../../voxels/Block.h"
|
||||||
#include "../../content/ItemDef.h"
|
#include "../../items/ItemDef.h"
|
||||||
#include "api_lua.h"
|
#include "api_lua.h"
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ const float JUMP_FORCE = 8.0f;
|
|||||||
|
|
||||||
Player::Player(glm::vec3 position, float speed) :
|
Player::Player(glm::vec3 position, float speed) :
|
||||||
speed(speed),
|
speed(speed),
|
||||||
chosenBlock(1) {
|
chosenItem(1) {
|
||||||
camera = new Camera(position, glm::radians(90.0f));
|
camera = new Camera(position, glm::radians(90.0f));
|
||||||
currentViewCamera = camera;
|
currentViewCamera = camera;
|
||||||
SPCamera = new Camera(position, glm::radians(90.0f));
|
SPCamera = new Camera(position, glm::radians(90.0f));
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public:
|
|||||||
bool flight = false;
|
bool flight = false;
|
||||||
bool noclip = false;
|
bool noclip = false;
|
||||||
bool debug = false;
|
bool debug = false;
|
||||||
int chosenBlock;
|
int chosenItem;
|
||||||
voxel selectedVoxel {0, 0};
|
voxel selectedVoxel {0, 0};
|
||||||
|
|
||||||
glm::vec2 cam = {};
|
glm::vec2 cam = {};
|
||||||
|
|||||||
@@ -9,6 +9,8 @@
|
|||||||
#include "../maths/aabb.h"
|
#include "../maths/aabb.h"
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
|
|
||||||
|
#define BLOCK_ITEM_SUFFIX ".item"
|
||||||
|
|
||||||
const uint FACE_MX = 0;
|
const uint FACE_MX = 0;
|
||||||
const uint FACE_PX = 1;
|
const uint FACE_PX = 1;
|
||||||
const uint FACE_MY = 2;
|
const uint FACE_MY = 2;
|
||||||
@@ -89,6 +91,7 @@ public:
|
|||||||
bool hidden = false;
|
bool hidden = false;
|
||||||
AABB hitbox;
|
AABB hitbox;
|
||||||
BlockRotProfile rotations;
|
BlockRotProfile rotations;
|
||||||
|
std::string pickingItem = name+BLOCK_ITEM_SUFFIX;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
blockid_t id;
|
blockid_t id;
|
||||||
@@ -96,6 +99,7 @@ public:
|
|||||||
bool emissive = false;
|
bool emissive = false;
|
||||||
AABB hitboxes[BlockRotProfile::MAX_COUNT];
|
AABB hitboxes[BlockRotProfile::MAX_COUNT];
|
||||||
block_funcs_set funcsset {};
|
block_funcs_set funcsset {};
|
||||||
|
itemid_t pickingItem = 0;
|
||||||
} rt;
|
} rt;
|
||||||
|
|
||||||
Block(std::string name);
|
Block(std::string name);
|
||||||
|
|||||||
Reference in New Issue
Block a user