diff options
| author | the lemons <citrons@mondecitronne.com> | 2022-12-19 18:28:46 -0600 |
|---|---|---|
| committer | the lemons <citrons@mondecitronne.com> | 2022-12-19 18:28:46 -0600 |
| commit | 94f7fc0d3e410a7c7dc6f6cffe0ed87238608391 (patch) | |
| tree | 805c7a90ed1adf9bf358868f19ef1e9a072b791d /world.lua | |
| parent | ba1b317b3b4de92d7f39dceb988a41840e4fc942 (diff) | |
world made of chunks with objects in them
Diffstat (limited to 'world.lua')
| -rw-r--r-- | world.lua | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/world.lua b/world.lua new file mode 100644 index 0000000..f4fcb70 --- /dev/null +++ b/world.lua @@ -0,0 +1,39 @@ +local world = {} + +local chunk_size = 1024 + +world.chunks = {} +setmetatable(world.chunks, { + __index = function(_, bee) + if rawget(world.chunks, bee) == nil then + world.chunks[bee] = {} + end + return world.chunks[bee] + end, +}) +world.objects = {} +world.last_id = 0 + +function world.chunk_pos(x, y) + return math.floor(x / chunk_size), math.floor(y / chunk_size) +end + +function world.world_pos(x, y) + return x * chunk_size, y * chunk_size +end + +function world.chunk(x, y) + local cx, cy = world.chunk_pos(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) + end + return world.chunks[cx][cy] +end + +function world.get_object(id) +end + +world.__index = world +return world |
