Add inventory.
All checks were successful
Build Dusk / run-tests (push) Successful in 2m12s
Build Dusk / build-linux (push) Successful in 1m49s
Build Dusk / build-psp (push) Successful in 1m52s

This commit is contained in:
2026-01-06 07:47:16 -06:00
parent 024ace1078
commit af5bf987c8
91 changed files with 1108 additions and 139 deletions

View File

@@ -56,8 +56,9 @@ add_subdirectory(display)
add_subdirectory(engine)
add_subdirectory(error)
add_subdirectory(input)
add_subdirectory(item)
add_subdirectory(locale)
add_subdirectory(rpg)
# add_subdirectory(rpg)
add_subdirectory(scene)
add_subdirectory(script)
add_subdirectory(thread)

View File

@@ -9,8 +9,6 @@
#include "type/assetpaletteimage.h"
#include "type/assetalphaimage.h"
#include "type/assetlanguage.h"
#include "type/assetmap.h"
#include "type/assetchunk.h"
#include "type/assetscript.h"
#include <zip.h>
@@ -20,8 +18,6 @@ typedef enum {
ASSET_TYPE_PALETTE_IMAGE,
ASSET_TYPE_ALPHA_IMAGE,
ASSET_TYPE_LANGUAGE,
ASSET_TYPE_MAP,
ASSET_TYPE_CHUNK,
ASSET_TYPE_SCRIPT,
ASSET_TYPE_COUNT,
@@ -72,19 +68,6 @@ static const assettypedef_t ASSET_TYPE_DEFINITIONS[ASSET_TYPE_COUNT] = {
.custom = assetLanguageHandler
},
[ASSET_TYPE_MAP] = {
.header = "DMF",
.loadStrategy = ASSET_LOAD_STRAT_ENTIRE,
.dataSize = sizeof(1),
.entire = assetMapLoad
},
[ASSET_TYPE_CHUNK] = {
.header = "DCF",
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
.custom = assetChunkLoad
},
[ASSET_TYPE_SCRIPT] = {
.header = "DSF",
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,

View File

@@ -9,7 +9,5 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
assetalphaimage.c
assetpaletteimage.c
assetlanguage.c
assetmap.c
assetchunk.c
assetscript.c
)

View File

@@ -1,178 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset/asset.h"
#include "assert/assert.h"
#include "rpg/entity/entity.h"
#pragma pack(push, 1)
typedef struct {
uint32_t tileCount;
uint8_t modelCount;
uint8_t entityCount;
} assetchunkheader_t;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct {
tile_t tile;
} assetchunktiledata_t;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct {
uint32_t vertexCount;
} assetchunkmodelheader_t;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct {
entitytype_t entityType;
uint8_t localX;
uint8_t localY;
uint8_t localZ;
} assetchunkentityheader_t;
#pragma pack(pop)
errorret_t assetChunkLoad(assetcustom_t custom) {
assertNotNull(custom.output, "Output pointer cannot be NULL");
assertNotNull(custom.zipFile, "Zip file pointer cannot be NULL");
chunk_t *chunk = (chunk_t *)custom.output;
assertTrue(chunk->meshCount == 0, "Chunk is not in a good state");
// Read header
assetchunkheader_t header;
size_t bytesRead = zip_fread(
custom.zipFile, &header, sizeof(assetchunkheader_t)
);
if(bytesRead != sizeof(assetchunkheader_t)) {
zip_fclose(custom.zipFile);
errorThrow("Failed to read chunk asset header.");
}
if(header.tileCount != CHUNK_TILE_COUNT) {
zip_fclose(custom.zipFile);
errorThrow(
"Chunk asset has invalid tile count: %d (expected %d).",
header.tileCount,
CHUNK_TILE_COUNT
);
}
if(header.modelCount > CHUNK_MESH_COUNT_MAX) {
zip_fclose(custom.zipFile);
errorThrow(
"Chunk asset has too many models: %d (max %d).",
header.modelCount,
CHUNK_MESH_COUNT_MAX
);
}
if(header.entityCount > CHUNK_ENTITY_COUNT_MAX) {
zip_fclose(custom.zipFile);
errorThrow(
"Chunk asset has too many entities: %d (max %d).",
header.entityCount,
CHUNK_ENTITY_COUNT_MAX
);
}
chunk->meshCount = header.modelCount;
// Read tile data
bytesRead = zip_fread(
custom.zipFile,
chunk->tiles,
sizeof(assetchunktiledata_t) * header.tileCount
);
if(bytesRead != sizeof(assetchunktiledata_t) * header.tileCount) {
zip_fclose(custom.zipFile);
errorThrow("Failed to read chunk tile data.");
}
// For each model...
uint32_t vertexIndex = 0;
for(uint8_t i = 0; i < header.modelCount; i++) {
assetchunkmodelheader_t modelHeader;
bytesRead = zip_fread(
custom.zipFile, &modelHeader, sizeof(assetchunkmodelheader_t)
);
if(bytesRead != sizeof(assetchunkmodelheader_t)) {
zip_fclose(custom.zipFile);
errorThrow("Failed to read chunk model header.");
}
if(
vertexIndex + modelHeader.vertexCount >
CHUNK_VERTEX_COUNT_MAX
) {
zip_fclose(custom.zipFile);
errorThrow("Chunk model vertex count exceeds maximum.");
}
// Read vertex data.
bytesRead = zip_fread(
custom.zipFile,
&chunk->vertices[vertexIndex],
sizeof(meshvertex_t) * modelHeader.vertexCount
);
if(bytesRead != sizeof(meshvertex_t) * modelHeader.vertexCount) {
zip_fclose(custom.zipFile);
errorThrow("Failed to read chunk model vertex data.");
}
// Init the mesh
if(modelHeader.vertexCount > 0) {
mesh_t *mesh = &chunk->meshes[i];
meshInit(
mesh,
MESH_PRIMITIVE_TRIANGLES,
modelHeader.vertexCount,
&chunk->vertices[vertexIndex]
);
vertexIndex += modelHeader.vertexCount;
} else {
chunk->meshes[i].vertexCount = 0;
}
}
// Read entity data
for(uint8_t i = 0; i < header.entityCount; i++) {
assetchunkentityheader_t entityHeader;
bytesRead = zip_fread(
custom.zipFile, &entityHeader, sizeof(assetchunkentityheader_t)
);
if(bytesRead != sizeof(assetchunkentityheader_t)) {
zip_fclose(custom.zipFile);
errorThrow("Failed to read chunk entity header.");
}
uint8_t entityIndex = entityGetAvailable();
if(entityIndex == 0xFF) {
zip_fclose(custom.zipFile);
errorThrow("No available entity slots.");
}
entity_t *entity = &ENTITIES[entityIndex];
entityInit(entity, (entitytype_t)entityHeader.entityType);
entity->position.x = (
(chunk->position.x * CHUNK_WIDTH) + entityHeader.localX
);
entity->position.y = (
(chunk->position.y * CHUNK_HEIGHT) + entityHeader.localY
);
entity->position.z = (
(chunk->position.z * CHUNK_DEPTH) + entityHeader.localZ
);
chunk->entities[i] = entityIndex;
}
errorOk();
}

View File

@@ -1,20 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include "rpg/world/chunk.h"
typedef struct assetcustom_s assetcustom_t;
/**
* Handles loading of chunk data from a chunk asset file.
*
* @param custom The custom asset loading parameters.
* @return An error code.
*/
errorret_t assetChunkLoad(assetcustom_t custom);

View File

@@ -1,19 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset/asset.h"
#include "assert/assert.h"
#include "util/memory.h"
errorret_t assetMapLoad(void *data, void *output) {
assertNotNull(data, "Data cannot be NULL");
assertNotNull(output, "Output cannot be NULL");
assertUnreachable("map not finished");
errorOk();
}

View File

@@ -1,20 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include "rpg/world/map.h"
#include "display/mesh/mesh.h"
/**
* Loads a map asset from the given data pointer into the output map structure.
*
* @param data Pointer to the raw assetmap_t data.
* @param output Pointer to the map_t to load the map into.
* @return An error code.
*/
errorret_t assetMapLoad(void *data, void *output);

View File

@@ -45,7 +45,7 @@ typedef color4b_t color_t;
#define color4b(r, g, b, a) ((color4b_t){r, g, b, a})
#define color(r, g, b, a) ((color_t){r, g, b, a})
#define color_hex(hex) color( \
#define colorHex(hex) color( \
((hex >> 24) & 0xFF), \
((hex >> 16) & 0xFF), \
((hex >> 8) & 0xFF), \

View File

@@ -8,7 +8,7 @@
#include "display/display.h"
#include "engine/engine.h"
#include "display/framebuffer.h"
#include "scene/scenemanager.h"
#include "scene/scene.h"
#include "display/spritebatch.h"
#include "display/mesh/quad.h"
#include "display/screen.h"
@@ -120,7 +120,9 @@ errorret_t displayUpdate(void) {
FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
COLOR_CORNFLOWER_BLUE
);
sceneManagerRender();
sceneRender();
// Render UI
uiRender();
// Finish up

View File

@@ -11,10 +11,10 @@
#include "input/input.h"
#include "locale/localemanager.h"
#include "display/display.h"
#include "scene/scenemanager.h"
#include "scene/scene.h"
#include "asset/asset.h"
#include "ui/ui.h"
#include "rpg/rpg.h"
// #include "rpg/rpg.h"
#include "script/scriptmanager.h"
#include "debug/debug.h"
@@ -35,8 +35,8 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
errorChain(scriptManagerInit());
errorChain(displayInit());
errorChain(uiInit());
errorChain(rpgInit());
errorChain(sceneManagerInit());
// errorChain(rpgInit());
errorChain(sceneInit());
// Run the initial script.
scriptcontext_t ctx;
@@ -51,9 +51,9 @@ errorret_t engineUpdate(void) {
timeUpdate();
inputUpdate();
errorChain(rpgUpdate());
// errorChain(rpgUpdate());
uiUpdate();
sceneManagerUpdate();
sceneUpdate();
errorChain(displayUpdate());
if(inputPressed(INPUT_ACTION_RAGEQUIT)) ENGINE.running = false;
@@ -67,8 +67,8 @@ void engineExit(void) {
errorret_t engineDispose(void) {
localeManagerDispose();
sceneManagerDispose();
rpgDispose();
// sceneManagerDispose();
// rpgDispose();
uiDispose();
errorChain(displayDispose());
assetDispose();

View File

@@ -6,5 +6,6 @@
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
cutsceneitem.c
item.c
inventory.c
)

165
src/item/inventory.c Normal file
View File

@@ -0,0 +1,165 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "inventory.h"
#include "util/memory.h"
#include "assert/assert.h"
void inventoryInit(
inventory_t* inventory,
inventorystack_t* storage,
uint8_t storageSize
) {
assertNotNull(inventory, "Inventory pointer is NULL.");
assertNotNull(storage, "Storage pointer is NULL.");
assertTrue(storageSize > 0, "Storage size must be greater than zero.");
inventory->storage = storage;
inventory->storageSize = storageSize;
// Zero item ids.
memoryZero(inventory->storage, sizeof(inventorystack_t) * storageSize);
}
bool_t inventoryItemExists(const inventory_t *inventory, const itemid_t item) {
assertNotNull(inventory, "Inventory pointer is NULL.");
assertNotNull(inventory->storage, "Storage pointer is NULL.");
assertTrue(inventory->storageSize > 0, "Storage too small.");
assertTrue(item != ITEM_ID_NULL, "Item ID cannot be ITEM_ID_NULL.");
inventorystack_t *stack = inventory->storage;
inventorystack_t *end = stack + inventory->storageSize;
do {
if(stack->item == ITEM_ID_NULL) break;
if(stack->item != item) continue;
assertTrue(stack->quantity > 0, "Item has quantity zero.");
return true;
} while(++stack < end);
return false;
}
void inventorySet(
inventory_t *inventory,
const itemid_t item,
const uint8_t quantity
) {
assertNotNull(inventory, "Inventory pointer is NULL.");
assertNotNull(inventory->storage, "Storage pointer is NULL.");
assertTrue(inventory->storageSize > 0, "Storage too small.");
assertTrue(item != ITEM_ID_NULL, "Item ID cannot be ITEM_ID_NULL.");
// If quantity 0, remove.
if(quantity == 0) return inventoryRemove(inventory, item);
// Search for existing stack.
inventorystack_t *stack = inventory->storage;
inventorystack_t *end = stack + inventory->storageSize;
do {
// Not in inventory yet, add as new stack.
if(stack->item == ITEM_ID_NULL) {
stack->item = item;
stack->quantity = quantity;
return;
}
// Not the stack we're looking for.
if(stack->item != item) continue;
// Update existing stack.
stack->quantity = quantity;
return;
} while(++stack < end);
// No space in the inventory.
assertUnreachable("Inventory is full, cannot set more items.");
}
void inventoryAdd(
inventory_t *inventory,
const itemid_t item,
const uint8_t quantity
) {
uint8_t current = inventoryGetCount(inventory, item);
uint16_t newQuantity = (uint16_t)current + (uint16_t)quantity;
assertTrue(
newQuantity <= UINT8_MAX,
"Cannot add item, would overflow maximum quantity."
);
inventorySet(inventory, item, (uint8_t)newQuantity);
}
void inventoryRemove(inventory_t *inventory, const itemid_t item) {
assertNotNull(inventory, "Inventory pointer is NULL.");
assertNotNull(inventory->storage, "Storage pointer is NULL.");
assertTrue(inventory->storageSize > 0, "Storage too small.");
assertTrue(item != ITEM_ID_NULL, "Item ID cannot be ITEM_ID_NULL.");
inventorystack_t *stack = inventory->storage;
inventorystack_t *end = stack + inventory->storageSize;
// Search for existing stack.
do {
// End of inventory, item not present.
if(stack->item == ITEM_ID_NULL) break;
// Not matching stack.
if(stack->item != item) continue;
// Match found, shift everything else down
memoryMove(stack, stack + 1, end - (stack + 1));
// Clear last stack.
inventorystack_t *last = end - 1;
last->item = ITEM_ID_NULL;
break;
} while(++stack < end);
}
uint8_t inventoryGetCount(const inventory_t *inventory, const itemid_t item) {
assertNotNull(inventory, "Inventory pointer is NULL.");
assertNotNull(inventory->storage, "Storage pointer is NULL.");
assertTrue(inventory->storageSize > 0, "Storage too small.");
assertTrue(item != ITEM_ID_NULL, "Item ID cannot be ITEM_ID_NULL.");
inventorystack_t *stack = inventory->storage;
inventorystack_t *end = stack + inventory->storageSize;
do {
// End of inventory, item not present.
if(stack->item == ITEM_ID_NULL) break;
// Not matching stack.
if(stack->item != item) continue;
// Match found, return quantity.
return stack->quantity;
} while(++stack < end);
return 0;
}
bool_t inventoryIsFull(const inventory_t *inventory) {
assertNotNull(inventory, "Inventory pointer is NULL.");
assertNotNull(inventory->storage, "Storage pointer is NULL.");
assertTrue(inventory->storageSize > 0, "Storage too small.");
inventorystack_t *stack = inventory->storage;
inventorystack_t *end = stack + inventory->storageSize;
do {
// Found empty stack, not full.
if(stack->item == ITEM_ID_NULL) return false;
} while(++stack < end);
return true;
}
bool_t inventoryItemFull(const inventory_t *inventory, const itemid_t item) {
return inventoryGetCount(inventory, item) == ITEM_STACK_QUANTITY_MAX;
}

103
src/item/inventory.h Normal file
View File

@@ -0,0 +1,103 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "item.h"
#define ITEM_STACK_QUANTITY_MAX UINT8_MAX
typedef struct {
itemid_t item;
uint8_t quantity;
} inventorystack_t;
typedef struct {
inventorystack_t *storage;
uint8_t storageSize;
} inventory_t;
/**
* Initializes an inventory.
*
* @param inventory The inventory to initialize.
* @param storage The storage array for the inventory.
* @param storageSize The size of the storage array.
*/
void inventoryInit(
inventory_t* inventory,
inventorystack_t* storage,
uint8_t storageSize
);
/**
* Checks if a specific item exists in the inventory (and has quantity > 0).
*
* @param inventory The inventory to check.
* @param item The item ID to check.
* @return true if the item exists, false otherwise.
*/
bool_t inventoryItemExists(const inventory_t *inventory, const itemid_t item);
/**
* Sets the quantity of a specific item in the inventory.
*
* @param inventory The inventory to modify.
* @param item The item ID to set.
* @param quantity The quantity to set.
*/
void inventorySet(
inventory_t *inventory,
const itemid_t item,
const uint8_t quantity
);
/**
* Adds a specific quantity of an item to the inventory.
*
* @param inventory The inventory to modify.
* @param item The item ID to add.
* @param quantity The quantity to add.
*/
void inventoryAdd(
inventory_t *inventory,
const itemid_t item,
const uint8_t quantity
);
/**
* Removes an item from the inventory.
*
* @param inventory The inventory to modify.
* @param item The item ID to remove.
*/
void inventoryRemove(inventory_t *inventory, const itemid_t item);
/**
* Gets the count of a specific item in the inventory.
*
* @param inventory The inventory to check.
* @param item The item ID to check.
* @return The count of the item in the inventory.
*/
uint8_t inventoryGetCount(const inventory_t *inventory, const itemid_t item);
/**
* Checks if the inventory is full.
*
* @param inventory The inventory to check.
* @return true if full, false otherwise.
*/
bool_t inventoryIsFull(const inventory_t *inventory);
/**
* Checks if a specific item stack is full in the inventory.
*
* @param inventory The inventory to check.
* @param item The item ID to check.
* @return true if the item stack is full, false otherwise.
*/
bool_t inventoryItemFull(const inventory_t *inventory, const itemid_t item);

19
src/item/item.c Normal file
View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "item.h"
const item_t ITEMS[] = {
[ITEM_ID_NULL] = {
.type = ITEM_TYPE_NULL
},
// Potion
[ITEM_ID_POTION] = {
.type = ITEM_TYPE_MEDICINE
},
};

21
src/item/item.h Normal file
View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "itemtype.h"
typedef struct {
itemtype_t type;
} item_t;
typedef uint8_t itemid_t;
#define ITEM_ID_NULL 0
#define ITEM_ID_POTION 1
#define ITEM_ID_POTATO 2
extern const item_t ITEMS[];

19
src/item/itemtype.h Normal file
View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef enum {
ITEM_TYPE_NULL,
ITEM_TYPE_MEDICINE,
ITEM_TYPE_INGREDIENT,
ITEM_TYPE_KEYITEM,
ITEM_TYPE_COUNT
} itemtype_t;

View File

@@ -1,17 +0,0 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
rpg.c
rpgcamera.c
rpgtextbox.c
)
# Subdirs
add_subdirectory(cutscene)
add_subdirectory(entity)
add_subdirectory(world)

View File

@@ -1,14 +0,0 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
cutscenesystem.c
cutscenemode.c
)
# Subdirs
add_subdirectory(item)

View File

@@ -1,14 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/cutscene/item/cutsceneitem.h"
typedef struct cutscene_s {
const cutsceneitem_t *items;
uint8_t itemCount;
} cutscene_t;

View File

@@ -1,19 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rpg/cutscene/cutscenesystem.h"
bool_t cutsceneModeIsInputAllowed() {
switch(CUTSCENE_SYSTEM.mode) {
case CUTSCENE_MODE_FULL_FREEZE:
case CUTSCENE_MODE_INPUT_FREEZE:
return false;
default:
return true;
}
}

View File

@@ -1,26 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef enum {
CUTSCENE_MODE_NONE,
CUTSCENE_MODE_FULL_FREEZE,
CUTSCENE_MODE_INPUT_FREEZE,
CUTSCENE_MODE_GAMEPLAY
} cutscenemode_t;
// Default mode for all cutscenes.
#define CUTSCENE_MODE_INITIAL CUTSCENE_MODE_INPUT_FREEZE
/**
* Check if input is allowed in the current cutscene mode.
*
* @return true if input is allowed, false otherwise.
*/
bool_t cutsceneModeIsInputAllowed();

View File

@@ -1,56 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "cutscenesystem.h"
#include "util/memory.h"
cutscenesystem_t CUTSCENE_SYSTEM;
void cutsceneSystemInit() {
memoryZero(&CUTSCENE_SYSTEM, sizeof(cutscenesystem_t));
}
void cutsceneSystemStartCutscene(const cutscene_t *cutscene) {
CUTSCENE_SYSTEM.scene = cutscene;
CUTSCENE_SYSTEM.mode = CUTSCENE_MODE_INITIAL;
CUTSCENE_SYSTEM.currentItem = 0xFF;// Set to 0xFF so start wraps.
cutsceneSystemNext();
}
void cutsceneSystemUpdate() {
if(CUTSCENE_SYSTEM.scene == NULL) return;
const cutsceneitem_t *item = cutsceneSystemGetCurrentItem();
cutsceneItemUpdate(item, &CUTSCENE_SYSTEM.data);
}
void cutsceneSystemNext() {
if(CUTSCENE_SYSTEM.scene == NULL) return;
CUTSCENE_SYSTEM.currentItem++;
// End of the cutscene?
if(
CUTSCENE_SYSTEM.currentItem >= CUTSCENE_SYSTEM.scene->itemCount
) {
CUTSCENE_SYSTEM.scene = NULL;
CUTSCENE_SYSTEM.currentItem = 0xFF;
CUTSCENE_SYSTEM.mode = CUTSCENE_MODE_NONE;
return;
}
// Start item.
const cutsceneitem_t *item = cutsceneSystemGetCurrentItem();
memset(&CUTSCENE_SYSTEM.data, 0, sizeof(CUTSCENE_SYSTEM.data));
cutsceneItemStart(item, &CUTSCENE_SYSTEM.data);
}
const cutsceneitem_t * cutsceneSystemGetCurrentItem() {
if(CUTSCENE_SYSTEM.scene == NULL) return NULL;
return &CUTSCENE_SYSTEM.scene->items[CUTSCENE_SYSTEM.currentItem];
}

View File

@@ -1,50 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "cutscene.h"
#include "cutscenemode.h"
typedef struct {
const cutscene_t *scene;
uint8_t currentItem;
// Data (used by the current item).
cutsceneitemdata_t data;
cutscenemode_t mode;
} cutscenesystem_t;
extern cutscenesystem_t CUTSCENE_SYSTEM;
/**
* Initialize the cutscene system.
*/
void cutsceneSystemInit();
/**
* Start a cutscene.
*
* @param cutscene Pointer to the cutscene to start.
*/
void cutsceneSystemStartCutscene(const cutscene_t *cutscene);
/**
* Advance to the next item in the cutscene.
*/
void cutsceneSystemNext();
/**
* Update the cutscene system for one frame.
*/
void cutsceneSystemUpdate();
/**
* Get the current cutscene item.
*
* @return Pointer to the current cutscene item.
*/
const cutsceneitem_t * cutsceneSystemGetCurrentItem();

View File

@@ -1,11 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef void (*cutscenecallback_t)(void);

View File

@@ -1,54 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rpg/cutscene/cutscenesystem.h"
#include "input/input.h"
#include "time/time.h"
void cutsceneItemStart(const cutsceneitem_t *item, cutsceneitemdata_t *data) {
switch(item->type) {
case CUTSCENE_ITEM_TYPE_TEXT: {
rpgTextboxShow(
item->text.position,
item->text.text
);
break;
}
case CUTSCENE_ITEM_TYPE_WAIT:
data->wait = item->wait;
break;
case CUTSCENE_ITEM_TYPE_CALLBACK:
if(item->callback != NULL) item->callback();
break;
case CUTSCENE_ITEM_TYPE_CUTSCENE:
if(item->cutscene != NULL) cutsceneSystemStartCutscene(item->cutscene);
break;
default:
break;
}
}
void cutsceneItemUpdate(const cutsceneitem_t *item, cutsceneitemdata_t *data) {
switch(item->type) {
case CUTSCENE_ITEM_TYPE_TEXT:
if(rpgTextboxIsVisible()) return;
cutsceneSystemNext();
break;
case CUTSCENE_ITEM_TYPE_WAIT:
data->wait -= TIME.delta;
if(data->wait <= 0) cutsceneSystemNext();
break;
default:
break;
}
}

View File

@@ -1,53 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "cutscenewait.h"
#include "cutscenecallback.h"
#include "cutscenetext.h"
typedef struct cutscene_s cutscene_t;
typedef enum {
CUTSCENE_ITEM_TYPE_NULL,
CUTSCENE_ITEM_TYPE_TEXT,
CUTSCENE_ITEM_TYPE_CALLBACK,
CUTSCENE_ITEM_TYPE_WAIT,
CUTSCENE_ITEM_TYPE_CUTSCENE
} cutsceneitemtype_t;
typedef struct cutsceneitem_s {
cutsceneitemtype_t type;
// Arguments/Data that will be used when this item is invoked.
union {
cutscenetext_t text;
cutscenecallback_t callback;
cutscenewait_t wait;
const cutscene_t *cutscene;
};
} cutsceneitem_t;
typedef union {
cutscenewaitdata_t wait;
} cutsceneitemdata_t;
/**
* Start the given cutscene item.
*
* @param item The cutscene item to start.
* @param data The cutscene item data storage.
*/
void cutsceneItemStart(const cutsceneitem_t *item, cutsceneitemdata_t *data);
/**
* Tick the given cutscene item (one frame).
*
* @param item The cutscene item to tick.
* @param data The cutscene item data storage.
*/
void cutsceneItemUpdate(const cutsceneitem_t *item, cutsceneitemdata_t *data);

View File

@@ -1,14 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/rpgtextbox.h"
typedef struct {
char_t text[RPG_TEXTBOX_MAX_CHARS];
rpgtextboxpos_t position;
} cutscenetext_t;

View File

@@ -1,12 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef float_t cutscenewait_t;
typedef float_t cutscenewaitdata_t;

View File

@@ -1,30 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/cutscene/cutscenesystem.h"
static const cutsceneitem_t TEST_CUTSCENE_ONE_ITEMS[] = {
{ .type = CUTSCENE_ITEM_TYPE_TEXT, .text = { .text = "This is a test cutscene.", .position = RPG_TEXTBOX_POS_BOTTOM } },
{ .type = CUTSCENE_ITEM_TYPE_WAIT, .wait = 2.0f },
{ .type = CUTSCENE_ITEM_TYPE_TEXT, .text = { .text = "It has multiple lines of text.\nAnd waits in between.", .position = RPG_TEXTBOX_POS_TOP } },
};
static const cutscene_t TEST_CUTSCENE_ONE = {
.items = TEST_CUTSCENE_ONE_ITEMS,
.itemCount = sizeof(TEST_CUTSCENE_ONE_ITEMS) / sizeof(cutsceneitem_t)
};
static const cutsceneitem_t TEST_CUTSCENE_TWO_ITEMS[] = {
{ .type = CUTSCENE_ITEM_TYPE_WAIT, .wait = 1.0f },
{ .type = CUTSCENE_ITEM_TYPE_CUTSCENE, .cutscene = &TEST_CUTSCENE_ONE },
};
static const cutscene_t TEST_CUTSCENE = {
.items = TEST_CUTSCENE_TWO_ITEMS,
.itemCount = sizeof(TEST_CUTSCENE_TWO_ITEMS) / sizeof(cutsceneitem_t)
};

View File

@@ -1,14 +0,0 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
entity.c
entityanim.c
npc.c
player.c
entitydir.c
)

