Cleanup overworld.

This commit is contained in:
2025-03-04 10:51:22 -06:00
parent d53b5eab49
commit ad9e8a78d3
20 changed files with 229 additions and 42 deletions

View File

@ -126,4 +126,6 @@ void assertMemoryRangeMatchesImpl(
assertTrue(strlen(str) <= len, message)
#define assertStrLenMin(str, len, message) \
assertTrue(strlen(str) >= len, message)
assertTrue(strlen(str) >= len, message)
// EOF

View File

@ -6,10 +6,9 @@
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
entity.c
overworld.c
player.c
tile.c
)
# Subdirs
# Subdirs
add_subdirectory(entity)
add_subdirectory(map)

View File

@ -0,0 +1,15 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
entity.c
facing.c
player.c
npc.c
)
# Subdirs

View File

@ -8,11 +8,12 @@
#include "entity.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "overworld.h"
#include "overworld/overworld.h"
entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = {
{ NULL, NULL },
{ playerInit, playerUpdate }
{ NULL, NULL, NULL },
{ playerInit, playerUpdate, NULL },
{ npcInit, npcUpdate, npcInteract }
};
entity_t ENTITY_TEST;
@ -69,26 +70,11 @@ void entityMove(entity_t *ent, const facingdir_t dir) {
entityTurn(ent, dir);
uint8_t targetX = ent->x, targetY = ent->y;
switch(dir) {
case FACING_DIRECTION_EAST:
if(ent->x >= OVERWORLD.mapWidth - 1) return;
targetX++;
break;
case FACING_DIRECTION_WEST:
if(ent->x == 0) return;
targetX--;
break;
case FACING_DIRECTION_NORTH:
if(ent->y >= OVERWORLD.mapHeight - 1) return;
targetY++;
break;
case FACING_DIRECTION_SOUTH:
if(ent->y == 0) return;
targetY--;
break;
default:
assertUnreachable("Invalid facing direction");
}
facingDirAdd(ent->direction, &targetX, &targetY);
// Check oob
if(targetX < 0 || targetX >= OVERWORLD.mapWidth) return;
if(targetY < 0 || targetY >= OVERWORLD.mapHeight) return;
// Check tile at target
uint8_t i = 0;

View File

@ -6,28 +6,24 @@
*/
#pragma once
#include "facing.h"
#include "player.h"
#include "npc.h"
typedef enum {
ENTITY_TYPE_NULL,
ENTITY_TYPE_PLAYER,
ENTITY_TYPE_NPC
} entitytype_t;
#define ENTITY_TYPE_COUNT 2
#define ENTITY_TYPE_COUNT 3
typedef struct {
void (*init)(entity_t *);
void (*update)(entity_t *);
void (*interact)(entity_t *);
} entitycallback_t;
typedef enum {
FACING_DIRECTION_SOUTH,
FACING_DIRECTION_EAST,
FACING_DIRECTION_NORTH,
FACING_DIRECTION_WEST
} facingdir_t;
#define FACING_DIRECTION_COUNT 4
typedef struct _entity_t {
uint8_t x, y;
int8_t subX, subY;

View File

@ -0,0 +1,50 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "facing.h"
#include "assert/assert.h"
void facingDirGetRelative(const facingdir_t dir, int8_t *x, int8_t *y) {
assertNotNull(x, "X cannot be NULL");
assertNotNull(y, "Y cannot be NULL");
switch(dir) {
case FACING_DIRECTION_SOUTH:
*x = 0;
*y = -1;
break;
case FACING_DIRECTION_EAST:
*x = 1;
*y = 0;
break;
case FACING_DIRECTION_NORTH:
*x = 0;
*y = 1;
break;
case FACING_DIRECTION_WEST:
*x = -1;
*y = 0;
break;
default:
assertUnreachable("Invalid facing direction.");
}
}
void facingDirAdd(const facingdir_t dir, uint8_t *x, uint8_t *y) {
assertNotNull(x, "X cannot be NULL");
assertNotNull(y, "Y cannot be NULL");
int8_t dx, dy;
facingDirGetRelative(dir, &dx, &dy);
*x += dx;
*y += dy;
}

View File

@ -0,0 +1,36 @@
/**
* 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 {
FACING_DIRECTION_SOUTH,
FACING_DIRECTION_EAST,
FACING_DIRECTION_NORTH,
FACING_DIRECTION_WEST
} facingdir_t;
#define FACING_DIRECTION_COUNT 4
/**
* Converts a facing direction to a directional vector.
*
* @param dir The direction to convert.
* @param x The x component of the vector.
* @param y The y component of the vector.
*/
void facingDirGetRelative(const facingdir_t dir, int8_t *x, int8_t *y);
/**
* Adds a facing direction to a position.
*
* @param dir The direction to add.
* @param x The x position to add to.
* @param y The y position to add to.
*/
void facingDirAdd(const facingdir_t dir, uint8_t *x, uint8_t *y);

View File

@ -0,0 +1,17 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "npc.h"
void npcInit(entity_t *ent) {
}
void npcUpdate(entity_t *ent) {
}
void npcInteract(entity_t *ent) {
}

View File

@ -0,0 +1,36 @@
/**
* 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_t entity_t;
typedef struct {
int32_t nothing;
} npc_t;
/**
* Handles initialization of an NPC.
*
* @param ent The entity to initialize as an NPC.
*/
void npcInit(entity_t *ent);
/**
* Handles updating an NPC.
*
* @param ent The entity to update as an NPC.
*/
void npcUpdate(entity_t *ent);
/**
* Handles interaction with an NPC.
*
* @param ent The entity to interact with.
*/
void npcInteract(entity_t *ent);

View File

@ -5,7 +5,7 @@
* https://opensource.org/licenses/MIT
*/
#include "entity.h"
#include "overworld/overworld.h"
#include "assert/assert.h"
#include "input.h"
@ -26,6 +26,7 @@ void playerUpdate(entity_t *ent) {
entityMove(ent, FACING_DIRECTION_NORTH);
} else if(inputIsDown(INPUT_DOWN)) {
entityMove(ent, FACING_DIRECTION_SOUTH);
} else if(inputWasPressed(INPUT_ACCEPT)) {
}
}

View File

@ -0,0 +1,12 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
tile.c
)
# Subdirs

