52 lines
1.5 KiB
C
52 lines
1.5 KiB
C
/**
|
|
* Copyright (c) 2024 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "player.h"
|
|
#include "entity.h"
|
|
#include "entitydirection.h"
|
|
#include "rpg/world/map.h"
|
|
#include "input.h"
|
|
#include "assert/assert.h"
|
|
#include "ui/textbox.h"
|
|
|
|
void playerInit(entity_t *entity) {
|
|
assertTrue(entity->type == ENTITY_TYPE_PLAYER, "Entity is not a player.");
|
|
}
|
|
|
|
void playerUpdate(entity_t *entity) {
|
|
assertTrue(entity->type == ENTITY_TYPE_PLAYER, "Entity is not a player.");
|
|
|
|
if(inputIsDown(INPUT_BIND_DOWN)) {
|
|
return entityWalk(entity, ENTITY_DIRECTION_SOUTH);
|
|
} else if(inputIsDown(INPUT_BIND_UP)) {
|
|
return entityWalk(entity, ENTITY_DIRECTION_NORTH);
|
|
} else if(inputIsDown(INPUT_BIND_LEFT)) {
|
|
return entityWalk(entity, ENTITY_DIRECTION_WEST);
|
|
} else if(inputIsDown(INPUT_BIND_RIGHT)) {
|
|
return entityWalk(entity, ENTITY_DIRECTION_EAST);
|
|
}
|
|
|
|
if(inputIsDown(INPUT_BIND_ACCEPT)) {
|
|
// Check what the player is trying to interact with
|
|
uint16_t x = entity->x, y = entity->y;
|
|
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
|
|
);
|
|
|
|
target->state = ENTITY_STATE_TALKING;
|
|
entity->state = ENTITY_STATE_TALKING;
|
|
textboxSetText("Hello Player");
|
|
return;
|
|
}
|
|
}
|
|
} |