View File

@@ -1,200 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "entity.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "time/time.h"
#include "util/math.h"
#include "rpg/cutscene/cutscenemode.h"
#include "rpg/world/map.h"
entity_t ENTITIES[ENTITY_COUNT];
void entityInit(entity_t *entity, const entitytype_t type) {
assertNotNull(entity, "Entity pointer cannot be NULL");
assertTrue(type < ENTITY_TYPE_COUNT, "Invalid entity type");
assertTrue(type != ENTITY_TYPE_NULL, "Cannot have NULL entity type");
assertTrue(
entity >= ENTITIES && entity < ENTITIES + ENTITY_COUNT,
"Entity pointer is out of bounds"
);
memoryZero(entity, sizeof(entity_t));
entity->id = (uint8_t)(entity - ENTITIES);
entity->type = type;
if(ENTITY_CALLBACKS[type].init != NULL) ENTITY_CALLBACKS[type].init(entity);
}
void entityUpdate(entity_t *entity) {
assertNotNull(entity, "Entity pointer cannot be NULL");
assertTrue(entity->type < ENTITY_TYPE_COUNT, "Invalid entity type");
assertTrue(entity->type != ENTITY_TYPE_NULL, "Cannot have NULL entity type");
// What state is the entity in?
if(entity->animation != ENTITY_ANIM_IDLE) {
// Entity is mid animation, tick it (down).
entity->animTime -= TIME.delta;
if(entity->animTime <= 0) {
entity->animation = ENTITY_ANIM_IDLE;
entity->animTime = 0;
}
return;
}
// Movement code.
if(
cutsceneModeIsInputAllowed() &&
ENTITY_CALLBACKS[entity->type].movement != NULL
) {
ENTITY_CALLBACKS[entity->type].movement(entity);
}
}
void entityTurn(entity_t *entity, const entitydir_t direction) {
entity->direction = direction;
entity->animation = ENTITY_ANIM_TURN;
entity->animTime = ENTITY_ANIM_TURN_DURATION;
}
void entityWalk(entity_t *entity, const entitydir_t direction) {
// TODO: Animation, delay, etc.
entity->direction = direction;
// Where are we moving?
worldpos_t newPos = entity->position;
{
worldunits_t relX, relY;
entityDirGetRelative(direction, &relX, &relY);
newPos.x += relX;
newPos.y += relY;
}
// Get tile under foot
tile_t tileCurrent = mapGetTile(entity->position);
tile_t tileNew = mapGetTile(newPos);
bool_t fall = false;
bool_t raise = false;
// Are we walking up a ramp?
if(
tileIsRamp(tileCurrent) &&
(
// Can only walk UP the direction the ramp faces.
(direction+TILE_SHAPE_RAMP_SOUTH) == tileCurrent ||
// If diagonal ramp, can go up one of two ways only.
(
(
tileCurrent == TILE_SHAPE_RAMP_SOUTHEAST &&
(direction == ENTITY_DIR_SOUTH || direction == ENTITY_DIR_EAST)
) ||
(
tileCurrent == TILE_SHAPE_RAMP_SOUTHWEST &&
(direction == ENTITY_DIR_SOUTH || direction == ENTITY_DIR_WEST)
) ||
(
tileCurrent == TILE_SHAPE_RAMP_NORTHEAST &&
(direction == ENTITY_DIR_NORTH || direction == ENTITY_DIR_EAST)
) ||
(
tileCurrent == TILE_SHAPE_RAMP_NORTHWEST &&
(direction == ENTITY_DIR_NORTH || direction == ENTITY_DIR_WEST)
)
)
// Must be able to walk up.
)
) {
tileNew = TILE_SHAPE_NULL;// Force check for ramp above.
worldpos_t abovePos = newPos;
abovePos.z += 1;
tile_t tileAbove = mapGetTile(abovePos);
if(tileAbove != TILE_SHAPE_NULL && tileIsWalkable(tileAbove)) {
// We can go up the ramp.
raise = true;
}
} else if(tileNew == TILE_SHAPE_NULL && newPos.z > 0) {
// Falling down?
worldpos_t belowPos = newPos;
belowPos.z -= 1;
tile_t tileBelow = mapGetTile(belowPos);
if(
tileBelow != TILE_SHAPE_NULL &&
tileIsRamp(tileBelow) &&
(
// This handles regular cardinal ramps
(entityDirGetOpposite(direction)+TILE_SHAPE_RAMP_SOUTH) == tileBelow ||
// This handles diagonal ramps
(
(
tileBelow == TILE_SHAPE_RAMP_SOUTHEAST &&
(direction == ENTITY_DIR_NORTH || direction == ENTITY_DIR_WEST)
) ||
(
tileBelow == TILE_SHAPE_RAMP_SOUTHWEST &&
(direction == ENTITY_DIR_NORTH || direction == ENTITY_DIR_EAST)
) ||
(
tileBelow == TILE_SHAPE_RAMP_NORTHEAST &&
(direction == ENTITY_DIR_SOUTH || direction == ENTITY_DIR_WEST)
) ||
(
tileBelow == TILE_SHAPE_RAMP_NORTHWEST &&
(direction == ENTITY_DIR_SOUTH || direction == ENTITY_DIR_EAST)
)
)
)
) {
// We will fall to this tile.
fall = true;
}
}
// Can we walk here?
if(!raise && !fall && !tileIsWalkable(tileNew)) return;// Blocked
// Entity in way?
entity_t *other = ENTITIES;
do {
if(other == entity) continue;
if(other->type == ENTITY_TYPE_NULL) continue;
if(!worldPosIsEqual(other->position, newPos)) continue;
return;// Blocked
} while(++other, other < &ENTITIES[ENTITY_COUNT]);
entity->lastPosition = entity->position;
entity->position = newPos;
entity->animation = ENTITY_ANIM_WALK;
entity->animTime = ENTITY_ANIM_WALK_DURATION;// TODO: Running vs walking
if(raise) {
entity->position.z += 1;
} else if(fall) {
entity->position.z -= 1;
}
}
entity_t * entityGetAt(const worldpos_t position) {
entity_t *ent = ENTITIES;
do {
if(ent->type == ENTITY_TYPE_NULL) continue;
if(!worldPosIsEqual(ent->position, position)) continue;
return ent;
} while(++ent, ent < &ENTITIES[ENTITY_COUNT]);
return NULL;
}
uint8_t entityGetAvailable() {
entity_t *ent = ENTITIES;
do {
if(ent->type == ENTITY_TYPE_NULL) return ent - ENTITIES;
} while(++ent, ent < &ENTITIES[ENTITY_COUNT]);
return 0xFF;
}

