diff options
| author | the lemons <citrons@mondecitronne.com> | 2022-12-21 00:13:49 -0600 |
|---|---|---|
| committer | the lemons <citrons@mondecitronne.com> | 2022-12-21 00:13:49 -0600 |
| commit | 619c46c37731df55bd2f50b7bb126c016734796e (patch) | |
| tree | b785914b5b8f82ad16610382afc75603cea85483 /world.lua | |
| parent | 15b1b3127e636a8a63ff9cdb11dbac68f5218ffe (diff) | |
chunk-based rendering
Diffstat (limited to 'world.lua')
| -rw-r--r-- | world.lua | 36 |
1 files changed, 29 insertions, 7 deletions
@@ -1,7 +1,6 @@ local world = {} -local chunk_size = 1024 - +world.chunk_size = 1024 world.chunks = {} setmetatable(world.chunks, { __index = function(_, bee) @@ -15,11 +14,11 @@ world.objects = {} world.last_id = 0 function world.chunk_pos(x, y) - return math.floor(x / chunk_size), math.floor(y / chunk_size) + return math.floor(x / world.chunk_size), math.floor(y / world.chunk_size) end function world.world_pos(x, y) - return x * chunk_size, y * chunk_size + return x * world.chunk_size, y * world.chunk_size end function world.chunk(x, y) @@ -27,13 +26,36 @@ function world.chunk(x, y) if not world.chunks[cx][cy] then -- load chunk from disk if the world.chunks[cx][cy] = setmetatable( - {pos = {cx, cy}, objects = {}}, chunk) + {pos = {cx, cy}, objects = {}}, chunk_mt) end return world.chunks[cx][cy] end -function world.get_object(id) +function world.object(id) + return world.objects[id] +end + +function world.all() + return coroutine.wrap(function() + for _, o in pairs(world.objects) do + coroutine.yield(o) + end + end) +end + +function world.iterate(x1, y1, x2, y2) + return coroutine.wrap(function() + for x = x1, x2 + world.chunk_size, world.chunk_size do + for y = y1, y2 + world.chunk_size, world.chunk_size do + for _, o in pairs(world.chunk(x, y).objects) do + local x, y = unpack(o.data.pos) + if x >= x1 and x <= x2 and y >= y1 and y <= y2 then + coroutine.yield(o) + end + end + end + end + end) end -world.__index = world return world |
