blob: 432e37b624327fe7acd5171fc49c967dc3665922 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
local CHUNK_SIZE = 128
-- for now tiles shall be booleans
local function index(offq,offr)
-- indexes start at 0
-- and go up to (CHUNK_SIZE^2)-1
assert(0<=offq and 0<=offr and offq<CHUNK_SIZE and offr<CHUNK_SIZE, "chunk hex offset out of bounds")
return CHUNK_SIZE*offq + offr
end
local Chunk = {}
Chunk.__index = Chunk
function Chunk.make()
-- todo actual worldgen
local tiles = {}
for i=0,CHUNK_SIZE-1 do
for j=0,CHUNK_SIZE-1 do
tiles[index(i,j)] = (math.random()<0.5)
end
end
return setmetatable({tiles=tiles},Chunk)
end
function Chunk.tile_at_offset(self,hoffs)
if not(0<=hoffs.q and 0<=hoffs.r and hoffs.q<CHUNK_SIZE and hoffs.r<CHUNK_SIZE) then return nil end
local idx = CHUNK_SIZE*hoffs.q + hoffs.r
return self.tiles[idx]
end
return {
Chunk=Chunk,
SIZE=CHUNK_SIZE,
index=index
}
|