View File

@@ -1,77 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "entitydir.h"
#include "entityanim.h"
#include "entitytype.h"
#include "npc.h"
typedef struct map_s map_t;
typedef struct entity_s {
uint8_t id;
entitytype_t type;
entitytypedata_t data;
// Movement
entitydir_t direction;
worldpos_t position;
worldpos_t lastPosition;
entityanim_t animation;
float_t animTime;
} entity_t;
extern entity_t ENTITIES[ENTITY_COUNT];
/**
* Initializes an entity structure.
*
* @param entity Pointer to the entity structure to initialize.
* @param type The type of the entity.
*/
void entityInit(entity_t *entity, const entitytype_t type);
/**
* Updates an entity.
*
* @param entity Pointer to the entity structure to update.
*/
void entityUpdate(entity_t *entity);
/**
* Turn an entity to face a new direction.
*
* @param entity Pointer to the entity to turn.
* @param direction The direction to face.
*/
void entityTurn(entity_t *entity, const entitydir_t direction);
/**
* Make an entity walk in a direction.
*
* @param entity Pointer to the entity to make walk.
* @param direction The direction to walk in.
*/
void entityWalk(entity_t *entity, const entitydir_t direction);
/**
* Gets the entity at a specific world position.
*
* @param map Pointer to the map to check.
* @param pos The world position to check.
* @return Pointer to the entity at the position, or NULL if none.
*/
entity_t *entityGetAt(const worldpos_t pos);
/**
* Gets an available entity index.
*
* @return The index of an available entity, or 0xFF if none are available.
*/
uint8_t entityGetAvailable();

View File

@@ -1,9 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "entityanim.h"

View File

@@ -1,18 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
#define ENTITY_ANIM_TURN_DURATION 0.06f
#define ENTITY_ANIM_WALK_DURATION 0.1f
typedef enum {
ENTITY_ANIM_IDLE,
ENTITY_ANIM_TURN,
ENTITY_ANIM_WALK,
} entityanim_t;

View File

