-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.lua
178 lines (153 loc) · 6.41 KB
/
nodes.lua
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require('textures')
require('xy_map')
------------------------
-- A node representation of the world
------------------------
-- all nodes in the world
nodes = {}
nodes.storage = newXYMap()
nodes.names = {}
local function put_on_screen(texture, x, y)
love.graphics.draw(texture.image, texture.quad, x * SIZE * draw.scale, y * SIZE * draw.scale, 0, draw.scale, draw.scale)
end
-- Generates a boolean binary tree of depth n
local function binaryTree(n)
assert(n >= 0)
if (n ~= 0) then
local out = {}
out[true] = binaryTree(n - 1)
out[false] = binaryTree(n - 1)
return out
else
return function() end
end
end
local function try(x, y)
return not not (nodes:isWalkable(x,y) or not nodes:contains(x,y))
end
function nodes:contains(x, y)
return nodes.storage:contains(x, y)
end
local function floorTextureRender(texture)
local textureMap = binaryTree(4)
--[[
-- 2
-- 1 3
-- 4
]]
-- true means walkable
textureMap[false][false][false][false] = function(x,y, texture) put_on_screen(texture[6][1], x, y) end
textureMap[false][false][false][true] = function(x,y, texture) put_on_screen(texture[4][1], x, y) end
textureMap[false][false][true][false] = function(x,y, texture) put_on_screen(texture[5][2], x, y) end
textureMap[false][false][true][true] = function(x,y, texture) put_on_screen(texture[1][1], x, y) end
textureMap[false][true][false][false] = function(x,y, texture) put_on_screen(texture[4][2], x, y) end
textureMap[false][true][false][true] = function(x,y, texture) put_on_screen(texture[4][2], x, y) end
textureMap[false][true][true][false] = function(x,y, texture) put_on_screen(texture[1][3], x, y) end
textureMap[false][true][true][true] = function(x,y, texture) put_on_screen(texture[1][2], x, y) end
textureMap[true][false][false][false] = function(x,y, texture) put_on_screen(texture[7][2], x, y) end
textureMap[true][false][false][true] = function(x,y, texture) put_on_screen(texture[3][1], x, y) end
textureMap[true][false][true][false] = function(x,y, texture) put_on_screen(texture[6][2], x, y) end
textureMap[true][false][true][true] = function(x,y, texture) put_on_screen(texture[2][1], x, y) end
textureMap[true][true][false][false] = function(x,y, texture) put_on_screen(texture[3][3], x, y) end
textureMap[true][true][false][true] = function(x,y, texture) put_on_screen(texture[3][2], x, y) end
textureMap[true][true][true][false] = function(x,y, texture) put_on_screen(texture[2][3], x, y) end
textureMap[true][true][true][true] = function(x,y, texture) put_on_screen(texture[2][2], x, y) end
return function(x,y)
textureMap[try(x-1,y)][try(x,y-1)][try(x+1,y)][try(x,y+1)](x, y, texture)
end
end
local function wallTextureRender(texture)
local textureMap = binaryTree(4)
--[[
-- 2
-- 1 3
-- 4
]]
-- true means walkable
textureMap[false][false][false][false] = function(x,y) put_on_screen(texture[5][2], x, y) end
textureMap[false][false][false][true] = function(x,y) put_on_screen(texture[5][3], x, y) end
textureMap[false][false][true][false] = function(x,y) put_on_screen(texture[6][2], x, y) end
textureMap[false][false][true][true] = function(x,y) put_on_screen(texture[3][3], x, y) end
textureMap[false][true][false][false] = function(x,y) put_on_screen(texture[5][1], x, y) end
textureMap[false][true][false][true] = function(x,y) put_on_screen(texture[2][1], x, y) end
textureMap[false][true][true][false] = function(x,y) put_on_screen(texture[3][1], x, y) end
textureMap[false][true][true][true] = function(x,y) put_on_screen(texture[3][3], x, y) end
textureMap[true][false][false][false] = function(x,y) put_on_screen(texture[4][2], x, y) end
textureMap[true][false][false][true] = function(x,y) put_on_screen(texture[1][3], x, y) end
textureMap[true][false][true][false] = function(x,y) put_on_screen(texture[1][2], x, y) end
textureMap[true][false][true][true] = function(x,y) put_on_screen(texture[2][2], x, y) end
textureMap[true][true][false][false] = function(x,y) put_on_screen(texture[1][1], x, y) end
textureMap[true][true][false][true] = function(x,y) put_on_screen(texture[1][3], x, y) end
textureMap[true][true][true][false] = function(x,y) put_on_screen(texture[1][2], x, y) end
textureMap[true][true][true][true] = function(x,y) put_on_screen(texture[2][2], x, y) end
return function(x,y)
textureMap[try(x-1,y)][try(x,y-1)][try(x+1,y)][try(x,y+1)](x, y, texture)
end
end
function nodes:load()
nodes.names["dirt_floor"] = {
name = "dirt_floor",
walkable = true,
deadly = false,
render = floorTextureRender(textures["dirt_floor"])
}
nodes.names["dirt_wall"] = {
name = "dirt_wall",
walkable = false,
deadly = true,
render = wallTextureRender(textures["dirt_wall"])
}
nodes.names["stone_floor"] = {
name = "stone_floor",
walkable = true,
deadly = false,
render = floorTextureRender(textures["stone_floor"])
}
nodes.names["stone_wall"] = {
name = "stone_wall",
walkable = false,
deadly = true,
render = wallTextureRender(textures["stone_wall"])
}
nodes.names["spikes"] = {
name = "spikes",
walkable = true,
deadly = true,
render = function(x, y)
put_on_screen(textures.spikes, x, y)
end
}
end
function nodes:addNode(x, y, name)
if nodes.names[name] then
nodes.storage:add(x, y, nodes.names[name])
else
error("Node " .. name .. " doesn't exist")
end
end
function nodes:safeAddNode(x, y, name)
nodes.storage:safeAdd(x, y, nodes.names[name])
end
function nodes:isWalkable(x, y)
return not not (nodes.storage:get(x, y) and nodes.storage:get(x, y).walkable)
end
function nodes:isDeadly(x, y)
return not not (nodes.storage:get(x, y) and nodes.storage:get(x, y).deadly)
end
function nodes:render()
nodes.storage:forEach(function(x, y, node)
if (math.abs(x - player.x) * SIZE * draw.scale < love.graphics.getWidth()
and math.abs(y - player.y) * SIZE * draw.scale < love.graphics.getHeight()) then
node.render(x, y)
end
end)
end
function nodes:forEach(fun)
nodes.storage:forEach(fun)
end
function nodes:clear()
nodes.storage:clear()
end
function nodes:remove(x, y)
nodes.storage:remove(x, y)
end