View File

@ -0,0 +1,14 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "map.h"
#include "assert/assert.h"
#include "util/memory.h"
void mapInit(map_t *map) {
memoryZero(map, sizeof(map_t));
}

View File

@ -0,0 +1,18 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "tile.h"
#include "overworld/overworlddefs.h"
typedef struct {
tileid_t tileIds[OVERWORLD_TILE_COUNT_MAX];
tiledata_t tileData[OVERWORLD_TILE_COUNT_MAX];
uint8_t width, height, layerCount;
} map_t;
void mapInit(map_t *map);

View File

@ -29,6 +29,9 @@ void overworldInit() {
);
entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_PLAYER);
entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_NPC);
OVERWORLD.entities[1].x = 2;
OVERWORLD.entities[1].y = 2;
}
void overworldSceneInit() {

View File

@ -7,8 +7,8 @@
#pragma once
#include "overworlddefs.h"
#include "entity.h"
#include "tile.h"
#include "overworld/entity/entity.h"
#include "overworld/map/tile.h"
typedef struct {
entity_t entities[OVERWORLD_ENTITY_COUNT_MAX];

View File

@ -14,7 +14,7 @@ shaderbuffer_t ENTITIES_BUFFER;
entitiesdata_t ENTITIES_DATA;
void entitiesInit() {
memset(&ENTITIES_DATA, 0, sizeof(entitiesdata_t));
memoryZero(&ENTITIES_DATA, sizeof(entitiesdata_t));
shaderBufferInit(&ENTITIES_BUFFER, sizeof(overworld_t));
}

View File

@ -12,7 +12,9 @@
void overworldRender() {
mapShaderUse();
quadRender(OVERWORLD.mapWidth*OVERWORLD.mapHeight*OVERWORLD.mapLayerCount);
quadRender(
OVERWORLD.mapWidth * OVERWORLD.mapHeight * OVERWORLD.mapLayerCount
);
entityShaderUse();
quadRender(OVERWORLD.entityCount);