@@ -1,51 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "entitydir.h"
#include "assert/assert.h"
entitydir_t entityDirGetOpposite(const entitydir_t dir) {
switch(dir) {
case ENTITY_DIR_NORTH: return ENTITY_DIR_SOUTH;
case ENTITY_DIR_SOUTH: return ENTITY_DIR_NORTH;
case ENTITY_DIR_EAST: return ENTITY_DIR_WEST;
case ENTITY_DIR_WEST: return ENTITY_DIR_EAST;
default: return dir;
}
}
void entityDirGetRelative(
const entitydir_t from,
worldunits_t *outX,
worldunits_t *outY
) {
assertValidEntityDir(from, "Invalid direction provided");
assertNotNull(outX, "Output X pointer cannot be NULL");
assertNotNull(outY, "Output Y pointer cannot be NULL");
switch(from) {
case ENTITY_DIR_NORTH:
*outX = 0;
*outY = -1;
break;
case ENTITY_DIR_EAST:
*outX = 1;
*outY = 0;
break;
case ENTITY_DIR_SOUTH:
*outX = 0;
*outY = 1;
break;
case ENTITY_DIR_WEST:
*outX = -1;
*outY = 0;
break;
}
}

View File

@@ -1,50 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/world/worldpos.h"
typedef enum {
ENTITY_DIR_UP = ENTITY_DIR_NORTH,
ENTITY_DIR_DOWN = ENTITY_DIR_SOUTH,
ENTITY_DIR_LEFT = ENTITY_DIR_WEST,
ENTITY_DIR_RIGHT = ENTITY_DIR_EAST,
} entitydir_t;
/**
* Gets the opposite direction of a given direction.
*
* @param dir The direction to get the opposite of.
* @return entitydir_t The opposite direction.
*/
entitydir_t entityDirGetOpposite(const entitydir_t dir);
/**
* Asserts a given direction is valid.
*
* @param dir The direction to validate.
* @param msg The message to display if the assertion fails.
*/
#define assertValidEntityDir(dir, msg) \
assertTrue( \
(dir) == ENTITY_DIR_NORTH || \
(dir) == ENTITY_DIR_EAST || \
(dir) == ENTITY_DIR_SOUTH || \
(dir) == ENTITY_DIR_WEST, \
msg \
)
/**
* Gets the relative x and y offsets for a given direction.
*
* @param dir The direction to get offsets for.
* @param relX Pointer to store the relative x offset.
* @param relY Pointer to store the relative y offset.
*/
void entityDirGetRelative(
const entitydir_t dir, worldunits_t *relX, worldunits_t *relY
);

View File

@@ -1,55 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "duskdefs.h"
#include "rpg/entity/player.h"
#include "npc.h"
typedef uint8_t entitytype_t;
typedef union {
player_t player;
npc_t npc;
} entitytypedata_t;
typedef struct {
/**
* Initialization callback for the entity type.
* @param entity Pointer to the entity to initialize.
*/
void (*init)(entity_t *entity);
/**
* Movement callback for the entity type.
* @param entity Pointer to the entity to move.
*/
void (*movement)(entity_t *entity);
/**
* Interaction callback for the entity type.
* @param player Pointer to the player entity.
* @param entity Pointer to the entity to interact with.
* @return True if the entity handled the interaction, false otherwise.
*/
bool_t (*interact)(entity_t *player, entity_t *entity);
} entitycallback_t;
static const entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = {
[ENTITY_TYPE_NULL] = { NULL },
[ENTITY_TYPE_PLAYER] = {
.init = playerInit,
.movement = playerInput
},
[ENTITY_TYPE_NPC] = {
.init = npcInit,
.movement = npcMovement,
.interact = npcInteract
}
};

View File

@@ -1,31 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "entity.h"
#include "assert/assert.h"
#include "rpg/cutscene/scene/testcutscene.h"
#include "rpg/rpgtextbox.h"
void npcInit(entity_t *entity) {
assertNotNull(entity, "Entity pointer cannot be NULL");
}
void npcMovement(entity_t *entity) {
assertNotNull(entity, "Entity pointer cannot be NULL");
}
bool_t npcInteract(entity_t *player, entity_t *npc) {
assertNotNull(player, "Player entity pointer cannot be NULL");
assertNotNull(npc, "NPC entity pointer cannot be NULL");
cutsceneSystemStartCutscene(&TEST_CUTSCENE);
// rpgTextboxShow(RPG_TEXTBOX_POS_BOTTOM, "Hello World!");
return false;
};

View File

@@ -1,37 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef struct entity_s entity_t;
typedef struct {
void *nothing;
} npc_t;
/**
* Initializes an NPC entity.
*
* @param entity Pointer to the entity structure to initialize.
*/
void npcInit(entity_t *entity);
/**
* Updates an NPC entity.
*
* @param entity Pointer to the entity structure to update.
*/
void npcMovement(entity_t *entity);
/**
* Handles interaction with an NPC entity.
*
* @param player Pointer to the player entity.
* @param npc Pointer to the NPC entity.
*/
bool_t npcInteract(entity_t *player, entity_t *npc);

View File

@@ -1,54 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "entity.h"
#include "assert/assert.h"
#include "rpg/rpgcamera.h"
#include "util/memory.h"
#include "time/time.h"
void playerInit(entity_t *entity) {
assertNotNull(entity, "Entity pointer cannot be NULL");
}
void playerInput(entity_t *entity) {
assertNotNull(entity, "Entity pointer cannot be NULL");
// Turn
const playerinputdirmap_t *dirMap = PLAYER_INPUT_DIR_MAP;
do {
if(!inputIsDown(dirMap->action)) continue;
if(entity->direction == dirMap->direction) continue;
return entityTurn(entity, dirMap->direction);
} while((++dirMap)->action != 0xFF);
// Walk
dirMap = PLAYER_INPUT_DIR_MAP;
do {
if(!inputIsDown(dirMap->action)) continue;
if(entity->direction != dirMap->direction) continue;
return entityWalk(entity, dirMap->direction);
} while((++dirMap)->action != 0xFF);
// Interaction
if(inputPressed(INPUT_ACTION_ACCEPT)) {
worldunit_t x, y, z;
{
worldunits_t relX, relY;
entityDirGetRelative(entity->direction, &relX, &relY);
x = entity->position.x + relX;
y = entity->position.y + relY;
z = entity->position.z;
}
entity_t *interact = entityGetAt((worldpos_t){ x, y, z });
if(interact != NULL && ENTITY_CALLBACKS[interact->type].interact != NULL) {
if(ENTITY_CALLBACKS[interact->type].interact(entity, interact)) return;
}
}
}

View File

@@ -1,43 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "input/input.h"
typedef struct entity_s entity_t;
typedef struct {
void *nothing;
} player_t;
typedef struct {
inputaction_t action;
entitydir_t direction;
} playerinputdirmap_t;
static const playerinputdirmap_t PLAYER_INPUT_DIR_MAP[] = {
{ INPUT_ACTION_UP, ENTITY_DIR_NORTH },
{ INPUT_ACTION_DOWN, ENTITY_DIR_SOUTH },
{ INPUT_ACTION_LEFT, ENTITY_DIR_WEST },
{ INPUT_ACTION_RIGHT, ENTITY_DIR_EAST },
{ 0xFF, 0xFF }
};
/**
* Initializes a player entity.
*
* @param entity Pointer to the entity structure to initialize.
*/
void playerInit(entity_t *entity);
/**
* Handles movement logic for the player entity.
*
* @param entity Pointer to the player entity structure.
*/
void playerInput(entity_t *entity);

View File

@@ -1,13 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef struct {
void *nothing;
} inventory_t;

View File

@@ -1,66 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rpg.h"
#include "entity/entity.h"
#include "rpg/world/map.h"
#include "rpg/cutscene/cutscenesystem.h"
#include "time/time.h"
#include "rpgcamera.h"
#include "rpgtextbox.h"
#include "util/memory.h"
#include "assert/assert.h"
errorret_t rpgInit(void) {
memoryZero(ENTITIES, sizeof(ENTITIES));
// Init cutscene subsystem
cutsceneSystemInit();
errorChain(mapInit());
rpgCameraInit();
rpgTextboxInit();
// TEST: Create some entities.
// uint8_t entIndex = entityGetAvailable();
// assertTrue(entIndex != 0xFF, "No available entity slots!.");
// entity_t *ent = &ENTITIES[entIndex];
// entityInit(ent, ENTITY_TYPE_PLAYER);
// RPG_CAMERA.mode = RPG_CAMERA_MODE_FOLLOW_ENTITY;
// RPG_CAMERA.followEntity.followEntityId = ent->id;
// ent->position.x = 2, ent->position.y = 2;
// All Good!
errorOk();
}
errorret_t rpgUpdate(void) {
#if TIME_FIXED == 0
if(TIME.dynamicUpdate) {
errorOk();
}
#endif
// TODO: Do not update if the scene is not the map scene?
mapUpdate();
// Update overworld ents.
entity_t *ent = &ENTITIES[0];
do {
if(ent->type == ENTITY_TYPE_NULL) continue;
entityUpdate(ent);
} while(++ent < &ENTITIES[ENTITY_COUNT]);
cutsceneSystemUpdate();
errorChain(rpgCameraUpdate());
errorOk();
}
void rpgDispose(void) {
mapDispose();
}

View File

@@ -1,32 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
typedef struct {
int32_t nothing;
} rpg_t;
/**
* Initialize the RPG subsystem.
*
* @return An error code and state.
*/
errorret_t rpgInit(void);
/**
* Update the RPG subsystem.
*
* @return An error code.
*/
errorret_t rpgUpdate(void);
/**
* Dispose of the RPG subsystem.
*/
void rpgDispose(void);

View File

@@ -1,52 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rpgcamera.h"
#include "util/memory.h"
#include "rpg/entity/entity.h"
#include "rpg/world/map.h"
#include "assert/assert.h"
rpgcamera_t RPG_CAMERA;
void rpgCameraInit(void) {
memoryZero(&RPG_CAMERA, sizeof(rpgcamera_t));
}
errorret_t rpgCameraUpdate(void) {
if(!mapIsLoaded()) errorOk();
chunkpos_t chunkPos;
switch(RPG_CAMERA.mode) {
case RPG_CAMERA_MODE_FREE:
worldPosToChunkPos(&RPG_CAMERA.free, &chunkPos);
break;
case RPG_CAMERA_MODE_FOLLOW_ENTITY: {
entity_t *entity = &ENTITIES[RPG_CAMERA.followEntity.followEntityId];
if(entity->type == ENTITY_TYPE_NULL) {
errorOk();
}
// Update map position to match camera. By default map wants to know the
// top left but we want to set the center, so we need to sub half map size
worldPosToChunkPos(&entity->position, &chunkPos);
break;
}
default:
assertUnreachable("Invalid RPG camera mode");
}
errorChain(mapPositionSet((chunkpos_t){
.x = chunkPos.x - (MAP_CHUNK_WIDTH / 2),
.y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2),
.z = chunkPos.z - (MAP_CHUNK_DEPTH / 2)
}));
errorOk();
}

View File

@@ -1,40 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/world/worldpos.h"
#include "error/error.h"
typedef enum {
RPG_CAMERA_MODE_FREE,
RPG_CAMERA_MODE_FOLLOW_ENTITY,
} rpgcameramode_t;
typedef struct {
rpgcameramode_t mode;
union {
worldpos_t free;
struct {
uint8_t followEntityId;
} followEntity;
};
} rpgcamera_t;
extern rpgcamera_t RPG_CAMERA;
/**
* Initializes the RPG camera.
*/
void rpgCameraInit(void);
/**
* Updates the RPG camera.
*
* @return An error code.
*/
errorret_t rpgCameraUpdate(void);

View File

@@ -1,39 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rpgtextbox.h"
#include "util/memory.h"
#include "util/string.h"
#include "assert/assert.h"
rpgtextbox_t RPG_TEXTBOX;
void rpgTextboxInit() {
memoryZero(&RPG_TEXTBOX, sizeof(rpgtextbox_t));
}
void rpgTextboxShow(
const rpgtextboxpos_t position,
const char_t *text
) {
RPG_TEXTBOX.position = position;
RPG_TEXTBOX.visible = true;
stringCopy(
RPG_TEXTBOX.text,
text,
RPG_TEXTBOX_MAX_CHARS
);
}
void rpgTextboxHide() {
RPG_TEXTBOX.visible = false;
}
bool_t rpgTextboxIsVisible() {
return RPG_TEXTBOX.visible;
}

View File

