Files
dusk/archive/rpg/rpgcamera.c
Dominic Masters af5bf987c8
All checks were successful
Build Dusk / run-tests (push) Successful in 2m12s
Build Dusk / build-linux (push) Successful in 1m49s
Build Dusk / build-psp (push) Successful in 1m52s
Add inventory.
2026-01-06 07:47:16 -06:00

52 lines
1.3 KiB
C

/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rpgcamera.h"
#include "util/memory.h"
#include "rpg/entity/entity.h"
#include "rpg/overworld/map.h"
#include "assert/assert.h"
rpgcamera_t RPG_CAMERA;
void rpgCameraInit(void) {
memoryZero(&RPG_CAMERA, sizeof(rpgcamera_t));
}
errorret_t rpgCameraUpdate(void) {
if(!mapIsLoaded()) errorOk();
chunkpos_t chunkPos;
switch(RPG_CAMERA.mode) {
case RPG_CAMERA_MODE_FREE:
worldPosToChunkPos(&RPG_CAMERA.free, &chunkPos);
break;
case RPG_CAMERA_MODE_FOLLOW_ENTITY: {
entity_t *entity = &ENTITIES[RPG_CAMERA.followEntity.followEntityId];
if(entity->type == ENTITY_TYPE_NULL) {
errorOk();
}
// Update map position to match camera. By default map wants to know the
// top left but we want to set the center, so we need to sub half map size
worldPosToChunkPos(&entity->position, &chunkPos);
break;
}
default:
assertUnreachable("Invalid RPG camera mode");
}
errorChain(mapPositionSet((chunkpos_t){
.x = chunkPos.x - (MAP_CHUNK_WIDTH / 2),
.y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2),
.z = chunkPos.z - (MAP_CHUNK_DEPTH / 2)
}));
errorOk();
}