Adding triggers, prepping for map loading.

This commit is contained in:
2024-10-06 18:03:46 -05:00
parent e14445680f
commit c69d0ec1cc
8 changed files with 183 additions and 2 deletions

27
assets/testmap.json Normal file
View File

@ -0,0 +1,27 @@
{
"width": 10,
"height": 10,
"layers": [
{
"tiles": [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1
]
}
],
"entities": [
{
"type": 1,
"x": 0,
"y": 0
}
]
}

View File

@ -115,6 +115,14 @@ void entityWalk(entity_t *entity, const uint8_t dir) {
entity->state = ENTITY_STATE_WALKING;
entity->walk.time = 0.1f;
// Check for triggers
trigger_t *trigger = mapTriggerGetByPosition(
entity->map,
entity->x,
entity->y
);
if(trigger != NULL) triggerOnStep(trigger, entity);
}
void entityPositionSet(entity_t *entity, const uint16_t x, const uint16_t y) {

View File

@ -10,4 +10,5 @@ target_sources(${DAWN_TARGET_NAME}
PRIVATE
tile.c
map.c
trigger.c
)

View File

@ -36,7 +36,7 @@ void mapInit(
"Map layers must be greater than 0."
);
assertTrue(
layers < MAP_LAYERS_MAX,
layers <= MAP_LAYERS_MAX,
"Map layers must be less than or equal to MAP_LAYERS_MAX."
);
@ -137,4 +137,33 @@ void mapTilesSet(
tiles,
map->width * map->height * sizeof(tile_t)
);
}
trigger_t * mapTriggerAdd(map_t *map) {
assertTrue(
map->triggerCount < MAP_TRIGGERS_MAX,
"Cannot add any more triggers to map."
);
trigger_t *trigger = &map->triggers[map->triggerCount];
map->triggerCount++;
return trigger;
}
trigger_t * mapTriggerGetByPosition(
map_t *map,
const uint16_t x,
const uint16_t y
) {
for(uint8_t i = 0; i < map->triggerCount; i++) {
trigger_t *trigger = &map->triggers[i];
if(
x >= trigger->x &&
x < trigger->x + trigger->width &&
y >= trigger->y &&
y < trigger->y + trigger->height
) return trigger;
}
return NULL;
}

View File

@ -8,11 +8,13 @@
#pragma once
#include "tile.h"
#include "rpg/entity/entity.h"
#include "rpg/world/trigger.h"
#define MAP_WIDTH_MAX 2048
#define MAP_HEIGHT_MAX 2048
#define MAP_LAYERS_MAX 4
#define MAP_LAYERS_MAX 1
#define MAP_ENTITIES_MAX 256
#define MAP_TRIGGERS_MAX 64
typedef struct _map_t {
tile_t tiles[MAP_WIDTH_MAX * MAP_HEIGHT_MAX * MAP_LAYERS_MAX];
@ -22,6 +24,9 @@ typedef struct _map_t {
entity_t entities[MAP_ENTITIES_MAX];
uint8_t entityCount;
trigger_t triggers[MAP_TRIGGERS_MAX];
uint8_t triggerCount;
} map_t;
/**
@ -107,4 +112,26 @@ void mapTilesSet(
map_t *map,
const tile_t tiles[],
const uint8_t layer
);
/**
* Adds a trigger to the map.
*
* @param map Map to add the trigger to.
* @return Trigger added to the map.
*/
trigger_t * mapTriggerAdd(map_t *map);
/**
* Gets the trigger at the specified position.
*
* @param map Map to get the trigger from.
* @param x X position of the trigger.
* @param y Y position of the trigger.
* @return Trigger at the specified position, or NULL if no trigger is there.
*/
trigger_t * mapTriggerGetByPosition(
map_t *map,
const uint16_t x,
const uint16_t y
);

View File

@ -24,6 +24,9 @@ void testMapInit(map_t *map) {
entityInit(npc, ENTITY_TYPE_NPC, map);
entityPositionSet(npc, 10, 10);
trigger_t *trigger = mapTriggerAdd(map);
triggerInit(trigger, TRIGGER_TYPE_NULL, 15, 15, 1, 1);
tile_t tiles[TEST_MAP_WIDTH * TEST_MAP_HEIGHT];
for(uint32_t i = 0; i < TEST_MAP_WIDTH * TEST_MAP_HEIGHT; i++) {
tiles[i].id = TILE_ID_GRASS;

View File

@ -0,0 +1,42 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "trigger.h"
#include "assert/assert.h"
void triggerInit(
trigger_t *trigger,
const uint8_t type,
const uint16_t x,
const uint16_t y,
const uint16_t width,
const uint16_t height
) {
assertNotNull(trigger, "Trigger cannot be NULL.");
assertTrue(
width > 0,
"Trigger width must be greater than 0."
);
assertTrue(
height > 0,
"Trigger height must be greater than 0."
);
memset(trigger, 0, sizeof(trigger_t));
trigger->type = type;
trigger->x = x;
trigger->y = y;
trigger->width = width;
trigger->height = height;
}
void triggerOnStep(trigger_t *trigger, entity_t *entity) {
assertNotNull(trigger, "Trigger cannot be NULL.");
assertNotNull(entity, "Entity cannot be NULL.");
printf("Trigger\n");
}

View File

@ -0,0 +1,44 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/entity/entity.h"
#define TRIGGER_TYPE_NULL 0
typedef struct {
uint16_t x, y;
uint16_t width, height;
uint8_t type;
} trigger_t;
/**
* Initializes a trigger.
*
* @param trigger Trigger to initialize.
* @param type Type of trigger.
* @param x X position of the trigger.
* @param y Y position of the trigger.
* @param width Width of the trigger.
* @param height Height of the trigger.
*/
void triggerInit(
trigger_t *trigger,
const uint8_t type,
const uint16_t x,
const uint16_t y,
const uint16_t width,
const uint16_t height
);
/**
* Called by an entity that has stepped on the trigger.
*
* @param trigger Trigger that was stepped on.
* @param entity Entity that stepped on the trigger.
*/
void triggerOnStep(trigger_t *trigger, entity_t *entity);