@@ -1,52 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
#define RPG_TEXTBOX_MAX_CHARS 256
typedef enum {
RPG_TEXTBOX_POS_TOP,
RPG_TEXTBOX_POS_BOTTOM,
} rpgtextboxpos_t;
typedef struct {
rpgtextboxpos_t position;
bool_t visible;
char_t text[RPG_TEXTBOX_MAX_CHARS];
} rpgtextbox_t;
extern rpgtextbox_t RPG_TEXTBOX;
/**
* Initializes the RPG textbox.
*/
void rpgTextboxInit();
/**
* Shows the RPG textbox at a specified position.
*
* @param position The position to show the textbox at.
* @param text The text to display in the textbox (copied).
*/
void rpgTextboxShow(
const rpgtextboxpos_t position,
const char_t *text
);
/**
* Hides the RPG textbox.
*/
void rpgTextboxHide();
/**
* Checks if the RPG textbox is currently visible.
*
* @return true if the textbox is visible, false otherwise.
*/
bool_t rpgTextboxIsVisible();

View File

@@ -1,13 +0,0 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
chunk.c
map.c
worldpos.c
tile.c
)

View File

@@ -1,20 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "chunk.h"
uint32_t chunkGetTileIndex(const chunkpos_t position) {
return (
(position.z * CHUNK_WIDTH * CHUNK_HEIGHT) +
(position.y * CHUNK_WIDTH) +
position.x
);
}
bool_t chunkPositionIsEqual(const chunkpos_t a, const chunkpos_t b) {
return (a.x == b.x) && (a.y == b.y) && (a.z == b.z);
}

View File

@@ -1,38 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/world/tile.h"
#include "worldpos.h"
#include "display/mesh/quad.h"
typedef struct chunk_s {
chunkpos_t position;
tile_t tiles[CHUNK_TILE_COUNT];
uint8_t meshCount;
meshvertex_t vertices[CHUNK_VERTEX_COUNT_MAX];
mesh_t meshes[CHUNK_MESH_COUNT_MAX];
uint8_t entities[CHUNK_ENTITY_COUNT_MAX];
} chunk_t;
/**
* Gets the tile index for a tile position within a chunk.
*
* @param position The position within the chunk.
* @return The tile index within the chunk.
*/
uint32_t chunkGetTileIndex(const chunkpos_t position);
/**
* Checks if two chunk positions are equal.
*
* @param a The first chunk position.
* @param b The second chunk position.
* @return true if equal, false otherwise.
*/
bool_t chunkPositionIsEqual(const chunkpos_t a, const chunkpos_t b);

View File

@@ -1,269 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "map.h"
#include "util/memory.h"
#include "assert/assert.h"
#include "asset/asset.h"
#include "rpg/entity/entity.h"
#include "util/string.h"
#include "script/scriptcontext.h"
map_t MAP;
errorret_t mapInit() {
memoryZero(&MAP, sizeof(map_t));
errorOk();
}
bool_t mapIsLoaded() {
return MAP.filePath[0] != '\0';
}
errorret_t mapLoad(const char_t *path, const chunkpos_t position) {
assertStrLenMin(path, 1, "Map file path cannot be empty");
assertStrLenMax(path, MAP_FILE_PATH_MAX - 1, "Map file path too long");
if(stringCompare(MAP.filePath, path) == 0) {
// Same map, no need to reload
errorOk();
}
chunkindex_t i;
// Unload all loaded chunks
if(mapIsLoaded()) {
for(i = 0; i < MAP_CHUNK_COUNT; i++) {
mapChunkUnload(&MAP.chunks[i]);
}
}
// Store the map file path
stringCopy(MAP.filePath, path, MAP_FILE_PATH_MAX);
// Determine directory path (it is dirname)
stringCopy(MAP.dirPath, path, MAP_FILE_PATH_MAX);
char_t *last = stringFindLastChar(MAP.dirPath, '/');
if(last == NULL) errorThrow("Invalid map file path");
// Store filename, sans extension
stringCopy(MAP.fileName, last + 1, MAP_FILE_PATH_MAX);
*last = '\0'; // Terminate to get directory path
last = stringFindLastChar(MAP.fileName, '.');
if(last == NULL) errorThrow("Map file name has no extension");
*last = '\0'; // Terminate to remove extension
// Reset map position
MAP.chunkPosition = position;
// Perform "initial load"
i = 0;
for(chunkunit_t z = 0; z < MAP_CHUNK_DEPTH; z++) {
for(chunkunit_t y = 0; y < MAP_CHUNK_HEIGHT; y++) {
for(chunkunit_t x = 0; x < MAP_CHUNK_WIDTH; x++) {
chunk_t *chunk = &MAP.chunks[i];
chunk->position.x = x + position.x;
chunk->position.y = y + position.y;
chunk->position.z = z + position.z;
MAP.chunkOrder[i] = chunk;
errorChain(mapChunkLoad(chunk));
i++;
}
}
}
// Execute map script.
char_t scriptPath[MAP_FILE_PATH_MAX + 16];
stringFormat(
scriptPath, sizeof(scriptPath), "%s/%s.dsf",
MAP.dirPath, MAP.fileName
);
if(assetFileExists(scriptPath)) {
scriptcontext_t ctx;
errorChain(scriptContextInit(&ctx));
errorChain(scriptContextExecFile(&ctx, scriptPath));
scriptContextDispose(&ctx);
}
errorOk();
}
errorret_t mapPositionSet(const chunkpos_t newPos) {
if(!mapIsLoaded()) errorThrow("No map loaded");
const chunkpos_t curPos = MAP.chunkPosition;
if(chunkPositionIsEqual(curPos, newPos)) {
errorOk();
}
// Determine which chunks remain loaded
chunkindex_t chunksRemaining[MAP_CHUNK_COUNT] = {0};
chunkindex_t chunksFreed[MAP_CHUNK_COUNT] = {0};
uint32_t remainingCount = 0;
uint32_t freedCount = 0;
for(chunkindex_t i = 0; i < MAP_CHUNK_COUNT; i++) {
// Will this chunk remain loaded?
chunk_t *chunk = &MAP.chunks[i];
if(
chunk->position.x >= newPos.x &&
chunk->position.x < newPos.x + MAP_CHUNK_WIDTH &&
chunk->position.y >= newPos.y &&
chunk->position.y < newPos.y + MAP_CHUNK_HEIGHT &&
chunk->position.z >= newPos.z &&
chunk->position.z < newPos.z + MAP_CHUNK_DEPTH
) {
// Stays loaded
chunksRemaining[remainingCount++] = i;
continue;
}
// Not remaining loaded
chunksFreed[freedCount++] = i;
}
// Unload the freed chunks
for(chunkindex_t i = 0; i < freedCount; i++) {
chunk_t *chunk = &MAP.chunks[chunksFreed[i]];
mapChunkUnload(chunk);
}
// This can probably be optimized later, for now we check each chunk and see
// if it needs loading or not, and update the chunk order
chunkindex_t orderIndex = 0;
for(chunkunit_t zOff = 0; zOff < MAP_CHUNK_DEPTH; zOff++) {
for(chunkunit_t yOff = 0; yOff < MAP_CHUNK_HEIGHT; yOff++) {
for(chunkunit_t xOff = 0; xOff < MAP_CHUNK_WIDTH; xOff++) {
const chunkpos_t newChunkPos = {
newPos.x + xOff, newPos.y + yOff, newPos.z + zOff
};
// Is this chunk already loaded (was not unloaded earlier)?
chunkindex_t chunkIndex = -1;
for(chunkindex_t i = 0; i < remainingCount; i++) {
chunk_t *chunk = &MAP.chunks[chunksRemaining[i]];
if(!chunkPositionIsEqual(chunk->position, newChunkPos)) continue;
chunkIndex = chunksRemaining[i];
break;
}
// Need to load this chunk
if(chunkIndex == -1) {
// Find a freed chunk to reuse
chunkIndex = chunksFreed[--freedCount];
chunk_t *chunk = &MAP.chunks[chunkIndex];
chunk->position = newChunkPos;
errorChain(mapChunkLoad(chunk));
}
MAP.chunkOrder[orderIndex++] = &MAP.chunks[chunkIndex];
}
}
}
// Update map position
MAP.chunkPosition = newPos;
errorOk();
}
void mapUpdate() {
}
void mapDispose() {
for(chunkindex_t i = 0; i < MAP_CHUNK_COUNT; i++) {
mapChunkUnload(&MAP.chunks[i]);
}
}
void mapChunkUnload(chunk_t* chunk) {
for(uint8_t i = 0; i < CHUNK_ENTITY_COUNT_MAX; i++) {
if(chunk->entities[i] == 0xFF) break;
entity_t *entity = &ENTITIES[chunk->entities[i]];
entity->type = ENTITY_TYPE_NULL;
}
for(uint8_t i = 0; i < chunk->meshCount; i++) {
if(chunk->meshes[i].vertexCount == 0) continue;
meshDispose(&chunk->meshes[i]);
}
}
errorret_t mapChunkLoad(chunk_t* chunk) {
if(!mapIsLoaded()) errorThrow("No map loaded");
char_t buffer[64];
// TODO: Can probably move this to asset load logic?
chunk->meshCount = 0;
memoryZero(chunk->meshes, sizeof(chunk->meshes));
memorySet(chunk->entities, 0xFF, sizeof(chunk->entities));
// Get chunk filepath.
snprintf(buffer, sizeof(buffer), "%s/chunks/%d_%d_%d.dcf",
MAP.dirPath,
chunk->position.x,
chunk->position.y,
chunk->position.z
);
// Chunk available?
if(!assetFileExists(buffer)) {
memoryZero(chunk->tiles, sizeof(chunk->tiles));
errorOk();
}
// Load.
errorChain(assetLoad(buffer, chunk));
errorOk();
}
chunkindex_t mapGetChunkIndexAt(const chunkpos_t position) {
if(!mapIsLoaded()) return -1;
chunkpos_t relPos = {
position.x - MAP.chunkPosition.x,
position.y - MAP.chunkPosition.y,
position.z - MAP.chunkPosition.z
};
if(
relPos.x < 0 || relPos.y < 0 || relPos.z < 0 ||
relPos.x >= MAP_CHUNK_WIDTH ||
relPos.y >= MAP_CHUNK_HEIGHT ||
relPos.z >= MAP_CHUNK_DEPTH
) {
return -1;
}
return chunkPosToIndex(&relPos);
}
chunk_t* mapGetChunk(const uint8_t index) {
if(index >= MAP_CHUNK_COUNT) return NULL;
if(!mapIsLoaded()) return NULL;
return MAP.chunkOrder[index];
}
tile_t mapGetTile(const worldpos_t position) {
if(!mapIsLoaded()) return TILE_SHAPE_NULL;
chunkpos_t chunkPos;
worldPosToChunkPos(&position, &chunkPos);
chunkindex_t chunkIndex = mapGetChunkIndexAt(chunkPos);
if(chunkIndex == -1) return TILE_SHAPE_NULL;
chunk_t *chunk = mapGetChunk(chunkIndex);
assertNotNull(chunk, "Chunk pointer cannot be NULL");
chunktileindex_t tileIndex = worldPosToChunkTileIndex(&position);
return chunk->tiles[tileIndex];
}

View File

@@ -1,106 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/world/chunk.h"
#define MAP_FILE_PATH_MAX 128
typedef struct map_s {
char_t filePath[MAP_FILE_PATH_MAX];
char_t dirPath[MAP_FILE_PATH_MAX];
char_t fileName[MAP_FILE_PATH_MAX];
chunk_t chunks[MAP_CHUNK_COUNT];
chunk_t *chunkOrder[MAP_CHUNK_COUNT];
chunkpos_t chunkPosition;
} map_t;
extern map_t MAP;
/**
* Initializes the map.
*
* @return An error code.
*/
errorret_t mapInit();
/**
* Checks if a map is loaded.
*
* @return true if a map is loaded, false otherwise.
*/
bool_t mapIsLoaded();
/**
* Loads a map from the given file path.
*
* @param path The file path.
* @param position The initial chunk position.
* @return An error code.
*/
errorret_t mapLoad(
const char_t *path,
const chunkpos_t position
);
/**
* Updates the map.
*/
void mapUpdate();
/**
* Disposes of the map.
*/
void mapDispose();
/**
* Sets the map position and updates chunks accordingly.
*
* @param newPos The new chunk position.
* @return An error code.
*/
errorret_t mapPositionSet(const chunkpos_t newPos);
/**
* Unloads a chunk.
*
* @param chunk The chunk to unload.
*/
void mapChunkUnload(chunk_t* chunk);
/**
* Loads a chunk.
*
* @param chunk The chunk to load.
* @return An error code.
*/
errorret_t mapChunkLoad(chunk_t* chunk);
/**
* Gets the index of a chunk, within the world, at the given position.
*
* @param position The chunk position.
* @return The index of the chunk, or -1 if out of bounds.
*/
chunkindex_t mapGetChunkIndexAt(const chunkpos_t position);
/**
* Gets a chunk by its index.
*
* @param chunkIndex The index of the chunk.
* @return A pointer to the chunk.
*/
chunk_t * mapGetChunk(const uint8_t chunkIndex);
/**
* Gets the tile at the given world position.
*
* @param position The world position.
* @return The tile at that position, or TILE_NULL if the chunk is unloaded.
*/
tile_t mapGetTile(const worldpos_t position);

