From e51b2ab9998588d2c4c842d861806c81011d751b Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Sat, 9 May 2026 20:18:45 -0700 Subject: Interpolates camera movement --- camera.lua | 40 +++++++++++++++++++++++++++++----------- dyl.lua | 11 +++++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/camera.lua b/camera.lua index 09e528b..22be0fc 100644 --- a/camera.lua +++ b/camera.lua @@ -1,24 +1,42 @@ SCREEN = vec2(128, 128) -_chunk_size = vec2(102, 102) +_chunk_size = vec2(100, 100) _chunk_size:apply(function (v) assert(v % 2 == 0) end) _padding = (SCREEN - _chunk_size) / 2 +_interpolate_step = 20 assert(_chunk_size.x < SCREEN.x and _chunk_size.y < SCREEN.y) Camera = {} Camera.__index = Camera function Camera:update() - line(_padding.x, SCREEN.y - _padding.y, _padding.x, _padding.y, 5) - line(_chunk_size.x + _padding.x, SCREEN.y - _padding.y, _chunk_size.x + _padding.x, _padding.y, 5) - line(_padding.x, _padding.y, SCREEN.x - _padding.x, _padding.y, 5) - line(_padding.x, _chunk_size.y + _padding.y, SCREEN.x - _padding.x, _chunk_size.y + _padding.y, 5) + if self.transition_to ~= self.position then + delta = (self.transition_to - self.position) + snap_x = delta.x == 0 or abs(delta.x) < _interpolate_step + snap_y = delta.y == 0 or abs(delta.y) < _interpolate_step + if snap_x then + self.position.x = self.transition_to.x + else + self.position.x += normalize_scalar(delta.x) * _interpolate_step + end + if snap_y then + self.position.y = self.transition_to.y + else + self.position.y += normalize_scalar(delta.y) * _interpolate_step + end + return + end assert(self.tracking.position) - tracking_chunk = vec2(self.tracking.position / _chunk_size):apply(flr) - if (tracking_chunk == self.current_chunk) then return end + tracking_chunk = chunk_from(self.tracking.position) + self.transition_to = (tracking_chunk * _chunk_size) - _padding +end + +function chunk_from(position) + return vec2(position / _chunk_size):apply(flr) +end - self.position = (tracking_chunk * _chunk_size) - _padding - self.current_chunk = tracking_chunk +function from_chunk(chunk) + return chunk * _chunk_size end function Camera:render(entity) @@ -28,10 +46,10 @@ end function Camera:new(tracking) cam = setmetatable({ - -- position = vec2(0, 0), - -- current_chunk = vec2(0, 0), + transition_to = from_chunk(chunk_from(tracking.position)), tracking = tracking }, Camera) + cam.position = cam.transition_to AddUpdateHook(function (dt) cam:update() end) return cam diff --git a/dyl.lua b/dyl.lua index fa0190d..a1174a9 100644 --- a/dyl.lua +++ b/dyl.lua @@ -234,6 +234,14 @@ end _camera = Camera:new(_player) _step_t = time() +_continue = true +function pause() + _continue = false +end +function resume() + _continue = true + _step_t = time() +end function _update60() -- clear here in case we want to print to screen outside of _draw cls(0) @@ -245,6 +253,8 @@ function _update60() foreach(update_hooks, function(f) f(step_dt) end) handle_input() + + if not _continue then return end World.foreach(function(e) e:update(step_dt) end) run_collisions() @@ -252,6 +262,7 @@ function _update60() World.cull_the_dead() end + function _draw() -- TODO: FIX THIS -- World.sort(function(a, b) -- cgit v1.3