diff options
| author | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-05-09 18:38:21 -0700 |
|---|---|---|
| committer | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-05-09 18:38:21 -0700 |
| commit | eb9466c8d3d18d65eb4bab2f6770f55cba9d8de3 (patch) | |
| tree | bd2c49b56d9a4bdc30e3aec5ad0b3139b398c566 | |
| parent | 78fd742b1e276aec3eb1e939542ef51c3231b060 (diff) | |
| download | dyl8-eb9466c8d3d18d65eb4bab2f6770f55cba9d8de3.tar.gz dyl8-eb9466c8d3d18d65eb4bab2f6770f55cba9d8de3.zip | |
Adding a simple grid-based camera
| -rw-r--r-- | camera.lua | 38 | ||||
| -rw-r--r-- | dyl.lua | 11 | ||||
| -rw-r--r-- | dyl.p8 | 22 | ||||
| -rw-r--r-- | entity.lua | 6 | ||||
| -rw-r--r-- | math.lua | 10 | ||||
| -rw-r--r-- | ui.lua | 0 | ||||
| -rw-r--r-- | util.lua | 6 | ||||
| -rw-r--r-- | world.lua | 6 |
8 files changed, 77 insertions, 22 deletions
diff --git a/camera.lua b/camera.lua new file mode 100644 index 0000000..09e528b --- /dev/null +++ b/camera.lua @@ -0,0 +1,38 @@ +SCREEN = vec2(128, 128) + +_chunk_size = vec2(102, 102) +_chunk_size:apply(function (v) assert(v % 2 == 0) end) +_padding = (SCREEN - _chunk_size) / 2 +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) + + assert(self.tracking.position) + tracking_chunk = vec2(self.tracking.position / _chunk_size):apply(flr) + if (tracking_chunk == self.current_chunk) then return end + + self.position = (tracking_chunk * _chunk_size) - _padding + self.current_chunk = tracking_chunk +end + +function Camera:render(entity) + screen_position = (entity.sprite_position or entity.position) - self.position + entity:render(screen_position) +end + +function Camera:new(tracking) + cam = setmetatable({ + -- position = vec2(0, 0), + -- current_chunk = vec2(0, 0), + tracking = tracking + }, Camera) + + AddUpdateHook(function (dt) cam:update() end) + return cam +end
\ No newline at end of file @@ -231,6 +231,8 @@ end function _init() end +_camera = Camera:new(_player) + _step_t = time() function _update60() -- clear here in case we want to print to screen outside of _draw @@ -251,8 +253,9 @@ function _update60() end function _draw() - World.sort(function(a, b) - return b.render_order < a.render_order - end) - World.foreach(function(e) e:render() end) + -- TODO: FIX THIS + -- World.sort(function(a, b) + -- return b.render_order < a.render_order + -- end) + World.foreach(function(e) _camera:render(e) end) end
\ No newline at end of file @@ -6,6 +6,7 @@ __lua__ #include entity.lua #include world.lua #include collisions.lua +#include camera.lua #include dyl.lua ------------------------ -- don't you leave -- @@ -109,14 +110,21 @@ __gfx__ 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -77055505000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -00000000004444005555505000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -05550555044004405000055000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -00000000040000400505050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -55077705040550400550050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -0000000004000a400505550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -05550555040000400550050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +77055505000000000000000000009000000000000000900000900000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000004444005555505000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000000000000000 +05550555044004405000055000090900000900000009900000090000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000400004005050500009a9900000a90000009a900009a9000000000000000000000000000000000000000000000000000000000000000000000000000 +550777050405504005500500009aaa00009aa900009aa900009a9000000000000000000000000000000000000000000000000000000000000000000000000000 +0000000004000a400505550000444400004444000044440000444400000000000000000000000000000000000000000000000000000000000000000000000000 +05550555040000400550050004400440044004400440044004400440000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000a0000000000000009000000000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000900000090000000a0000000a0000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000444400004444000044440000444400000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000004400440044004400440044004400440000000000000000000000000000000000000000000000000000000000000000000000000 __sfx__ 000100002605026050240502305022050210502105021050220500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00090000000502b3502b3502a35029350293503235032350313503134031330209303032030320303303034031340313403334034340373400f050340502f0502c05000000000000000000000000000000000000 @@ -172,9 +172,7 @@ function _get_animation_key(line_of_sight) return _animation_keys[n_line_of_sight.x + 2] .. "_x" end -function Entity:render() - pos = self.sprite_position or self.position - +function Entity:render(screen_position) animation = self.states[self.state].animation if (animation == nil) then return end if (self.line_of_sight ~= nil) then @@ -190,7 +188,7 @@ function Entity:render() reflection = animation.reflect or vec2(false, false) spr( frame, - self.sprite_position.x, self.sprite_position.y, + screen_position.x, screen_position.y, 1, 1, reflection.x, reflection.y ) @@ -1,13 +1,17 @@ Vec2 = {} +Vec2.__eq = function(a, b) return a.x == b.x and a.y == b.y end Vec2.__index = Vec2 Vec2.__add = function(a, b) return vec2(a.x + b.x, a.y + b.y) end Vec2.__unm = function(a) return vec2(-a.x, -a.y) end Vec2.__sub = function(a, b) return a + (-b) end Vec2.__mul = function(a, b) if (type(b) == "number") then return vec2(a.x * b, a.y * b) end -- scalar mult - return a.x * b.x + a.y * b.y + return vec2(a.x * b.x, a.y * b.y) +end +Vec2.__div = function(a, b) + if (type(b) == "number") then return vec2(a.x / b, a.y / b) end -- scalar mult + return vec2(a.x / b.x, a.y / b.y) end -Vec2.__div = function(a, b) return vec2(a.x / b.x, a.y / b.y) end Vec2.__tostring = function(a) return "(" .. fmt(a.x) .. "," .. fmt(a.y) .. ")" end function Vec2:magnitude() @@ -17,7 +21,7 @@ end function Vec2:normal() local m = self:magnitude() if m == 0 then return vec2(self.x, self.y) end - return vec2(self.x / m, self.y / m) + return self / m end function Vec2:apply(f) @@ -1,5 +1,9 @@ update_hooks = {} +function AddUpdateHook(f) + update_hooks[#update_hooks + 1] = f +end + -- https://pico-8.fandom.com/wiki/Qsort function qsort(a, c, l, r) c, l, r = c or function(a, b) return a < b end, l or 1, r or #a @@ -99,7 +103,7 @@ function _update_button_states(_dt) end end end -update_hooks[1] = _update_button_states +AddUpdateHook(_update_button_states) function button_just_pressed(id) return _button_states[id] == "just_pressed" end
\ No newline at end of file @@ -8,9 +8,9 @@ function _next_id() return i end -function World.sort(cmp) - -- qsort(_World, cmp) -end +-- function World.sort(cmp) +-- qsort(_World, cmp) +-- end function World.get(id) return _World[id] |
