Render test
This commit is contained in:
@@ -1,101 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "entity.h"
|
||||
|
||||
entity_t ENTITIES[ENTITY_COUNT];
|
||||
|
||||
entitycallbacks_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = {
|
||||
[ENTITY_TYPE_NULL] = { 0 },
|
||||
|
||||
[ENTITY_TYPE_PLAYER] = {
|
||||
.init = playerInit,
|
||||
.update = playerUpdate
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
|
||||
void entityInit(entity_t *entity, const entitytype_t type) {
|
||||
assertNotNull(entity, "Entity is NULL");
|
||||
assertTrue(type < ENTITY_TYPE_COUNT, "Invalid entity type");
|
||||
assertNotNull(ENTITY_CALLBACKS[type].init, "Entity type has no init");
|
||||
|
||||
memoryZero(entity, sizeof(entity_t));
|
||||
entity->type = type;
|
||||
ENTITY_CALLBACKS[type].init(entity);
|
||||
}
|
||||
|
||||
void entityUpdate(entity_t *entity) {
|
||||
assertNotNull(entity, "Entity cannot be NULL");
|
||||
assertNotNull(
|
||||
ENTITY_CALLBACKS[entity->type].update,
|
||||
"Entity type has no update"
|
||||
);
|
||||
|
||||
// Handle subpixel movement
|
||||
if(entity->subX < 0) {
|
||||
entity->subX++;
|
||||
} else if(entity->subY < 0) {
|
||||
entity->subY++;
|
||||
} else if(entity->subX > 0) {
|
||||
entity->subX--;
|
||||
} else if(entity->subY > 0) {
|
||||
entity->subY--;
|
||||
}
|
||||
|
||||
// Entity-Type handling
|
||||
ENTITY_CALLBACKS[entity->type].update(entity);
|
||||
}
|
||||
|
||||
void entityTurn(entity_t *entity, const entitydir_t dir) {
|
||||
assertNotNull(entity, "Entity cannot be NULL");
|
||||
assertFalse(entityIsWalking(entity), "Entity is currently walking");
|
||||
entity->dir = dir;
|
||||
}
|
||||
|
||||
void entityWalk(entity_t *entity) {
|
||||
assertNotNull(entity, "Entity cannot be NULL");
|
||||
assertFalse(entityIsWalking(entity), "Entity is already walking");
|
||||
|
||||
switch(entity->dir) {
|
||||
case ENTITY_DIR_UP:
|
||||
entity->y--;
|
||||
entity->subY = ENTITY_MOVE_SUBPIXEL;
|
||||
break;
|
||||
case ENTITY_DIR_DOWN:
|
||||
entity->y++;
|
||||
entity->subY = -ENTITY_MOVE_SUBPIXEL;
|
||||
break;
|
||||
case ENTITY_DIR_LEFT:
|
||||
entity->x--;
|
||||
entity->subX = ENTITY_MOVE_SUBPIXEL;
|
||||
break;
|
||||
case ENTITY_DIR_RIGHT:
|
||||
entity->x++;
|
||||
entity->subX = -ENTITY_MOVE_SUBPIXEL;
|
||||
break;
|
||||
default:
|
||||
assertUnreachable("Invalid entity direction");
|
||||
}
|
||||
}
|
||||
|
||||
bool_t entityIsWalking(const entity_t *entity) {
|
||||
assertNotNull(entity, "Entity cannot be NULL");
|
||||
return (entity->subX != 0 || entity->subY != 0);
|
||||
}
|
||||
|
||||
entity_t * entityGetAt(const uint8_t x, const uint8_t y) {
|
||||
entity_t *e = ENTITIES;
|
||||
do {
|
||||
if(e->type && e->x == x && e->y == y) return e;
|
||||
e++;
|
||||
} while(e < (ENTITIES + ENTITY_COUNT));
|
||||
return NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user