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 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'camera.lua') 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 -- cgit v1.3