diff --git a/code/apps/client/source/camera.c b/code/apps/client/source/camera.c index f4d7dee..c20ae46 100644 --- a/code/apps/client/source/camera.c +++ b/code/apps/client/source/camera.c @@ -3,6 +3,8 @@ #include "entity_view.h" #include "game.h" +#define CAMERA_LERP_FACTOR 0.06 + static camera main_camera; void camera_reset(void) { @@ -13,11 +15,13 @@ void camera_reset(void) { void camera_update(void) { switch (main_camera.mode) { case CAMERA_MODE_FOLLOW: { - entity_view *view = entity_view_get(game_world_view_get_active(), main_camera.ent_id); + world_view *world = game_world_view_get_active(); + if (!world) break; + entity_view *view = entity_view_get(&world->entities, main_camera.ent_id); if (!view) break; - main_camera.x = view->x; - main_camera.y = view->y; + main_camera.x = zpl_lerp(main_camera.x, view->x, CAMERA_LERP_FACTOR); + main_camera.y = zpl_lerp(main_camera.y, view->y, CAMERA_LERP_FACTOR); }break; default: { diff --git a/code/apps/client/source/game.c b/code/apps/client/source/game.c index 74d4e44..c93d70c 100644 --- a/code/apps/client/source/game.c +++ b/code/apps/client/source/game.c @@ -71,6 +71,16 @@ world_view *game_world_view_get_active(void) { return active_viewer; } +void game_world_view_set_active_by_idx(uint16_t idx) { + ZPL_ASSERT(idx >= 0 && idx < zpl_buffer_count(world_viewers)); + game_world_view_set_active(&world_viewers[idx]); +} + +void game_world_view_set_active(world_view *view) { + active_viewer = view; + camera_set_follow(view->owner_id); +} + void flecs_dash_init() { ECS_IMPORT(world_ecs(), FlecsDash); ECS_IMPORT(world_ecs(), FlecsSystemsCivetweb); @@ -131,6 +141,7 @@ void game_update() { } void game_render() { + camera_update(); platform_render(); } diff --git a/code/common/game.h b/code/common/game.h index 6b528f3..9824684 100644 --- a/code/common/game.h +++ b/code/common/game.h @@ -12,4 +12,6 @@ void game_update(); void game_render(); world_view *game_world_view_get_active(void); -world_view *game_world_view_get(uint16_t idx); \ No newline at end of file +world_view *game_world_view_get(uint16_t idx); +void game_world_view_set_active_by_idx(uint16_t idx); +void game_world_view_set_active(world_view *view); diff --git a/code/common/packets/pkt_01_welcome.c b/code/common/packets/pkt_01_welcome.c index 4da27c2..1d0f374 100644 --- a/code/common/packets/pkt_01_welcome.c +++ b/code/common/packets/pkt_01_welcome.c @@ -37,5 +37,6 @@ int32_t pkt_01_welcome_handler(pkt_header *header) { zpl_printf("[INFO] initializing read-only world view ...\n"); world_view_init(view, table.ent_id, table.block_size, table.chunk_size, table.world_size); + game_world_view_set_active(view); return 0; }