View File

@@ -1,35 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "tile.h"
bool_t tileIsWalkable(const tile_t tile) {
switch(tile) {
case TILE_SHAPE_NULL:
return false;
default:
return true;
}
}
bool_t tileIsRamp(const tile_t tile) {
switch(tile) {
case TILE_SHAPE_RAMP_NORTH:
case TILE_SHAPE_RAMP_SOUTH:
case TILE_SHAPE_RAMP_EAST:
case TILE_SHAPE_RAMP_WEST:
case TILE_SHAPE_RAMP_NORTHEAST:
case TILE_SHAPE_RAMP_NORTHWEST:
case TILE_SHAPE_RAMP_SOUTHEAST:
case TILE_SHAPE_RAMP_SOUTHWEST:
return true;
default:
return false;
}
}

View File

@@ -1,27 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/entity/entitydir.h"
typedef uint8_t tile_t;
/**
* Returns whether or not the given tile is walkable.
*
* @param tile The tile to check.
* @return bool_t True if walkable, false if not.
*/
bool_t tileIsWalkable(const tile_t tile);
/**
* Returns whether or not the given tile is a ramp tile.
*
* @param tile The tile to check.
* @return bool_t True if ramp, false if not.
*/
bool_t tileIsRamp(const tile_t tile);

View File

@@ -1,97 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "worldpos.h"
#include "assert/assert.h"
bool_t worldPosIsEqual(const worldpos_t a, const worldpos_t b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out) {
assertNotNull(chunkPos, "Chunk position pointer cannot be NULL");
assertNotNull(out, "Output world position pointer cannot be NULL");
out->x = (worldunit_t)(chunkPos->x * CHUNK_WIDTH);
out->y = (worldunit_t)(chunkPos->y * CHUNK_HEIGHT);
out->z = (worldunit_t)(chunkPos->z * CHUNK_DEPTH);
}
void worldPosToChunkPos(const worldpos_t* worldPos, chunkpos_t* out) {
assertNotNull(worldPos, "World position pointer cannot be NULL");
assertNotNull(out, "Output chunk position pointer cannot be NULL");
if(worldPos->x < 0) {
out->x = (chunkunit_t)((worldPos->x - (CHUNK_WIDTH - 1)) / CHUNK_WIDTH);
} else {
out->x = (chunkunit_t)(worldPos->x / CHUNK_WIDTH);
}
if(worldPos->y < 0) {
out->y = (chunkunit_t)((worldPos->y - (CHUNK_HEIGHT - 1)) / CHUNK_HEIGHT);
} else {
out->y = (chunkunit_t)(worldPos->y / CHUNK_HEIGHT);
}
if(worldPos->z < 0) {
out->z = (chunkunit_t)((worldPos->z - (CHUNK_DEPTH - 1)) / CHUNK_DEPTH);
} else {
out->z = (chunkunit_t)(worldPos->z / CHUNK_DEPTH);
}
}
chunktileindex_t worldPosToChunkTileIndex(const worldpos_t* worldPos) {
assertNotNull(worldPos, "World position pointer cannot be NULL");
uint8_t localX, localY, localZ;
if(worldPos->x < 0) {
localX = (uint8_t)(
(CHUNK_WIDTH - 1) - ((-worldPos->x - 1) % CHUNK_WIDTH)
);
} else {
localX = (uint8_t)(worldPos->x % CHUNK_WIDTH);
}
if(worldPos->y < 0) {
localY = (uint8_t)(
(CHUNK_HEIGHT - 1) - ((-worldPos->y - 1) % CHUNK_HEIGHT)
);
} else {
localY = (uint8_t)(worldPos->y % CHUNK_HEIGHT);
}
if(worldPos->z < 0) {
localZ = (uint8_t)(
(CHUNK_DEPTH - 1) - ((-worldPos->z - 1) % CHUNK_DEPTH)
);
} else {
localZ = (uint8_t)(worldPos->z % CHUNK_DEPTH);
}
chunktileindex_t chunkTileIndex = (chunktileindex_t)(
(localZ * CHUNK_WIDTH * CHUNK_HEIGHT) +
(localY * CHUNK_WIDTH) +
localX
);
assertTrue(
chunkTileIndex < CHUNK_TILE_COUNT,
"Calculated chunk tile index is out of bounds"
);
return chunkTileIndex;
}
chunkindex_t chunkPosToIndex(const chunkpos_t* pos) {
assertNotNull(pos, "Chunk position pointer cannot be NULL");
chunkindex_t chunkIndex = (chunkindex_t)(
(pos->z * MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT) +
(pos->y * MAP_CHUNK_WIDTH) +
pos->x
);
return chunkIndex;
}

View File

@@ -1,75 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
#include "duskdefs.h"
#define CHUNK_TILE_COUNT (CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH)
#define MAP_CHUNK_WIDTH 3
#define MAP_CHUNK_HEIGHT 3
#define MAP_CHUNK_DEPTH 3
#define MAP_CHUNK_COUNT (MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT * MAP_CHUNK_DEPTH)
typedef int16_t worldunit_t;
typedef int16_t chunkunit_t;
typedef int16_t chunkindex_t;
typedef uint32_t chunktileindex_t;
typedef int32_t worldunits_t;
typedef int32_t chunkunits_t;
typedef struct worldpos_s {
worldunit_t x, y, z;
} worldpos_t;
typedef struct chunkpos_t {
chunkunit_t x, y, z;
} chunkpos_t;
/**
* Compares two world positions for equality.
*
* @param a The first world position.
* @param b The second world position.
* @return true if equal, false otherwise.
*/
bool_t worldPosIsEqual(const worldpos_t a, const worldpos_t b);
/**
* Converts a world position to a chunk position.
*
* @param worldPos The world position.
* @param out The output chunk position.
*/
void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out);
/**
* Converts a chunk position to a world position.
*
* @param worldPos The world position.
* @param out The output chunk position.
*/
void worldPosToChunkPos(const worldpos_t* worldPos, chunkpos_t* out);
/**
* Converts a position in world-space to an index inside a chunk that the tile
* resides in.
*
* @param worldPos The world position.
* @return The tile index within the chunk.
*/
chunktileindex_t worldPosToChunkTileIndex(const worldpos_t* worldPos);
/**
* Converts a chunk position to a world position.
*
* @param worldPos The world position.
* @param out The output chunk position.
*/
chunkindex_t chunkPosToIndex(const chunkpos_t* pos);

View File

@@ -6,8 +6,5 @@
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
scenemanager.c
)
# Subdirs
add_subdirectory(scene)
scene.c
)

84
src/scene/scene.c Normal file
View File

@@ -0,0 +1,84 @@
// Copyright (c) 2026 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "scene.h"
#include "assert/assert.h"
#include "util/string.h"
const scene_t SCENES[] = {
};
uint_fast8_t SCENE_CURRENT = 0xFF;
errorret_t sceneInit(void) {
// Initialize the scene subsystem here
errorOk();
}
void sceneUpdate(void) {
const scene_t *current = sceneGetCurrent();
if(current && current->update) {
current->update();
}
}
void sceneRender(void) {
const scene_t *current = sceneGetCurrent();
if(current && current->render) {
current->render();
}
}
void sceneDispose(void) {
const scene_t *current = sceneGetCurrent();
if(current && current->dispose) {
current->dispose();
}
}
errorret_t sceneSet(const scene_t *scene) {
sceneDispose();
if(scene) {
SCENE_CURRENT = (uint_fast8_t)(scene - SCENES);
assertTrue(
SCENE_CURRENT < sizeof(SCENES) / sizeof(scene_t),
"Invalid scene index."
);
if(scene->init) {
errorret_t err = scene->init();
if(err.code != ERROR_OK) SCENE_CURRENT = 0xFF;
errorChain(err);
}
} else {
SCENE_CURRENT = 0xFF;
}
errorOk();
}
const scene_t* sceneGetCurrent(void) {
if(SCENE_CURRENT == 0xFF) return NULL;
assertTrue(
SCENE_CURRENT < sizeof(SCENES) / sizeof(scene_t),
"Invalid current scene index."
);
return &SCENES[SCENE_CURRENT];
}
const scene_t* sceneGetByName(const char_t *name) {
for(uint_fast8_t i = 0; i < sizeof(SCENES) / sizeof(scene_t); i++) {
if(stringCompare(SCENES[i].name, name) == 0) {
return &SCENES[i];
}
}
return NULL;
}

View File

@@ -1,24 +1,63 @@
/**
* Copyright (c) 2025 Dominic Masters
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
#include "error/error.h"
#include "display/color.h"
#define SCENE_FLAG_INITIALIZED (1 << 0)
typedef struct scenedata_s scenedata_t;
typedef struct {
const char_t *name;
errorret_t (*init)(scenedata_t *data);
void (*update)(scenedata_t *data);
void (*render)(scenedata_t *data);
void (*dispose)(scenedata_t *data);
uint8_t flags;
} scene_t;
errorret_t (*init)(void);
void (*update)(void);
void (*render)(void);
void (*dispose)(void);
} scene_t;
extern const scene_t SCENES[];
extern uint_fast8_t SCENE_CURRENT;
/**
* Initialize the scene subsystem.
*/
errorret_t sceneInit(void);
/**
* Update the current scene.
*/
void sceneUpdate(void);
/**
* Render the current scene.
*/
void sceneRender(void);
/**
* Dispose of the scene subsystem.
*/
void sceneDispose(void);
/**
* Set the current scene.
*
* @param sceneIndex The index of the scene to set.
* @return An error code indicating success or failure.
*/
errorret_t sceneSet(const scene_t *scene);
/**
* Get the current scene.
*
* @return The current scene.
*/
const scene_t* sceneGetCurrent(void);
/**
* Get a scene by its name.
*
* @param name The name of the scene.
* @return The scene with the given name, or NULL if not found.
*/
const scene_t* sceneGetByName(const char_t *name);

View File

@@ -1,13 +0,0 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
scenetest.c
scenemap.c
)
# Subdirs

View File

