Progness
This commit is contained in:
11
src/overworld/CMakeLists.txt
Normal file
11
src/overworld/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
# 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
|
||||
map.c
|
||||
mapentity.c
|
||||
)
|
||||
61
src/overworld/map.c
Normal file
61
src/overworld/map.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "map.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/string.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
map_t MAP;
|
||||
|
||||
void mapInit(void) {
|
||||
memoryZero(&MAP, sizeof(map_t));
|
||||
}
|
||||
|
||||
mapentity_t * mapEntityAdd(const mapentitytype_t type) {
|
||||
assertTrue(type != MAP_ENTITY_TYPE_NULL, "Type cannot be NULL");
|
||||
assertTrue(type < MAP_ENTITY_TYPE_COUNT, "Type is out of range");
|
||||
|
||||
// Find any available entity.
|
||||
mapentity_t *start = &MAP.entities[0];
|
||||
mapentity_t *end = &MAP.entities[MAP_ENTITY_COUNT_MAX];
|
||||
do {
|
||||
if(start->type != MAP_ENTITY_TYPE_NULL) continue;
|
||||
mapEntityInit(start, type);
|
||||
return start;
|
||||
} while(start->type != MAP_ENTITY_TYPE_NULL && ++start < end);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void mapEntityRemove(mapentity_t *ent) {
|
||||
assertNotNull(ent, "Ent cannot be NULL");
|
||||
assertTrue(ent->type != MAP_ENTITY_TYPE_NULL, "Ent is already NULL");
|
||||
assertTrue(
|
||||
ent >= &MAP.entities[0] && ent < &MAP.entities[MAP_ENTITY_COUNT_MAX],
|
||||
"Ent is out of range"
|
||||
);
|
||||
|
||||
ent->type = MAP_ENTITY_TYPE_NULL;
|
||||
}
|
||||
|
||||
mapentity_t * mapEntityGetByName(const char_t *name) {
|
||||
assertNotNull(name, "Name cannot be NULL");
|
||||
assertStrLenMax(name, MAP_ENTITY_NAME_MAX_LENGTH, "Name is too long");
|
||||
|
||||
// Search for the entity by name.
|
||||
mapentity_t *start = &MAP.entities[0];
|
||||
mapentity_t *end = &MAP.entities[MAP_ENTITY_COUNT_MAX];
|
||||
do {
|
||||
if(start->type == MAP_ENTITY_TYPE_NULL) continue;
|
||||
if(stringCompare(start->name, name) == 0) {
|
||||
return start;
|
||||
}
|
||||
} while(++start < end);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
22
src/overworld/map.h
Normal file
22
src/overworld/map.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "mapentity.h"
|
||||
|
||||
#define MAP_ENTITY_COUNT_MAX 32
|
||||
|
||||
typedef struct {
|
||||
mapentity_t entities[MAP_ENTITY_COUNT_MAX];
|
||||
} map_t;
|
||||
|
||||
extern map_t MAP;
|
||||
|
||||
/**
|
||||
* Initialize the overworld map.
|
||||
*/
|
||||
void mapInit(void);
|
||||
23
src/overworld/mapentity.c
Normal file
23
src/overworld/mapentity.c
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "mapentity.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/string.h"
|
||||
|
||||
void mapEntityInit(mapentity_t *ent, const mapentitytype_t type) {
|
||||
memoryZero(ent, sizeof(mapentity_t));
|
||||
ent->type = type;
|
||||
}
|
||||
|
||||
void mapEntitySetName(mapentity_t *ent, const char_t *name) {
|
||||
assertNotNull(ent, "Ent cannot be NULL");
|
||||
assertNotNull(name, "Name cannot be NULL");
|
||||
assertStrLenMax(name, MAP_ENTITY_NAME_MAX_LENGTH, "Name is too long");
|
||||
|
||||
stringCopy(ent->name, name, MAP_ENTITY_NAME_MAX_LENGTH);
|
||||
}
|
||||
57
src/overworld/mapentity.h
Normal file
57
src/overworld/mapentity.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
#define MAP_ENTITY_NAME_MAX_LENGTH 16
|
||||
#define MAP_ENTITY_SCRIPT_MAX_LENGTH 64
|
||||
|
||||
typedef enum {
|
||||
MAP_ENTITY_DIR_NORTH = 0,
|
||||
MAP_ENTITY_DIR_EAST = 1,
|
||||
MAP_ENTITY_DIR_SOUTH = 2,
|
||||
MAP_ENTITY_DIR_WEST = 3,
|
||||
|
||||
MAP_ENTITY_DIR_COUNT = 4,
|
||||
|
||||
MAP_ENTITY_DIR_UP = MAP_ENTITY_DIR_NORTH,
|
||||
MAP_ENTITY_DIR_RIGHT = MAP_ENTITY_DIR_EAST,
|
||||
MAP_ENTITY_DIR_DOWN = MAP_ENTITY_DIR_SOUTH,
|
||||
MAP_ENTITY_DIR_LEFT = MAP_ENTITY_DIR_WEST
|
||||
} mapentitydir_t;
|
||||
|
||||
typedef enum {
|
||||
MAP_ENTITY_TYPE_NULL,
|
||||
|
||||
MAP_ENTITY_TYPE_NPC,
|
||||
|
||||
MAP_ENTITY_TYPE_COUNT
|
||||
} mapentitytype_t;
|
||||
|
||||
typedef struct {
|
||||
char_t name[MAP_ENTITY_NAME_MAX_LENGTH];
|
||||
|
||||
// Position within the map
|
||||
uint16_t x, y;
|
||||
uint8_t layer;
|
||||
|
||||
// Rendering properties
|
||||
mapentitydir_t dir;
|
||||
mapentitytype_t type;
|
||||
|
||||
// Script to execute on interaction
|
||||
char_t script[MAP_ENTITY_SCRIPT_MAX_LENGTH];
|
||||
} mapentity_t;
|
||||
|
||||
/**
|
||||
* Initialize a map entity to default values.
|
||||
*
|
||||
* @param ent The entity to initialize.
|
||||
* @param type The type of the entity.
|
||||
*/
|
||||
void mapEntityInit(mapentity_t *ent, const mapentitytype_t type);
|
||||
Reference in New Issue
Block a user