sprite items + tetragons format update

This commit is contained in:
MihailRis 2024-01-09 15:51:09 +03:00
parent 0216c4e00b
commit 486068b2af
5 changed files with 73 additions and 12 deletions

View File

@ -62,6 +62,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, bool allAssets) {
loader.add(ASSET_FONT, FONTS_FOLDER"/font", "normal");
}
loader.add(ASSET_ATLAS, TEXTURES_FOLDER"/blocks", "blocks");
loader.add(ASSET_ATLAS, TEXTURES_FOLDER"/items", "items");
}
const ResPaths* AssetsLoader::getPaths() const {

View File

@ -207,21 +207,33 @@ void ContentLoader::loadCustomBlockModel(Block* def, json::JObject* primitives)
/* Parse tetragon to points */
json::JArray* tgonobj = modeltetragons->arr(i);
glm::vec3 p1(tgonobj->num(0), tgonobj->num(1), tgonobj->num(2)),
p2(tgonobj->num(3), tgonobj->num(4), tgonobj->num(5)),
p3(tgonobj->num(6), tgonobj->num(7), tgonobj->num(8));
glm::vec3 p4 = p3 + (p1 - p2);
xw(tgonobj->num(3), tgonobj->num(4), tgonobj->num(5)),
yh(tgonobj->num(6), tgonobj->num(7), tgonobj->num(8));
def->modelExtraPoints.push_back(p1);
def->modelExtraPoints.push_back(p2);
def->modelExtraPoints.push_back(p3);
def->modelExtraPoints.push_back(p4);
def->modelExtraPoints.push_back(p1+xw);
def->modelExtraPoints.push_back(p1+xw+yh);
def->modelExtraPoints.push_back(p1+yh);
def->modelTextures.push_back(tgonobj->str(9));
}
}
}
void ContentLoader::loadItem(ItemDef* def, std::string name, std::filesystem::path file) {
void ContentLoader::loadItem(ItemDef* def, std::string name, fs::path file) {
std::unique_ptr<json::JObject> root(files::read_json(file));
std::string iconTypeStr = "none";
root->str("icon-type", iconTypeStr);
if (iconTypeStr == "none") {
def->iconType = item_icon_type::none;
} else if (iconTypeStr == "block") {
def->iconType = item_icon_type::block;
} else if (iconTypeStr == "sprite") {
def->iconType = item_icon_type::sprite;
} else {
std::cerr << "unknown icon type" << iconTypeStr << std::endl;
}
root->str("icon", def->icon);
root->str("placing-block", def->placingBlock);
}
void ContentLoader::loadBlock(Block* def, std::string full, std::string name) {

View File

@ -41,6 +41,10 @@ void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tin
y = height - y - 1 - 35 /* magic garbage */;
x += 2;
if (def->model == BlockModel::aabb) {
y += (1.0f - def->hitbox.size()).y * size * 0.5f;
}
glm::vec3 offset (x/float(width) * 2, y/float(height) * 2, 0.0f);
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
blockid_t id = def->rt.id;

View File

@ -6,6 +6,7 @@
#include "LevelFrontend.h"
#include "../window/Events.h"
#include "../assets/Assets.h"
#include "../graphics/Atlas.h"
#include "../graphics/Shader.h"
#include "../graphics/Batch2D.h"
#include "../graphics/GfxContext.h"
@ -77,7 +78,7 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
scroll = std::max(scroll, 0);
auto blocksPreview = frontend->getBlocksPreview();
blocksPreview->begin(&ctx->getViewport());
// todo: optimize
{
Window::clearDepth();
GfxContext subctx = ctx->sub();
@ -105,11 +106,34 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
tint = glm::vec4(1.0f);
}
switch (item->iconType) {
case item_icon_type::none:
break;
case item_icon_type::block: {
Block* cblock = content->requireBlock(item->icon);
blocksPreview->begin(&ctx->getViewport());
blocksPreview->draw(cblock, x, y, iconSize, tint);
break;
}
case item_icon_type::sprite: {
batch->begin();
uiShader->use();
size_t index = item->icon.find(':');
std::string name = item->icon.substr(index+1);
UVRegion region(0.0f, 0.0, 1.0f, 1.0f);
if (index == std::string::npos) {
batch->texture(assets->getTexture(name));
} else {
std::string atlasname = item->icon.substr(0, index);
Atlas* atlas = assets->getAtlas(atlasname);
if (atlas && atlas->has(name)) {
region = atlas->get(name);
batch->texture(atlas->getTexture());
}
}
batch->rect(x, y, 48, 48, 0, 0, 0, region, false, true, glm::vec4(1.0f));
batch->render();
break;
}
}
index++;
}

View File

@ -184,7 +184,7 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
auto content = level->content;
auto indices = content->indices;
std::vector<itemid_t> items;
for (itemid_t id = 0; id < indices->countItemDefs(); id++) {
for (itemid_t id = 1; id < indices->countItemDefs(); id++) {
items.push_back(id);
}
contentAccess.reset(new InventoryView(8, content, frontend, items));
@ -275,8 +275,7 @@ void HudRenderer::draw(const GfxContext& ctx){
batch->color = vec4(1.0f);
batch->render();
auto blocksPreview = frontend->getBlocksPreview();
blocksPreview->begin(&ctx.getViewport());
{
Window::clearDepth();
GfxContext subctx = ctx.sub();
@ -285,13 +284,34 @@ void HudRenderer::draw(const GfxContext& ctx){
ItemDef* item = contentIds->getItemDef(player->chosenItem);
switch (item->iconType) {
case item_icon_type::none:
break;
case item_icon_type::block: {
Block* cblock = content->findBlock(item->icon);
assert(cblock != nullptr);
auto blocksPreview = frontend->getBlocksPreview();
blocksPreview->begin(&ctx.getViewport());
blocksPreview->draw(cblock, width - 56, uicamera->getFov() - 56, 48, vec4(1.0f));
break;
}
// TODO: handle other types
case item_icon_type::sprite: {
size_t index = item->icon.find(':');
std::string name = item->icon.substr(index+1);
UVRegion region(0.0f, 0.0, 1.0f, 1.0f);
if (index == std::string::npos) {
batch->texture(assets->getTexture(name));
} else {
std::string atlasname = item->icon.substr(0, index);
Atlas* atlas = assets->getAtlas(atlasname);
if (atlas && atlas->has(name)) {
region = atlas->get(name);
batch->texture(atlas->getTexture());
}
}
batch->rect(width - 56, uicamera->getFov() - 56, 48, 48, 0, 0, 0, region, false, true, glm::vec4(1.0f));
batch->render();
}
}
}
uishader->use();