@@ -1,212 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scenemap.h"
#include "scene/scenedata.h"
#include "display/spritebatch.h"
#include "assert/assert.h"
#include "asset/asset.h"
#include "rpg/entity/entity.h"
#include "rpg/world/map.h"
#include "display/screen.h"
#include "rpg/rpgcamera.h"
#include "util/memory.h"
#include "duskdefs.h"
errorret_t sceneMapInit(scenedata_t *data) {
// Init the camera.
cameraInitPerspective(&data->sceneMap.camera);
data->sceneMap.camera.projType = CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED;
data->sceneMap.camera.viewType = CAMERA_VIEW_TYPE_LOOKAT_PIXEL_PERFECT;
glm_vec3_zero(data->sceneMap.camera.lookatPixelPerfect.offset);
data->sceneMap.camera.lookatPixelPerfect.offset[1] = RPG_CAMERA_Z_OFFSET;
glm_vec3_copy(
(vec3){ 0.0f, 0.0f, 0.0f },
data->sceneMap.camera.lookatPixelPerfect.target
);
glm_vec3_copy(
(vec3){ 0.0f, 1.0f, 0.0f },
data->sceneMap.camera.lookatPixelPerfect.up
);
data->sceneMap.camera.lookatPixelPerfect.pixelsPerUnit = (
RPG_CAMERA_PIXELS_PER_UNIT
);
data->sceneMap.camera.perspective.fov = glm_rad(RPG_CAMERA_FOV);
errorOk();
}
void sceneMapUpdate(scenedata_t *data) {
}
void sceneMapGetWorldPosition(const worldpos_t pos, vec3 outPosition) {
assertNotNull(outPosition, "Output position cannot be NULL");
outPosition[0] = pos.x * TILE_WIDTH;
outPosition[1] = pos.y * TILE_HEIGHT;
outPosition[2] = pos.z * TILE_DEPTH;
// Handle stair tiles.
tile_t tile = mapGetTile(pos);
if(tileIsRamp(tile)) {
outPosition[2] += TILE_DEPTH / 2.0f;
}
}
void sceneMapEntityGetPosition(const entity_t *entity, vec3 outPosition) {
assertNotNull(entity, "Entity cannot be NULL");
assertNotNull(outPosition, "Output position cannot be NULL");
// Get position
sceneMapGetWorldPosition(entity->position, outPosition);
// Add a small offset so we render above the tile
outPosition[2] += 0.1f;
// Add animation offset(s)
switch(entity->animation) {
case ENTITY_ANIM_WALK: {
float_t animPercentage = entity->animTime / ENTITY_ANIM_WALK_DURATION;
vec3 lastPosition;
sceneMapGetWorldPosition(entity->lastPosition, lastPosition);
vec3 offset;
glm_vec3_sub(outPosition, lastPosition, offset);
glm_vec3_scale(offset, -animPercentage, offset);
glm_vec3_add(outPosition, offset, outPosition);
break;
}
default:
break;
}
}
void sceneMapRender(scenedata_t *data) {
if(!mapIsLoaded()) return;
// Look at target.
vec3 cameraTarget;
switch(RPG_CAMERA.mode) {
case RPG_CAMERA_MODE_FREE:
sceneMapGetWorldPosition(RPG_CAMERA.free, cameraTarget);
break;
case RPG_CAMERA_MODE_FOLLOW_ENTITY: {
const entity_t *ent = &ENTITIES[RPG_CAMERA.followEntity.followEntityId];
sceneMapEntityGetPosition(ent, cameraTarget);
break;
}
default:
glm_vec3_zero(cameraTarget);
break;
}
glm_vec3_copy(
cameraTarget,
data->sceneMap.camera.lookatPixelPerfect.target
);
// Push camera
cameraPushMatrix(&data->sceneMap.camera);
// Render map probably.
sceneMapRenderMap();
// Render ents
entity_t *ent = ENTITIES;
do {
sceneMapRenderEntity(ent);
} while(++ent, ent < &ENTITIES[ENTITY_COUNT]);
spriteBatchFlush();
// Finished, pop back camera.
cameraPopMatrix();
}
void sceneMapRenderEntity(entity_t *entity) {
assertNotNull(entity, "Entity cannot be NULL");
if(entity->type == ENTITY_TYPE_NULL) return;
vec3 posMin, posMax;
vec3 size = { TILE_WIDTH, TILE_HEIGHT, TILE_DEPTH };
sceneMapEntityGetPosition(entity, posMin);
glm_vec3_add(posMin, size, posMax);
// TEST: Change color depending on dir.
color_t testColor;
switch(entity->direction) {
case ENTITY_DIR_NORTH:
testColor = COLOR_BLUE;
break;
case ENTITY_DIR_EAST:
testColor = COLOR_GREEN;
break;
case ENTITY_DIR_SOUTH:
testColor = COLOR_CYAN;
break;
case ENTITY_DIR_WEST:
testColor = COLOR_YELLOW;
break;
default:
testColor = COLOR_WHITE;
break;
}
vec2 uv0 = { 0.0f, 0.0f };
vec2 uv1 = { 1.0f, 1.0f };
spriteBatchPush3D(NULL, posMin, posMax, testColor, uv0, uv1);
}
void sceneMapRenderMap() {
assertTrue(mapIsLoaded(), "No map loaded to render");
// For each chunk.
for(uint32_t i = 0; i < MAP_CHUNK_COUNT; i++) {
chunk_t *chunk = MAP.chunkOrder[i];
for(uint8_t j = 0; j < chunk->meshCount; j++) {
mesh_t *mesh = &chunk->meshes[j];
if(mesh->vertexCount == 0) continue;
textureBind(NULL);
meshDraw(mesh, -1, -1);
}
// vec3 min, max;
// min[0] = chunk->position.x * CHUNK_WIDTH * TILE_WIDTH;
// min[1] = chunk->position.y * CHUNK_HEIGHT * TILE_HEIGHT;
// min[2] = chunk->position.z * CHUNK_DEPTH * TILE_DEPTH;
// max[0] = min[0] + (CHUNK_WIDTH * TILE_WIDTH);
// max[1] = min[1] + (CHUNK_HEIGHT * TILE_HEIGHT);
// max[2] = min[2];
// color_t color = COLOR_WHITE;
// if(chunk->position.x % 2 == 0) {
// color = (chunk->position.y % 2 == 0) ? COLOR_BLACK : COLOR_WHITE;
// } else {
// color = (chunk->position.y % 2 == 0) ? COLOR_WHITE : COLOR_BLACK;
// }
// spriteBatchPush3D(
// NULL,
// min,
// max,
// color,
// (vec2){ 0.0f, 0.0f },
// (vec2){ 1.0f, 1.0f }
// );
}
}
void sceneMapDispose(scenedata_t *data) {
}

View File

@@ -1,32 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "scene/scene.h"
#include "rpg/entity/entity.h"
#include "display/camera.h"
#include "asset/asset.h"
typedef struct {
camera_t camera;
} scenemap_t;
errorret_t sceneMapInit(scenedata_t *data);
void sceneMapUpdate(scenedata_t *data);
void sceneMapRender(scenedata_t *data);
void sceneMapRenderEntity(entity_t *entity);
void sceneMapRenderMap();
void sceneMapDispose(scenedata_t *data);
static scene_t SCENE_MAP = {
.name = "map",
.init = sceneMapInit,
.update = sceneMapUpdate,
.render = sceneMapRender,
.dispose = sceneMapDispose,
.flags = 0
};

View File

@@ -1,26 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scenetest.h"
#include "scene/scenedata.h"
errorret_t sceneTestInit(scenedata_t *data) {
data->sceneTest.nothing = 0;
errorOk();
}
void sceneTestUpdate(scenedata_t *data) {
}
void sceneTestRender(scenedata_t *data) {
}
void sceneTestDispose(scenedata_t *data) {
}

View File

@@ -1,27 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "scene/scene.h"
typedef struct {
int32_t nothing;
} scenetest_t;
errorret_t sceneTestInit(scenedata_t *data);
void sceneTestUpdate(scenedata_t *data);
void sceneTestRender(scenedata_t *data);
void sceneTestDispose(scenedata_t *data);
static scene_t SCENE_TEST = {
.name = "test",
.init = sceneTestInit,
.update = sceneTestUpdate,
.render = sceneTestRender,
.dispose = sceneTestDispose,
.flags = 0
};

View File

@@ -1,19 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "scene/scene.h"
#include "scene/scene/scenetest.h"
#include "scene/scene/scenemap.h"
typedef struct scenedata_s {
union {
scenetest_t sceneTest;
scenemap_t sceneMap;
};
} scenedata_t;

View File

@@ -1,119 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scenemanager.h"
#include "util/memory.h"
#include "assert/assert.h"
#include "display/framebuffer.h"
#include "util/string.h"
#include "asset/asset.h"
#include "script/scriptmanager.h"
scenemanager_t SCENE_MANAGER;
errorret_t sceneManagerInit(void) {
memoryZero(&SCENE_MANAGER, sizeof(scenemanager_t));
sceneManagerRegisterScene(&SCENE_TEST);
sceneManagerRegisterScene(&SCENE_MAP);
errorOk();
}
scene_t * sceneManagerGetSceneByName(const char_t *name) {
assertNotNull(name, "Name is null");
for(uint8_t i = 0; i < SCENE_MANAGER.sceneCount; i++) {
if(strcmp(SCENE_MANAGER.scenes[i]->name, name) != 0) continue;
return SCENE_MANAGER.scenes[i];
}
return NULL;
}
void sceneManagerRegisterScene(scene_t *scene) {
assertNotNull(scene, "Scene is null");
assertTrue(
SCENE_MANAGER.sceneCount < SCENE_MANAGER_SCENE_COUNT_MAX,
"Scene count exceeded max"
);
assertNotNull(scene->name, "Scene name is null");
assertNull(
sceneManagerGetSceneByName(scene->name), "Scene name already registered"
);
SCENE_MANAGER.scenes[SCENE_MANAGER.sceneCount++] = scene;
}
errorret_t sceneManagerSetScene(scene_t *scene) {
if(
SCENE_MANAGER.current &&
(SCENE_MANAGER.current->flags & SCENE_FLAG_INITIALIZED) != 0
) {
SCENE_MANAGER.current->flags &= ~SCENE_FLAG_INITIALIZED;
if(SCENE_MANAGER.current->dispose) {
SCENE_MANAGER.current->dispose(&SCENE_MANAGER.sceneData);
}
}
SCENE_MANAGER.current = scene;
if(scene && (scene->flags & SCENE_FLAG_INITIALIZED) == 0) {
scene->flags |= SCENE_FLAG_INITIALIZED;
if(scene->init) errorChain(scene->init(&SCENE_MANAGER.sceneData));
// Execute scene script if it exists
char_t buffer[256];
snprintf(buffer, sizeof(buffer), "scene/%s.dsf", scene->name);
if(assetFileExists(buffer)) {
scriptcontext_t ctx;
scriptContextInit(&ctx);
errorChain(scriptContextExecFile(&ctx, buffer));
scriptContextDispose(&ctx);
}
}
errorOk();
}
void sceneManagerUpdate(void) {
if(!SCENE_MANAGER.current) return;
assertTrue(
SCENE_MANAGER.current->flags & SCENE_FLAG_INITIALIZED,
"Current scene not initialized"
);
if(SCENE_MANAGER.current->update) {
SCENE_MANAGER.current->update(&SCENE_MANAGER.sceneData);
}
}
void sceneManagerRender(void) {
if(!SCENE_MANAGER.current) return;
assertTrue(
SCENE_MANAGER.current->flags & SCENE_FLAG_INITIALIZED,
"Current scene not initialized"
);
if(SCENE_MANAGER.current->render) {
SCENE_MANAGER.current->render(&SCENE_MANAGER.sceneData);
}
}
void sceneManagerDispose(void) {
for(uint8_t i = 0; i < SCENE_MANAGER.sceneCount; i++) {
scene_t *scene = SCENE_MANAGER.scenes[i];
if(scene->flags & SCENE_FLAG_INITIALIZED) {
scene->dispose(&SCENE_MANAGER.sceneData);
}
}
SCENE_MANAGER.sceneCount = 0;
}

View File

@@ -1,64 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "scene.h"
#include "scenedata.h"
#define SCENE_MANAGER_SCENE_COUNT_MAX 16
typedef struct {
scene_t *current;
scene_t *scenes[SCENE_MANAGER_SCENE_COUNT_MAX];
uint8_t sceneCount;
scenedata_t sceneData;
} scenemanager_t;
extern scenemanager_t SCENE_MANAGER;
/**
* Initializes the scene manager and the initial scene.
*/
errorret_t sceneManagerInit(void);
/**
* Retrieves a registered scene by its name.
*
* @param name The name of the scene to retrieve.
* @return The scene with the specified name, or NULL if not found.
*/
scene_t * sceneManagerGetSceneByName(const char_t *name);
/**
* Registers a scene with the scene manager.
*
* @param scene The scene to register.
*/
void sceneManagerRegisterScene(scene_t *scene);
/**
* Sets the current active scene.
*
* @param scene The scene to set as current.
* @return An error code indicating success or failure.
*/
errorret_t sceneManagerSetScene(scene_t *scene);
/**
* Updates all active scenes.
*/
void sceneManagerUpdate(void);
/**
* Renders all visible scenes.
*/
void sceneManagerRender(void);
/**
* Disposes of all scenes.
*/
void sceneManagerDispose(void);

View File

