-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.lua
379 lines (216 loc) · 6.66 KB
/
background.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local math = _tl_compat and _tl_compat.math or math; local os = _tl_compat and _tl_compat.os or os; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string; require("love")
require("fonts")
require("common")
local g = love.graphics
local inspect = require("inspect")
local pallete = require("pallete")
Background = {}
local Background_mt = {
__index = Background,
}
local Block = {}
local Block_mt = {
__index = Block,
}
function Background.new()
print('Background.new()')
local self = {
bsize = 128,
fragmentCode = [[
extern float time;
vec4 effect(vec4 color, Image image, vec2 uvs, vec2 screen_coords) {
vec4 pixel = Texel(image, uvs);
float av = (pixel.r + pixel.g + pixel.b) / time;
return pixel * color;
}
]],
tile = love.graphics.newImage(SCENEPREFIX .. "gfx/IMG_20190111_115755.png"),
blockSize = 128,
emptyNum = 1,
execList = {},
paused = false,
}
self = setmetatable(self, Background_mt)
local blocksPath = "gfx/blocks"
local files = love.filesystem.getDirectoryItems(blocksPath)
self.tiles = {}
for _, v in ipairs(files) do
local ok, errmsg = pcall(function()
self.tiles[#self.tiles + 1] = love.graphics.newImage(blocksPath ..
"/" .. v)
end)
if not ok then
print(errmsg)
end
end
self:fillGrid()
self:resize(g.getDimensions())
return self
end
function Block.new(back, img, xidx,
yidx, duration)
local self = {
back = back,
img = img,
x = (xidx - 1) * back.bsize,
y = (yidx - 1) * back.bsize,
xidx = xidx,
yidx = yidx,
active = false,
duration = duration,
}
return setmetatable(self, Block_mt)
end
function Block:draw()
local quad = g.newQuad(0, 0, self.img:getWidth(), self.img:getHeight(),
self.img:getWidth(), self.img:getHeight())
g.setColor({ 1, 1, 1, 1 })
g.draw(self.img, quad, self.x, self.y, 0, self.back.bsize /
self.img:getWidth(), self.back.bsize / self.img:getHeight())
if self.active then
g.setColor({ 1, 1, 1 })
local oldLineWidth = g.getLineWidth()
g.setLineWidth(3)
g.setLineWidth(oldLineWidth)
end
local oldFont = g.getFont()
g.setColor({ 0, 0, 0 })
g.setFont(oldFont)
end
function Block:move(dirx, diry)
self.dirx = dirx
self.diry = diry
self.newXidx = self.xidx + dirx
self.newYidx = self.yidx + diry
self.active = true
self.animCounter = self.back.bsize
end
function Block:process(dt)
local ret = false
local speed = 20
local ds = (dt * speed)
if self.animCounter - ds > 0 then
self.animCounter = self.animCounter - ds
self.x = self.x + self.dirx * ds
self.y = self.y + self.diry * ds
ret = true
else
self.active = false
self.back.blocks[math.floor(self.xidx)][math.floor(self.yidx)] = {}
self.back.blocks[math.floor(self.newXidx)][math.floor(self.newYidx)] = self
self.oldXidx, self.oldYidx = self.xidx, self.yidx
self.xidx = self.newXidx
self.yidx = self.newYidx
end
return ret
end
function Background:get(xidx, yidx)
local firstColumn = self.blocks[1]
if xidx >= 1 and xidx <= #self.blocks and
yidx >= 1 and yidx <= #firstColumn then
return self.blocks[xidx][yidx]
else
return nil
end
end
function Background:findDirection(xidx, yidx)
local x, y = xidx, yidx
local columnMax = #self.blocks[1]
local directions = { xidx - 1 >= 1, yidx - 1 >= 1, xidx + 1 <= columnMax,
yidx + 1 <= columnMax, }
local inserted = false
local j = 1
while not inserted do
local dir = math.random(1, 4)
if dir == 1 then
if directions[dir] and self.blocks[xidx - 1] and
self.blocks[xidx - 1][yidx] then
x = x - 1
inserted = true
end
elseif dir == 2 then
if directions[dir] and self.blocks[xidx] and
self.blocks[xidx][yidx - 1] then
y = y - 1
inserted = true
end
elseif dir == 3 then
if directions[dir] and self.blocks[xidx + 1] and
self.blocks[xidx + 1][yidx] then
x = x + 1
inserted = true
end
elseif dir == 4 then
if directions[dir] and self.blocks[xidx] and
self.blocks[xidx][yidx + 1] then
y = y + 1
inserted = true
end
end
j = j + 1
if j > 16 then
error(string.format(
"Something wrong in block placing alrogithm on [%d][%d].",
xidx, yidx))
end
end
return x, y
end
function Background:fillGrid()
local w, h = g.getDimensions()
self.blocks = {}
local xcount, ycount = math.floor(w / self.bsize) + 1,
math.floor(h / self.bsize) + 1
for i = 1, xcount do
local column = {}
for j = 1, ycount do
column[#column + 1] = Block.new(self, self.tile, i, j, 1000)
end
self.blocks[#self.blocks + 1] = column
end
math.randomseed(os.time())
local fieldWidth, fieldHeight = #self.blocks, #self.blocks[1]
self.execList = {}
for _ = 1, self.emptyNum do
local xidx = math.random(1, fieldWidth)
local yidx = math.random(1, fieldHeight)
self.blocks[xidx][yidx] = {}
local x, y = self:findDirection(xidx, yidx)
self.blocks[x][y]:move(xidx - x, yidx - y)
self.execList[#self.execList + 1] = self.blocks[x][y]
end
end
function Background:update(dt)
if self.paused then return end
for k, v in ipairs(self.execList) do
local block = v
local ret = block:process(dt)
if not ret then
local xidx, yidx = block.oldXidx or block.xidx,
block.oldYidx or block.yidx
local x, y = self:findDirection(math.floor(xidx), math.floor(yidx))
if self.blocks[x][y].move then
self.blocks[x][y]:move(xidx - x, yidx - y)
elseif self.blocks[x][y] == {} then
error("{} block")
else
error("Ououou " .. inspect(self.blocks[x][y]))
end
self.execList[k] = self.blocks[x][y]
end
end
end
function Background:draw()
g.clear(pallete.background)
for _, v in ipairs(self.blocks) do
for _, p in ipairs(v) do
if p.draw then p:draw() end
end
end
end
function Background:resize(_, _)
self:fillGrid()
end
return {
new = Background.new,
}