Dawn/src/dawn/rpg/entity/entitydirection.c
2024-10-06 21:01:05 -05:00

70 lines
1.4 KiB
C

/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "entitydirection.h"
#include "assert/assert.h"
void entityDirectionOffsetGet(
const entitydirection_t dir,
uint16_t *x,
uint16_t *y
) {
switch(dir) {
case ENTITY_DIRECTION_SOUTH:
*x = 0;
*y = 1;
break;
case ENTITY_DIRECTION_NORTH:
*x = 0;
*y = -1;
break;
case ENTITY_DIRECTION_WEST:
*x = -1;
*y = 0;
break;
case ENTITY_DIRECTION_EAST:
*x = 1;
*y = 0;
break;
default:
assertUnreachable("Invalid entity direction.");
}
}
void entityDirectionOffsetAdd(
const entitydirection_t dir,
uint16_t *x,
uint16_t *y
) {
switch(dir) {
case ENTITY_DIRECTION_SOUTH:
*y += 1;
break;
case ENTITY_DIRECTION_NORTH:
*y -= 1;
break;
case ENTITY_DIRECTION_WEST:
*x -= 1;
break;
case ENTITY_DIRECTION_EAST:
*x += 1;
break;
default:
assertUnreachable("Invalid entity direction.");
}
}
entitydirection_t entityDirectionLookAt(
const uint16_t srcX, const uint16_t srcY,
const uint16_t trgX, const uint16_t trgY
) {
if(srcX < trgX) return ENTITY_DIRECTION_EAST;
if(srcX > trgX) return ENTITY_DIRECTION_WEST;
if(srcY < trgY) return ENTITY_DIRECTION_SOUTH;
if(srcY > trgY) return ENTITY_DIRECTION_NORTH;
return ENTITY_DIRECTION_SOUTH;
}