@@ -1,49 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/scriptcontext.h"
#include "debug/debug.h"
#include "assert/assert.h"
#include "rpg/world/map.h"
int moduleMapLoad(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
if(!lua_isstring(L, 1)) {
luaL_error(L, "Expected string map filename");
return 0;
}
// Potentially provide up to 3 params
chunkpos_t initial = { .x = 0, .y = 0, .z = 0 };
if(lua_isnumber(L, 2)) {
initial.x = (chunkunit_t)luaL_checkinteger(L, 2);
}
if(lua_isnumber(L, 3)) {
initial.y = (chunkunit_t)luaL_checkinteger(L, 3);
}
if(lua_isnumber(L, 4)) {
initial.z = (chunkunit_t)luaL_checkinteger(L, 4);
}
// Load the map.
errorret_t ret = mapLoad(luaL_checkstring(L, 1), initial);
if(ret.code != ERROR_OK) {
luaL_error(L, "Failed to load map");
errorCatch(errorPrint(ret));
return 0;
}
return 0;
}
void moduleMapSystem(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
scriptContextRegFunc(context, "mapLoad", moduleMapLoad);
}

View File

@@ -7,7 +7,7 @@
#pragma once
#include "script/scriptcontext.h"
#include "scene/scenemanager.h"
#include "scene/scene.h"
int moduleSceneSetScene(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
@@ -20,13 +20,13 @@ int moduleSceneSetScene(lua_State *L) {
return 0;
}
scene_t *scene = sceneManagerGetSceneByName(sceneName);
const scene_t *scene = sceneGetByName(sceneName);
if(scene == NULL) {
luaL_error(L, "Scene '%s' not found", sceneName);
return 0;
}
errorret_t err = sceneManagerSetScene(scene);
errorret_t err = sceneSet(scene);
if(err.code != ERROR_OK) {
luaL_error(L, "Failed to set scene '%s'", sceneName);
return 0;

View File

@@ -1,135 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/scriptcontext.h"
#include "rpg/entity/entity.h"
#include "assert/assert.h"
int scriptFuncEntityAdd(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
assertTrue(lua_isinteger(L, 1), "Expected integer entity type");
lua_Integer entityType = luaL_checkinteger(L, 1);
assertTrue(
entityType >= ENTITY_TYPE_NULL && entityType < ENTITY_TYPE_COUNT,
"Invalid entity type passed to scriptFuncEntityAdd"
);
// Pop entity
uint8_t available = entityGetAvailable();
if(available == 0xFF) {
lua_pushnil(L);
return 1;
}
entity_t *ent = &ENTITIES[available];
entityInit(ent, (entitytype_t)entityType);
// May include X, Y and/or Z
if(lua_isinteger(L, 2)) {
lua_Integer xPos = luaL_checkinteger(L, 2);
ent->position.x = (int32_t)xPos;
}
if(lua_isinteger(L, 3)) {
lua_Integer yPos = luaL_checkinteger(L, 3);
ent->position.y = (int32_t)yPos;
}
if(lua_isinteger(L, 4)) {
lua_Integer zPos = luaL_checkinteger(L, 4);
ent->position.z = (int32_t)zPos;
}
// Send entity id.
lua_pushinteger(L, ent->id);
return 1;
}
int scriptFuncEntitySetX(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
assertTrue(lua_isinteger(L, 1), "Expected integer entity id");
assertTrue(lua_isinteger(L, 2), "Expected integer x position");
lua_Integer entityId = luaL_checkinteger(L, 1);
lua_Integer xPos = luaL_checkinteger(L, 2);
assertTrue(
entityId >= 0 && entityId < ENTITY_COUNT,
"Invalid entity id passed to scriptFuncEntitySetX"
);
entity_t *ent = &ENTITIES[entityId];
assertTrue(
ent->type != ENTITY_TYPE_NULL,
"Cannot set position of NULL entity in scriptFuncEntitySetX"
);
ent->position.x = (int32_t)xPos;
return 0;
}
int scriptFuncEntitySetY(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
assertTrue(lua_isinteger(L, 1), "Expected integer entity id");
assertTrue(lua_isinteger(L, 2), "Expected integer y position");
lua_Integer entityId = luaL_checkinteger(L, 1);
lua_Integer yPos = luaL_checkinteger(L, 2);
assertTrue(
entityId >= 0 && entityId < ENTITY_COUNT,
"Invalid entity id passed to scriptFuncEntitySetY"
);
entity_t *ent = &ENTITIES[entityId];
assertTrue(
ent->type != ENTITY_TYPE_NULL,
"Cannot set position of NULL entity in scriptFuncEntitySetY"
);
ent->position.y = (int32_t)yPos;
return 0;
}
int scriptFuncEntitySetZ(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
assertTrue(lua_isinteger(L, 1), "Expected integer entity id");
assertTrue(lua_isinteger(L, 2), "Expected integer z position");
lua_Integer entityId = luaL_checkinteger(L, 1);
lua_Integer zPos = luaL_checkinteger(L, 2);
assertTrue(
entityId >= 0 && entityId < ENTITY_COUNT,
"Invalid entity id passed to scriptFuncEntitySetZ"
);
entity_t *ent = &ENTITIES[entityId];
assertTrue(
ent->type != ENTITY_TYPE_NULL,
"Cannot set position of NULL entity in scriptFuncEntitySetZ"
);
ent->position.z = (int32_t)zPos;
return 0;
}
void scriptFuncEntity(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
scriptContextRegFunc(context, "entityAdd", scriptFuncEntityAdd);
scriptContextRegFunc(context, "entitySetX", scriptFuncEntitySetX);
scriptContextRegFunc(context, "entitySetY", scriptFuncEntitySetY);
scriptContextRegFunc(context, "entitySetZ", scriptFuncEntitySetZ);
}

View File

@@ -10,14 +10,12 @@
#include "script/module/moduleinput.h"
#include "script/module/moduleplatform.h"
#include "script/module/modulescene.h"
#include "script/module/modulemap.h"
const scriptmodule_t SCRIPT_MODULE_LIST[] = {
{ .name = "system", .callback = moduleSystem },
{ .name = "input", .callback = moduleInput },
{ .name = "platform", .callback = modulePlatform },
{ .name = "scene", .callback = moduleScene },
{ .name = "map", .callback = moduleMapSystem },
};
#define SCRIPT_MODULE_COUNT ( \

View File

@@ -26,6 +26,7 @@ void timeInit(void) {
TIME.dynamicTime = TIME_STEP;
TIME.dynamicDelta = TIME_STEP;
TIME.dynamicUpdate = false;
TIME.lastNonDynamic = TIME.dynamicTime;
#endif
}
@@ -44,8 +45,9 @@ void timeUpdate(void) {
assertTrue(TIME.dynamicDelta >= 0.0f, "Time delta is negative");
// Is within 1ms of a full step?
if(TIME.dynamicTime - TIME.time >= TIME_STEP * 0.999f) {
if(TIME.dynamicTime - TIME.lastNonDynamic >= TIME_STEP * 0.999f) {
TIME.dynamicUpdate = false;
TIME.lastNonDynamic = TIME.dynamicTime;
TIME.delta = TIME_STEP;
TIME.time += TIME_STEP;
}

View File

@@ -18,6 +18,7 @@ typedef struct {
float_t time;
#if TIME_FIXED == 0
float_t lastNonDynamic;
bool_t dynamicUpdate;
float_t dynamicDelta;
float_t dynamicTime;

View File

@@ -11,7 +11,7 @@
#include "util/memory.h"
#include "display/tileset/tileset_minogram.h"
#include "display/screen.h"
#include "ui/uitextbox.h"
// #include "ui/uitextbox.h"
ui_t UI;
@@ -33,7 +33,7 @@ void uiUpdate(void) {
UI.camera.orthographic.top = 0;
UI.camera.orthographic.bottom = SCREEN.height;
uiTextboxUpdate();
// uiTextboxUpdate();
}
void uiRender(void) {
@@ -42,7 +42,7 @@ void uiRender(void) {
// Render UI elements here
if(UI.fontTexture.width > 0) {
uiDebugRender(UI.fontTileset, &UI.fontTexture);
uiTextboxRender();
// uiTextboxRender();
}
cameraPopMatrix();
}

View File

@@ -11,7 +11,7 @@
#include "ui/uitext.h"
#include "display/screen.h"
#include "display/spritebatch.h"
#include "rpg/entity/entity.h"
// #include "rpg/entity/entity.h"
bool_t UI_DEBUG_DRAW = true;
@@ -67,32 +67,32 @@ void uiDebugRender(const tileset_t *tileset, texture_t *texture) {
// Player position
entity_t *player = NULL;
for(uint8_t i = 0; i < ENTITY_COUNT; i++) {
if(ENTITIES[i].type != ENTITY_TYPE_PLAYER) continue;
player = &ENTITIES[i];
break;
}
if(player == NULL) {
snprintf(buffer, sizeof(buffer), "Player: N/A");
} else {
snprintf(
buffer,
sizeof(buffer),
"%d,%d,%d/%d/%d",
player->position.x,
player->position.y,
player->position.z,
(int32_t)player->direction,
(int32_t)player->animation
);
}
uiTextMeasure(buffer, tileset, &w, &h);
uiTextDraw(
SCREEN.width - w, hOffset,
buffer, COLOR_GREEN, tileset, texture
);
hOffset += h;
// entity_t *player = NULL;
// for(uint8_t i = 0; i < ENTITY_COUNT; i++) {
// if(ENTITIES[i].type != ENTITY_TYPE_PLAYER) continue;
// player = &ENTITIES[i];
// break;
// }
// if(player == NULL) {
// snprintf(buffer, sizeof(buffer), "Player: N/A");
// } else {
// snprintf(
// buffer,
// sizeof(buffer),
// "%d,%d,%d/%d/%d",
// player->position.x,
// player->position.y,
// player->position.z,
// (int32_t)player->direction,
// (int32_t)player->animation
// );
// }
// uiTextMeasure(buffer, tileset, &w, &h);
// uiTextDraw(
// SCREEN.width - w, hOffset,
// buffer, COLOR_GREEN, tileset, texture
// );
// hOffset += h;
spriteBatchFlush();
}

View File

@@ -8,38 +8,38 @@
#include "uitextbox.h"
#include "ui/ui.h"
#include "ui/uitext.h"
#include "rpg/rpgtextbox.h"
// #include "rpg/rpgtextbox.h"
#include "display/screen.h"
#include "display/spritebatch.h"
#include "input/input.h"
void uiTextboxUpdate() {
if(!rpgTextboxIsVisible()) return;
// void uiTextboxUpdate() {
// if(!rpgTextboxIsVisible()) return;
if(inputPressed(INPUT_ACTION_ACCEPT)) {
rpgTextboxHide();
}
}
// if(inputPressed(INPUT_ACTION_ACCEPT)) {
// rpgTextboxHide();
// }
// }
void uiTextboxRender() {
if(!rpgTextboxIsVisible()) return;
// void uiTextboxRender() {
// if(!rpgTextboxIsVisible()) return;
const char_t *text = RPG_TEXTBOX.text;
int32_t textWidth, textHeight;
// const char_t *text = RPG_TEXTBOX.text;
// int32_t textWidth, textHeight;
uiTextMeasure(text, UI.fontTileset, &textWidth, &textHeight);
// uiTextMeasure(text, UI.fontTileset, &textWidth, &textHeight);
float_t y = 0;
if(RPG_TEXTBOX.position == RPG_TEXTBOX_POS_BOTTOM) {
y = SCREEN.height - (float_t)textHeight;
}
// float_t y = 0;
// if(RPG_TEXTBOX.position == RPG_TEXTBOX_POS_BOTTOM) {
// y = SCREEN.height - (float_t)textHeight;
// }
spriteBatchPush(
NULL,
0.0f, y,
(float_t)SCREEN.width, (float_t)(y + textHeight),
COLOR_BLACK,
0.0f, 0.0f, 1.0f, 1.0f
);
uiTextDraw(0, y, text, COLOR_RED, UI.fontTileset, &UI.fontTexture);
}
// spriteBatchPush(
// NULL,
// 0.0f, y,
// (float_t)SCREEN.width, (float_t)(y + textHeight),
// COLOR_BLACK,
// 0.0f, 0.0f, 1.0f, 1.0f
// );
// uiTextDraw(0, y, text, COLOR_RED, UI.fontTileset, &UI.fontTexture);
// }