94 lines
2.5 KiB
C
94 lines
2.5 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "entity.h"
|
|
#include "assert/assert.h"
|
|
#include "input.h"
|
|
#include "display/render.h"
|
|
#include "world/world.h"
|
|
|
|
#include "ui/uitextbox.h"
|
|
|
|
inventory_t PLAYER_INVENTORY;
|
|
|
|
void playerInit() {
|
|
entity_t *ent = &ENTITIES[0];
|
|
|
|
entity_t playerEntityData = {
|
|
.id = PLAYER_ENTITY_ID,
|
|
.type = ENTITY_TYPE_PLAYER,
|
|
.x = WORLD_PLAYER_SPAWN_X,
|
|
.y = WORLD_PLAYER_SPAWN_Y,
|
|
};
|
|
|
|
entityLoad(ent, &playerEntityData);
|
|
inventoryInit(&PLAYER_INVENTORY, INVENTORY_SIZE_MAX);
|
|
}
|
|
|
|
void playerEntityLoad(entity_t *entity, const entity_t *source) {
|
|
assertNotNull(entity, "Entity pointer cannot be NULL");
|
|
assertNotNull(source, "Source entity pointer cannot be NULL");
|
|
assertTrue(entity->type == ENTITY_TYPE_PLAYER, "Entity type must be PLAYER");
|
|
assertTrue(source->type == entity->type, "Source/Entity type mismatch");
|
|
}
|
|
|
|
void playerEntityUpdate(entity_t *entity) {
|
|
assertNotNull(entity, "Entity pointer cannot be NULL");
|
|
assertTrue(entity->type == ENTITY_TYPE_PLAYER, "Entity type must be PLAYER");
|
|
|
|
// TODO: make this just a method somewhere.
|
|
if(UI_TEXTBOX.visible) return;
|
|
if(entityIsMoving(entity)) return;
|
|
|
|
const uint8_t moveSpeed = inputIsDown(INPUT_BIND_CANCEL) ? PLAYER_SPEED_RUN : PLAYER_SPEED_WALK;
|
|
|
|
if(inputIsDown(INPUT_BIND_UP)) {
|
|
if(entity->dir != DIRECTION_NORTH) {
|
|
entityTurn(entity, DIRECTION_NORTH);
|
|
return;
|
|
}
|
|
entityMove(entity, moveSpeed);
|
|
return;
|
|
|
|
} else if(inputIsDown(INPUT_BIND_DOWN)) {
|
|
if(entity->dir != DIRECTION_SOUTH) {
|
|
entityTurn(entity, DIRECTION_SOUTH);
|
|
return;
|
|
}
|
|
|
|
entityMove(entity, moveSpeed);
|
|
return;
|
|
} else if(inputIsDown(INPUT_BIND_LEFT)) {
|
|
if(entity->dir != DIRECTION_WEST) {
|
|
entityTurn(entity, DIRECTION_WEST);
|
|
return;
|
|
}
|
|
entityMove(entity, moveSpeed);
|
|
return;
|
|
|
|
} else if(inputIsDown(INPUT_BIND_RIGHT)) {
|
|
if(entity->dir != DIRECTION_EAST) {
|
|
entityTurn(entity, DIRECTION_EAST);
|
|
return;
|
|
}
|
|
|
|
entityMove(entity, moveSpeed);
|
|
return;
|
|
}
|
|
|
|
// Interact
|
|
if(inputPressed(INPUT_BIND_ACTION)) {
|
|
int8_t x, y;
|
|
directionGetCoordinates(entity->dir, &x, &y);
|
|
entity_t *ent = entityGetAt(entity->x + x, entity->y + y);
|
|
|
|
if(ent != NULL && ENTITY_CALLBACKS[ent->type].interact != NULL) {
|
|
assertTrue(ent->type < ENTITY_TYPE_COUNT, "Entity type out of bounds");
|
|
ENTITY_CALLBACKS[ent->type].interact(entity, ent);
|
|
}
|
|
}
|
|
} |