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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
-- Tilemap entity, it dynamically rebuilds its draw cache thingy
-- You just need to worry not and use the tile getting/setting functions
TILESIZE = 16
function new_tilemap(width, height)
local map = new_entity()
add_comp(map, "Tilemap", width, height)
return map
end
local function ID(map, x, y)
return x + y * map.width + 1
end
-- local function POS(map, id)
-- id = id - 1
-- return id % map.width, math.floor(id / map.width)
-- end
register_comp("Tilemap", function(tilemap, width, height)
tilemap.tiledata = {}
for i=1, width*height do
tilemap.tiledata[i] = 0
end
tilemap.width = width
tilemap.height = height
tilemap.cache = {}
tilemap.cachesize = 0
tilemap.needs_rebuild = false
end)
function rebuild_tilemap(map)
map.cache = {}
for x = 0, map.width - 1 do
for y = 0, map.height - 1 do
local tile = get_tile(map, x, y)
if tile ~= 0 then
map.cachesize = map.cachesize + 1
map.cache[map.cachesize] = {
x = x * TILESIZE, y = y * TILESIZE,
}
end
end
end
end
function tilemap_draw_sys(tilemap)
if tilemap.needs_rebuild then
rebuild_tilemap(tilemap)
tilemap.needs_rebuild = false
end
for i=1, tilemap.cachesize do
local tile = tilemap.cache[i]
lg.circle("fill", tile.x, tile.y, 4)
end
end
function get_tile(map, x, y)
return map.tiledata[ID(map, x, y)]
end
function queue_tilemap_rebuild(tilemap)
tilemap.needs_rebuild = true
end
function set_tile(map, x, y, tileid)
map.tiledata[ID(map, x, y)] = tileid
map.needs_rebuild = true
end
function remove_tile(map, x, y)
map.tiledata[ID(map, x, y)] = 0
map.needs_rebuild = true
end
|