camera follow player + lerp

isolation_bkp/dynres
Dominik Madarász 2021-05-07 13:47:45 +02:00
parent 51c31d9777
commit 2ddf1e48fd
4 changed files with 22 additions and 4 deletions

View File

@ -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: {

View File

@ -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();
}

View File

@ -13,3 +13,5 @@ void game_render();
world_view *game_world_view_get_active(void);
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);

View File

@ -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;
}