item: add item definitions

This commit is contained in:
Lephenixnoir 2022-07-17 15:23:23 +01:00
parent 1a6cea1e19
commit 8fd89c01a9
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
11 changed files with 140 additions and 10 deletions

View File

@ -10,12 +10,14 @@ set(SOURCES
src/block.cpp
src/camera.cpp
src/game.cpp
src/item.cpp
src/main.cpp
src/render.cpp
src/world.cpp)
set(ASSETS
assets-cg/font_nooncraft.png
assets-cg/blockinfo.yaml)
assets-cg/blockinfo.yaml
assets-cg/iteminfo.yaml)
fxconv_declare_assets(${ASSETS} WITH_METADATA)
fxconv_declare_converters(converters.py)

View File

@ -1,5 +1,3 @@
# TODO: We don't have breaking times yet!
- name: "AIR"
cluster: " "
walkable: yes
@ -8,35 +6,43 @@
- name: "STONE"
cluster: "□□□□"
toolKind: Pickaxe
breakingTime: 20
- name: "COBBLESTONE"
cluster: "####"
toolKind: Pickaxe
breakingTime: 20
- name: "DIRT"
cluster: "...."
walkable: yes
toolKind: Shovel
breakingTime: 5
- name: "FLOWERS"
cluster: ".îî."
walkable: yes
breakingTime: 1
- name: "COAL ORE"
cluster: "□●●□"
toolKind: Pickaxe
breakingTime: 30
- name: "IRON ORE"
cluster: "%□□%"
toolKind: Pickaxe
breakingTime: 30
- name: "GOLD ORE"
cluster: "□**□"
toolKind: Pickaxe
breakingTime: 30
- name: "DIAMOND ORE"
cluster: "★□□★"
toolKind: Pickaxe
breakingTime: 30
- name: "WATER SOURCE"
cluster: "~~~~"
@ -45,10 +51,12 @@
- name: "OAK LOG"
cluster: "∥∥∥∥"
toolKind: Axe
breakingTime: 20
- name: "OAK PLANKS"
cluster: "===="
toolKind: Axe
breakingTime: 20
- name: "FLOWING WATER"
cluster: "∫∫∫∫"
@ -58,11 +66,14 @@
cluster: "↓..↓"
walkable: yes
toolKind: Shovel
breakingTime: 7
- name: "OAK LEAVES"
cluster: "MMMM"
toolKind: Hoe
breakingTime: 4
- name: "OAK SAPLING"
cluster: ".↥↥."
walkable: yes
breakingTime: 1

View File

@ -8,3 +8,7 @@ font_nooncraft.png:
blockinfo.yaml:
custom-type: blockinfo
name: Nooncraft_blockInfo
iteminfo.yaml:
custom-type: iteminfo
name: Nooncraft_itemInfo

11
assets-cg/iteminfo.yaml Normal file
View File

@ -0,0 +1,11 @@
- name: "COAL"
cluster: " ● "
- name: "IRON INGOT"
cluster: " % "
- name: "GOLD INGOT"
cluster: " * "
- name: "DIAMOND"
cluster: " ★ "

View File

@ -5,6 +5,8 @@ def convert(input, output, params, target):
recognized = True
if params["custom-type"] == "blockinfo":
o = convert_blockinfo(input, params)
elif params["custom-type"] == "iteminfo":
o = convert_iteminfo(input, params)
else:
recognized = False
@ -82,16 +84,38 @@ def convert_blockinfo(input, params):
# If a tool is specified, default to Breakability::ByTool
tk = b.get("toolKind", None)
br = b.get("breakability", "ByTool" if tk is not None else "Fluid")
br = b.get("breakability", "ByTool" if tk is not None else "ByAnything")
o += fxconv.string(b["name"])
o += mkGlyphCluster(b["cluster"])
o += mkBreakability(br)
o += mkToolKind(tk)
o += fxconv.u16(int(b.get("baseBreakDurationTicks", "20")))
o += fxconv.u16(int(b.get("breakingTime", "20")))
o += mkBool(b.get("walkable", False))
o += bytes(3)
o += fxconv.sym("Nooncraft_blockInfoCount")
o += fxconv.u32(len(data))
return o
def convert_iteminfo(input, params):
with open(input, "r") as fp:
data = yaml.safe_load(fp.read())
o = fxconv.ObjectData()
for i in data:
# Make sure there are no typos in the fields
assert (f in ["name", "cluster", "toolKind"] for f in i)
tk = i.get("toolKind", None)
o += fxconv.string(i["name"])
o += fxconv.u8(tk is not None)
o += mkToolKind(tk)
o += fxconv.u8(0 if tk is None else i["toolLevel"])
o += bytes(1)
o += fxconv.sym("Nooncraft_itemInfoCount")
o += fxconv.u32(len(data))
return o

