Cleanup overworld.
This commit is contained in:
@ -126,4 +126,6 @@ void assertMemoryRangeMatchesImpl(
|
|||||||
assertTrue(strlen(str) <= len, message)
|
assertTrue(strlen(str) <= len, message)
|
||||||
|
|
||||||
#define assertStrLenMin(str, len, message) \
|
#define assertStrLenMin(str, len, message) \
|
||||||
assertTrue(strlen(str) >= len, message)
|
assertTrue(strlen(str) >= len, message)
|
||||||
|
|
||||||
|
// EOF
|
@ -6,10 +6,9 @@
|
|||||||
# Sources
|
# Sources
|
||||||
target_sources(${DUSK_TARGET_NAME}
|
target_sources(${DUSK_TARGET_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
entity.c
|
|
||||||
overworld.c
|
overworld.c
|
||||||
player.c
|
|
||||||
tile.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
|
add_subdirectory(entity)
|
||||||
|
add_subdirectory(map)
|
15
src/dusk/overworld/entity/CMakeLists.txt
Normal file
15
src/dusk/overworld/entity/CMakeLists.txt
Normal 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
|
@ -8,11 +8,12 @@
|
|||||||
#include "entity.h"
|
#include "entity.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
#include "overworld.h"
|
#include "overworld/overworld.h"
|
||||||
|
|
||||||
entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = {
|
entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = {
|
||||||
{ NULL, NULL },
|
{ NULL, NULL, NULL },
|
||||||
{ playerInit, playerUpdate }
|
{ playerInit, playerUpdate, NULL },
|
||||||
|
{ npcInit, npcUpdate, npcInteract }
|
||||||
};
|
};
|
||||||
|
|
||||||
entity_t ENTITY_TEST;
|
entity_t ENTITY_TEST;
|
||||||
@ -69,26 +70,11 @@ void entityMove(entity_t *ent, const facingdir_t dir) {
|
|||||||
entityTurn(ent, dir);
|
entityTurn(ent, dir);
|
||||||
|
|
||||||
uint8_t targetX = ent->x, targetY = ent->y;
|
uint8_t targetX = ent->x, targetY = ent->y;
|
||||||
switch(dir) {
|
facingDirAdd(ent->direction, &targetX, &targetY);
|
||||||
case FACING_DIRECTION_EAST:
|
|
||||||
if(ent->x >= OVERWORLD.mapWidth - 1) return;
|
// Check oob
|
||||||
targetX++;
|
if(targetX < 0 || targetX >= OVERWORLD.mapWidth) return;
|
||||||
break;
|
if(targetY < 0 || targetY >= OVERWORLD.mapHeight) return;
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check tile at target
|
// Check tile at target
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
@ -6,28 +6,24 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include "facing.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
#include "npc.h"
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
ENTITY_TYPE_NULL,
|
ENTITY_TYPE_NULL,
|
||||||
ENTITY_TYPE_PLAYER,
|
ENTITY_TYPE_PLAYER,
|
||||||
|
ENTITY_TYPE_NPC
|
||||||
} entitytype_t;
|
} entitytype_t;
|
||||||
|
|
||||||
#define ENTITY_TYPE_COUNT 2
|
#define ENTITY_TYPE_COUNT 3
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
void (*init)(entity_t *);
|
void (*init)(entity_t *);
|
||||||
void (*update)(entity_t *);
|
void (*update)(entity_t *);
|
||||||
|
void (*interact)(entity_t *);
|
||||||
} entitycallback_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 {
|
typedef struct _entity_t {
|
||||||
uint8_t x, y;
|
uint8_t x, y;
|
||||||
int8_t subX, subY;
|
int8_t subX, subY;
|
50
src/dusk/overworld/entity/facing.c
Normal file
50
src/dusk/overworld/entity/facing.c
Normal 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;
|
||||||
|
}
|
36
src/dusk/overworld/entity/facing.h
Normal file
36
src/dusk/overworld/entity/facing.h
Normal 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);
|
17
src/dusk/overworld/entity/npc.c
Normal file
17
src/dusk/overworld/entity/npc.c
Normal 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) {
|
||||||
|
}
|
36
src/dusk/overworld/entity/npc.h
Normal file
36
src/dusk/overworld/entity/npc.h
Normal 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);
|
@ -5,7 +5,7 @@
|
|||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "entity.h"
|
#include "overworld/overworld.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
@ -26,6 +26,7 @@ void playerUpdate(entity_t *ent) {
|
|||||||
entityMove(ent, FACING_DIRECTION_NORTH);
|
entityMove(ent, FACING_DIRECTION_NORTH);
|
||||||
} else if(inputIsDown(INPUT_DOWN)) {
|
} else if(inputIsDown(INPUT_DOWN)) {
|
||||||
entityMove(ent, FACING_DIRECTION_SOUTH);
|
entityMove(ent, FACING_DIRECTION_SOUTH);
|
||||||
|
} else if(inputWasPressed(INPUT_ACCEPT)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
12
src/dusk/overworld/map/CMakeLists.txt
Normal file
12
src/dusk/overworld/map/CMakeLists.txt
Normal 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
|
14
src/dusk/overworld/map/map.c
Normal file
14
src/dusk/overworld/map/map.c
Normal 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));
|
||||||
|
}
|
18
src/dusk/overworld/map/map.h
Normal file
18
src/dusk/overworld/map/map.h
Normal 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);
|
@ -29,6 +29,9 @@ void overworldInit() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_PLAYER);
|
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() {
|
void overworldSceneInit() {
|
||||||
|
@ -7,8 +7,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "overworlddefs.h"
|
#include "overworlddefs.h"
|
||||||
#include "entity.h"
|
#include "overworld/entity/entity.h"
|
||||||
#include "tile.h"
|
#include "overworld/map/tile.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
entity_t entities[OVERWORLD_ENTITY_COUNT_MAX];
|
entity_t entities[OVERWORLD_ENTITY_COUNT_MAX];
|
||||||
|
@ -14,7 +14,7 @@ shaderbuffer_t ENTITIES_BUFFER;
|
|||||||
entitiesdata_t ENTITIES_DATA;
|
entitiesdata_t ENTITIES_DATA;
|
||||||
|
|
||||||
void entitiesInit() {
|
void entitiesInit() {
|
||||||
memset(&ENTITIES_DATA, 0, sizeof(entitiesdata_t));
|
memoryZero(&ENTITIES_DATA, sizeof(entitiesdata_t));
|
||||||
shaderBufferInit(&ENTITIES_BUFFER, sizeof(overworld_t));
|
shaderBufferInit(&ENTITIES_BUFFER, sizeof(overworld_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,9 @@
|
|||||||
|
|
||||||
void overworldRender() {
|
void overworldRender() {
|
||||||
mapShaderUse();
|
mapShaderUse();
|
||||||
quadRender(OVERWORLD.mapWidth*OVERWORLD.mapHeight*OVERWORLD.mapLayerCount);
|
quadRender(
|
||||||
|
OVERWORLD.mapWidth * OVERWORLD.mapHeight * OVERWORLD.mapLayerCount
|
||||||
|
);
|
||||||
|
|
||||||
entityShaderUse();
|
entityShaderUse();
|
||||||
quadRender(OVERWORLD.entityCount);
|
quadRender(OVERWORLD.entityCount);
|
||||||
|
Reference in New Issue
Block a user