-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example_-_tile map lighting.lua
192 lines (181 loc) · 5.35 KB
/
Example_-_tile map lighting.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
-- Map Torches
-- tilemap with lighting(no multiple torches-untested)
-- basically a 4way dijkstra fill with lower transparancy at every step
-- addtional less transparancy when block 1(solid)
-- ai is a move to target(random destination)
-- Use this function to perform your initial setup
function setup()
print("Hello World! - restart if weird/stuck")
mw = 50
mh = 50
tw = WIDTH/mw
th = HEIGHT/mh
map = {}
lightmap = {}
for x=0,mw do
map[x]={}
lightmap[x]={}
for y=0,mh do
map[x][y]=0
lightmap[x][y]=0
end
end
for i=1,3 do
wallrect(math.random(5,30),math.random(5,30),20,3)
end
x = math.random(1,mw-1)
y = math.random(1,mh-1)
while map[x][y]==1 do
x = math.random(1,mw-1)
y = math.random(1,mh-1)
end
placetorch(x,y)
lmpos = vec2(x,y)
lmdes = vec2(10,10)
cnt=0
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(0,0,0)
-- move the torch around
cnt=cnt+1
if cnt>8 then
cnt=0
oldx = lmpos.x
oldy = lmpos.y
if lmpos.x < lmdes.x then lmpos.x=lmpos.x+1 end
if lmpos.y < lmdes.y then lmpos.y=lmpos.y+1 end
if lmpos.x > lmdes.x then lmpos.x=lmpos.x-1 end
if lmpos.y > lmdes.y then lmpos.y=lmpos.y-1 end
if map[lmpos.x][lmpos.y]==1 then
lmpos.x = oldx
lmpos.y = oldy
end
if math.random()<.1 then
lmdes.x = math.random(0,mw)
lmdes.y = math.random(0,mh)
while lightmap[lmdes.x][lmdes.y]<20 and lmdes:dist(vec2(oldx,oldy))<5 do
lmdes.x = math.random(0,mw)
lmdes.y = math.random(0,mh)
end
end
clearlightmap()
placetorch(lmpos.x,lmpos.y)
end
-- Do your drawing here
drawmap()
fill(255,255,0)
stroke(255)
rect(lmpos.x*tw,lmpos.y*th,tw,th)
end
function clearlightmap()
for x=0,mw do
lightmap[x]={}
for y=0,mh do
lightmap[x][y]=0
end
end
end
function placetorch(x,y)
orig = vec2(x,y)
openlist={}
table.insert(openlist,vec2(x,y))
lightmap[x][y]=255
-- left top right bottom of our position
--switch = {vec2(-1,0),vec2(0,-1),vec2(1,0),vec2(0,1)}
switch = {vec2(-1,0),vec2(-1,-1),vec2(0,-1),vec2(1,-1),vec2(1,0),vec2(1,1),vec2(0,1),vec2(-1,1)}
while #openlist>0 do
x = openlist[1].x
y = openlist[1].y
table.remove(openlist,1)
if lightmap[x][y]>10 and vec2(x,y):dist(orig)<12 then
for i = 1,#switch do
pos = vec2(x+switch[i].x,y+switch[i].y)
if pos.x>=0 and pos.y>=0 and pos.x<mw and pos.y<mh then
if lightmap[pos.x][pos.y]==0 then
if map[x][y]==0 then
lightmap[pos.x][pos.y]=lightmap[x][y]/100*90
else
lightmap[pos.x][pos.y]=lightmap[x][y]/100*60
end
if map[pos.x][pos.y]==1 then
lightmap[pos.x][pos.y]=lightmap[pos.x][pos.y]/2
end
if lightmap[pos.x][pos.y]<0 then lightmap[pos.x][pos.y]=0 end
if lightmap[pos.x][pos.y]>0 then
table.insert(openlist,vec2(pos.x,pos.y))
end
end
end
end
end
end
blendlight2(orig.x,orig.y)
end
function blendlight2(x,y)
for x1 = x-10,x+10 do
for y1 = y-10,y+10 do
if x1>=0 and y1>=0 and x1<mw and y1<mh then
z=0
d=0
for y2=y1-1,y1+1 do
for x2=x1-1,x1+1 do
if x2>=0 and y2>=0 and x2<mw and y2<mh then
z=z+lightmap[x2][y2]
d=d+1
end
end
end
lightmap[x1][y1]=z/d
end
end
end
end
-- this is a flickering style of lighting
function blendlight(x,y)
for i = 1,(6*6)*100 do
x1 = x+math.random(-6,6)
y1 = y+math.random(-6,6)
if x1>=0 and y1>=0 and x1<mw and y1<mh then
z=0
d=0
for y2=y1-1,y1+1 do
for x2=x1-1,x1+1 do
if x2>=0 and y2>=0 and x2<mw and y2<mh then
z=z+lightmap[x2][y2]
d=d+1
end
end
end
lightmap[x1][y1]=z/d
end
end
end
function wallrect(x,y,w,h)
for y1=y,y+h do
for x1=x,x+w do
if x1>=0 and y1>=0 and x1<mw and y1<mh then
map[x1][y1]=1
end
end
end
end
function drawmap()
for y = 0,mh,1 do
for x = 0,mw,1 do
if lightmap[x][y]>0 then
if map[x][y]==0 then
fill(40,40,0,lightmap[x][y])
stroke(40,40,0,lightmap[x][y])
rect(x*tw,y*th,tw,th)
end
if map[x][y]==1 then
fill(255,155,55,lightmap[x][y])
stroke(255,155,55,lightmap[x][y])
rect(x*tw,y*th,tw,th)
end
end
end
end
end