View File

@ -1,12 +1,14 @@
// nooncraft.block: Block data in world map and general block information
#pragma once
#include "item.h"
#include "render.h"
#include <cstdint>
using Block = uint8_t;
#include "item.h"
#include "render.h"
enum class Breakability: uint8_t {
Fluid,
ByAnything,

View File

@ -1,5 +1,10 @@
#include "game.h"
BlockInfo *Game::pointedBlockInfo() const
{
return this->world->blockInfoAt(this->cursorPos);
}
bool Game::movePlayerBy(WorldCoord dir, bool camera_follow)
{
WorldCoord candidatePos = this->playerPos + dir;
@ -39,7 +44,7 @@ bool Game::moveCursorBy(WorldCoord dir)
int Game::blockBreakingDuration() const
{
BlockInfo *info = this->world->blockInfoAt(this->cursorPos);
BlockInfo *info = this->pointedBlockInfo();
if(!info)
return -1;

View File

@ -13,6 +13,9 @@ struct Game
WorldCoord playerPos;
WorldCoord cursorPos;
/* Info on the pointed block */
BlockInfo *pointedBlockInfo() const;
/* Current ticks spent damaging the pointed block, ≥ 0 when breaking */
int damageTicks;

27
src/item.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "item.h"
extern "C" {
extern ItemInfo Nooncraft_itemInfo[];
extern int Nooncraft_itemInfoCount;
}
namespace Nooncraft {
ItemInfo *getItemInfo(Item const &i)
{
if(i.isBlock)
return nullptr;
if(i.itemID >= Nooncraft_itemInfoCount)
return nullptr;
return &Nooncraft_itemInfo[i.itemID];
}
BlockInfo *getItemBlockInfo(Item const &i)
{
if(!i.isBlock)
return nullptr;
return Nooncraft::getBlockInfo(i.block);
}
} /* namespace Nooncraft */

View File

@ -6,3 +6,38 @@
enum class ToolKind: uint8_t {
Pickaxe, Axe, Shovel, Hoe,
};
enum class ItemType: uint8_t {
Block, Item, Tool,
};
#include "block.h"
struct ItemInfo
{
char const *name;
bool isTool;
ToolKind toolKind;
/* Wooden, Stone, Iron, Gold, Diamond */
uint8_t toolLevel;
};
struct Item
{
bool isBlock;
union {
Block block;
uint8_t itemID;
};
};
struct BlockInfo;
namespace Nooncraft {
ItemInfo *getItemInfo(Item const &i);
BlockInfo *getItemBlockInfo(Item const &i);
} /* namespace Nooncraft */

View File

@ -74,10 +74,16 @@ static void renderGame(Game *game)
int hudY = 27;
renderText(0, hudY, separator);
renderText(22, hudY+1, "HAND: COBBLESTONE");
renderFormat(46, hudY+1, "X%d Y%d", game->playerPos.x, game->playerPos.y);
renderText(22, hudY+2, "CURSOR: COBBLESTONE");
renderFormat(24, hudY+3, debugMessage);
BlockInfo *pointedBlockInfo = game->pointedBlockInfo();
char const *pointedBlockName =
pointedBlockInfo ? pointedBlockInfo->name : "(NOTHING)";
renderFormat(22, hudY+2, "CURSOR: %s", pointedBlockName);
renderFormat(22, hudY+3, debugMessage);
renderCluster(0, hudY+1, invLeft);
renderCluster(2, hudY+1, Nooncraft::getBlockInfo(2)->cluster);
renderCluster(20, hudY+1, invRight);