Added sign

This commit is contained in:
2024-10-06 22:23:26 -05:00
parent b5d7b7e229
commit bf3912bb7f
15 changed files with 180 additions and 112 deletions

View File

@ -11,4 +11,6 @@ target_sources(${DAWN_TARGET_NAME}
entity.c
entitydirection.c
player.c
sign.c
interact.c
)

View File

@ -32,12 +32,6 @@ void entityInit(
case ENTITY_TYPE_PLAYER:
playerInit(entity);
break;
case ENTITY_TYPE_NPC:
break;
default:
assertUnreachable("Unknown entity type.");
}
}
@ -54,12 +48,6 @@ void entityUpdate(entity_t *entity) {
case ENTITY_TYPE_PLAYER:
playerUpdate(entity);
break;
case ENTITY_TYPE_NPC:
break;
default:
assertUnreachable("Unknown entity type.");
}
break;
@ -78,7 +66,7 @@ void entityUpdate(entity_t *entity) {
}
}
void entityWalk(entity_t *entity, const uint8_t dir) {
void entityWalk(entity_t *entity, const entitydirection_t dir) {
assertNotNull(entity, "Entity cannot be NULL.");
uint16_t newX = entity->x, newY = entity->y;

View File

@ -7,18 +7,23 @@
#pragma once
#include "player.h"
#include "sign.h"
#include "entitydirection.h"
typedef struct _map_t map_t;
typedef enum {
ENTITY_TYPE_NULL = 0,
ENTITY_TYPE_PLAYER = 1,
ENTITY_TYPE_NPC = 2
ENTITY_TYPE_NPC = 2,
ENTITY_TYPE_SIGN = 3
} entitytype_t;
#define ENTITY_STATE_IDLE 0
#define ENTITY_STATE_WALKING 1
#define ENTITY_STATE_TALKING 2
typedef struct _map_t map_t;
typedef enum {
ENTITY_STATE_IDLE = 0,
ENTITY_STATE_WALKING = 1,
ENTITY_STATE_TALKING = 2
} entitystate_t;
typedef struct {
float_t time;
@ -30,17 +35,18 @@ typedef struct _entity_t {
uint16_t x;
uint16_t y;
uint8_t layer;
uint8_t direction;
uint8_t state;
entitydirection_t direction;
entitystate_t state;
// State
union {
entitywalkstate_t walk;
};
// Type
// Type data
union {
player_t player;
sign_t sign;
};
} entity_t;
@ -69,7 +75,7 @@ void entityUpdate(entity_t *entity);
* @param entity Entity to move.
* @param dir Direction to move.
*/
void entityWalk(entity_t *entity, const uint8_t dir);
void entityWalk(entity_t *entity, const entitydirection_t dir);
/**
* Sets the position of an entity. Performs an immediate move.

View File

@ -0,0 +1,44 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "interact.h"
#include "assert/assert.h"
void entityInteractEntity(
entity_t *source,
entity_t *target
) {
assertNotNull(source, "entityInteractEntity: Source is NULL!");
assertNotNull(target, "entityInteractEntity: Target is NULL!");
switch(target->type) {
case ENTITY_TYPE_SIGN:
source->state = ENTITY_STATE_TALKING;
target->state = ENTITY_STATE_TALKING;
textboxSetText(NULL, target->sign.text);
return;
default:
return;
}
}
void entityInteractTile(
entity_t *source,
tile_t tile
) {
assertNotNull(source, "entityInteractTile: Source is NULL!");
switch(tile) {
case TILE_ID_WATER:
textboxSetText(NULL, "You cannot swim.");
source->state = ENTITY_STATE_TALKING;
return;
default:
break;
}
}

View File

@ -0,0 +1,32 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "entity.h"
#include "rpg/world/tile.h"
/**
* Interacts with an entity.
*
* @param source Entity that is interacting.
* @param target Entity that is being interacted with.
*/
void entityInteractEntity(
entity_t *source,
entity_t *target
);
/**
* Interacts with a tile.
*
* @param source Entity that is interacting.
* @param tile Tile that is being interacted with.
*/
void entityInteractTile(
entity_t *source,
tile_t tile
);

View File

@ -13,6 +13,7 @@
#include "assert/assert.h"
#include "ui/textbox.h"
#include "game/game.h"
#include "interact.h"
void playerInit(entity_t *entity) {
assertTrue(entity->type == ENTITY_TYPE_PLAYER, "Entity is not a player.");
@ -37,27 +38,9 @@ void playerUpdate(entity_t *entity) {
entityDirectionOffsetAdd(entity->direction, &x, &y);
entity_t *target = mapEntityGetByPosition(entity->map, x, y);
if(target) {
// Interact with the target
target->direction = entityDirectionLookAt(
target->x, target->y,
entity->x, entity->y
);
if(target) return entityInteractEntity(entity, target);
target->state = ENTITY_STATE_TALKING;
entity->state = ENTITY_STATE_TALKING;
textboxSetText("NPC", "Hello Player.\nHow are you today?");
return;
}
tile_t tile = mapTileGetByPosition(entity->map, x, y, 0);
switch(tile) {
case TILE_ID_WATER:
textboxSetText(NULL, "You cannot swim.");
entity->state = ENTITY_STATE_TALKING;
return;
default:
break;
}
tile_t tile = mapTileGetByPosition(entity->map, x, y, entity->layer);
entityInteractTile(entity, tile);
}
}

View File

@ -0,0 +1,15 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "sign.h"
#include "assert/assert.h"
void signTextSet(sign_t *sign, const char_t *text) {
assertNotNull(sign, "signTextSet: Sign is NULL!");
assertNotNull(text, "signTextSet: Text is NULL!");
strncpy(sign->text, text, TEXTBOX_TEXT_MAX);
}

View File

@ -0,0 +1,21 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "ui/textbox.h"
typedef struct {
char_t text[TEXTBOX_TEXT_MAX + 1];
} sign_t;
/**
* Sets the text of a sign.
*
* @param sign Sign to set text for.
* @param text Text to set.
*/
void signTextSet(sign_t *sign, const char_t *text);

View File

@ -1,53 +0,0 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "rpg/world/map.h"
#include "assert/assert.h"
#define TEST_MAP_WIDTH MAP_WIDTH_MAX
#define TEST_MAP_HEIGHT MAP_HEIGHT_MAX
#define TEST_MAP_LAYERS 1
void testMapInit(map_t *map) {
mapInit(map, TEST_MAP_WIDTH, TEST_MAP_HEIGHT, TEST_MAP_LAYERS);
entity_t *player = mapEntityAdd(map);
entityInit(player, ENTITY_TYPE_PLAYER, map);
entityPositionSet(player, 5, 5);
entity_t *npc = mapEntityAdd(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] = TILE_ID_GRASS;
}
tiles[10] = TILE_ID_WATER;
tiles[11] = TILE_ID_WATER;
tiles[12] = TILE_ID_WATER;
tiles[13] = TILE_ID_WATER;
tiles[14] = TILE_ID_WATER;
tiles[26] = TILE_ID_WATER;
tiles[27] = TILE_ID_WATER;
tiles[28] = TILE_ID_WATER;
tiles[29] = TILE_ID_WATER;
tiles[41] = TILE_ID_WATER;
tiles[42] = TILE_ID_WATER;
tiles[43] = TILE_ID_WATER;
tiles[44] = TILE_ID_WATER;
tiles[45] = TILE_ID_WATER;
mapTilesSet(map, tiles, 0);
}