-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.lua
814 lines (684 loc) · 17.3 KB
/
graphics.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
-- Copyright (c) Jesse Weaver, 2021
--
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
module("graphics", package.seeall)
local std = require("std")
local ui = require "ui"
local function lerp2(a, b, t)
return {b[1] * t + a[1] * (1-t), b[2] * t + a[2] * (1-t)}
end
local function unpackEachv(input)
local result = std.list:clone()
for _, t in ipairs(input) do
for _, x in ipairs(t) do
result:insert(x)
end
end
return unpack(result)
end
local function unpackEach(...)
return unpackEachv({...})
end
local function drawLayers(...)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.setBlendMode('alpha', 'premultiplied')
for _, layer in ipairs({...}) do
love.graphics.draw(layer)
end
love.graphics.setBlendMode('alpha')
end
local Renderer = std.object:clone()
function Renderer:fontDescriptions()
return {}
end
Renderer.fonts = std.memoizedMember(
function(self) return ui.shape end,
function(self, s)
local fonts = {}
for key, description in pairs(self.fontDescriptions()) do
local path, relSize = unpack(description)
fonts[key] = love.graphics.newFont(path, s:pct(relSize))
end
return fonts
end
)
function memoizedCanvasRenderer(key, renderFunc)
return std.memoized(
key,
function(...)
local canvas = love.graphics.newCanvas()
love.graphics.setCanvas(canvas)
renderFunc(...)
love.graphics.setCanvas()
return canvas
end
)
end
local B_TOP_RADIUS = 0.4 -- Radius of outside of board as a proportion of the screen.
local B_TOP_CORNERNESS = 0.15 -- Amount that corners are drawn out.
local B_BOTTOM_RADIUS = 0.18 -- Radius of bottom of board as a proportion of the screen.
local B_START_ANGLE = -3/4 * math.pi -- The angle of t=1.
local B_GRID_COLOR = "#181818"
local B_BG_PATH_COLOR = "#0B0B0B"
local B_BG_SHADOW_COLOR = "#181818"
local B_BG_BLOCKED_COLOR = "#331111"
local B_BG_FLASH_COLOR = "#BBBBBB"
local B_GRID_HIGHLIGHT_COLOR = "#888888"
GridLayout = std.object:clone()
BoardRenderer = Renderer:clone()
function BoardRenderer:new(board, getClearedLineFlash)
return self:extend({
board = board,
getClearedLineFlash = getClearedLineFlash,
})
end
BoardRenderer.gridLayout = std.memoized(
function() return ui.shape end,
function(self, s)
-- Calculate top of board
local upperGridPositions = std.grid:new(self.board.circumf, self.board.depth)
local top_radius = s.smallest * (B_TOP_RADIUS + B_TOP_CORNERNESS)
local b_cornerness = top_radius * B_TOP_CORNERNESS
local step = 2 * math.pi / self.board.circumf
for t=1,self.board.circumf do
local angle = (t - 1) * step + B_START_ANGLE
local cornerness = (math.abs(math.sin((t - 1) * math.pi / self.board.width))) * b_cornerness
upperGridPositions[t][self.board.depth] = {
s.cx + math.cos(angle) * (top_radius - cornerness),
s.cy + math.sin(angle) * (top_radius - cornerness)
}
end
-- Calculate bottom of board
local gl = GridLayout:extend({
board = self.board,
bottomRadius = s.smallest * B_BOTTOM_RADIUS,
upperGridPositions = upperGridPositions,
})
local t = 1
-- North
for x = 1,self.board.width do
upperGridPositions[t][0] = gl:bottomGridPoint(x, 1)
t = t + 1
end
-- East
for y = 1,self.board.width do
upperGridPositions[t][0] = gl:bottomGridPoint(self.board.width+1, y)
t = t + 1
end
-- South
for x = self.board.width+1,2,-1 do
upperGridPositions[t][0] = gl:bottomGridPoint(x, self.board.width+1)
t = t + 1
end
-- West
for y = self.board.width+1,2,-1 do
upperGridPositions[t][0] = gl:bottomGridPoint(1, y)
t = t + 1
end
for t = 1, self.board.circumf do
for r = 1, self.board.depth - 1 do
upperGridPositions[t][r] = lerp2(
upperGridPositions[t][0],
upperGridPositions[t][self.board.depth],
r/self.board.depth
)
end
end
return gl
end
)
function GridLayout:bottomGridPoint(x, y)
local s = ui.shape
return {
s.cx + self.bottomRadius * (2 * ((x - 1) / self.board.width) - 1),
s.cy + self.bottomRadius * (2 * ((y - 1) / self.board.width) - 1),
}
end
function GridLayout:gridPoint(t, r, side)
side = side or self.board:side(t)
-- Coordinates on the sides of the bucket are easy.
if r > 0 then
t = self.board:normalizeT(t)
return self.upperGridPositions[t][r]
end
local x, y = self.board:radialToGrid(t, r, side)
if side == 1 then
-- North
elseif side == 2 then
-- East
x = x + 1
elseif side == 3 then
-- South
x = x + 1
y = y + 1
else
-- West
y = y + 1
end
return self:bottomGridPoint(x, y)
end
function BoardRenderer:drawSquare(t, r, graphics)
local gridLayout = self:gridLayout()
local side = self.board:side(t)
love.graphics.polygon(
'fill',
unpackEach(
gridLayout:gridPoint(t+1, r-1, side),
gridLayout:gridPoint(t, r-1, side),
gridLayout:gridPoint(t, r, side),
gridLayout:gridPoint(t+1, r, side)
)
)
end
function BoardRenderer:drawSide(side)
local gridLayout = self:gridLayout()
local points = std.list:clone()
points:insert(gridLayout:gridPoint(side * self.board.width + 1, 0, side))
points:insert(gridLayout:gridPoint((side - 1) * self.board.width + 1, 0, side))
for t = (side - 1) * self.board.width + 1, side * self.board.width + 1 do
points:insert(gridLayout:gridPoint(t, self.board.depth, side))
end
love.graphics.polygon('fill', unpackEachv(points))
end
local function hexToRgba(hex)
hex = hex:gsub('#', '')
local r = tonumber("0x" .. hex:sub(1, 2))
local g = tonumber("0x" .. hex:sub(3, 4))
local b = tonumber("0x" .. hex:sub(5, 6))
local a = tonumber("0x" .. hex:sub(7, 8))
if a ~= nil then
return r/255, g/255, b/255, a/255
else
return r/255, g/255, b/255
end
end
function BoardRenderer:drawPieceShadow()
local pieceSide = self.board:side(self.board.pieceT)
local lowestPieceR = {}
local minPieceT = 1/0
local maxPieceT = -1/0
for t, r in self.board:iterPieceSquares() do
if not lowestPieceR[t] or r < lowestPieceR[t] then
lowestPieceR[t] = r
end
if t < minPieceT then minPieceT = t end
if t > maxPieceT then maxPieceT = t end
end
local highestFallenPieceR = {}
local fallenPieceR = self.board.pieceR
while not self.board:pieceWouldCollide(self.board.pieceT, fallenPieceR - 1) do
fallenPieceR = fallenPieceR - 1
end
love.graphics.setColor(hexToRgba(B_BG_SHADOW_COLOR))
for t, r in self.board:iterPieceSquares(self.board.pieceT, fallenPieceR) do
if not highestFallenPieceR[t] or r > highestFallenPieceR[t] then
highestFallenPieceR[t] = r
end
self:drawSquare(t, r)
end
love.graphics.setColor(hexToRgba(B_BG_PATH_COLOR))
for t = minPieceT,maxPieceT do
for r = lowestPieceR[t] - 1, highestFallenPieceR[t] + 1, -1 do
self:drawSquare(t, r)
end
end
end
BoardRenderer.renderGridAnimationsGraphics = memoizedCanvasRenderer(
function(self)
local t, lastClearedLines = self.getClearedLineFlash()
return t, lastClearedLines, ui.shape
end,
function(self, t, lastClearedLines)
if t == nil or t == 0 then
return
end
local h, v = unpack(lastClearedLines)
local gridLayout = self:gridLayout()
local r, g, b, _ = hexToRgba(B_BG_FLASH_COLOR)
love.graphics.setColor(r, g, b, t * .6)
for y in h:values() do
love.graphics.polygon(
'fill',
unpackEach(
gridLayout:bottomGridPoint(1, y),
gridLayout:bottomGridPoint(1, y + 1),
gridLayout:bottomGridPoint(self.board.width + 1, y + 1),
gridLayout:bottomGridPoint(self.board.width + 1, y)
)
)
end
for x in v:values() do
love.graphics.polygon(
'fill',
unpackEach(
gridLayout:bottomGridPoint(x, 1),
gridLayout:bottomGridPoint(x + 1, 1),
gridLayout:bottomGridPoint(x + 1, self.board.width + 1),
gridLayout:bottomGridPoint(x, self.board.width + 1)
)
)
end
end
)
BoardRenderer.renderBackgroundGraphics = memoizedCanvasRenderer(
function(self) return self.board.pieceGeneration, ui.shape end,
function(self)
if true then
if self.board.piece then
self:drawPieceShadow()
end
else
love.graphics.setColor(hexToRgba(B_BG_PATH_COLOR))
if self.board.piece and self.board.pieceR - self.board.piece.height >= 0 then
self:drawSide(self.board:side(self.board.pieceT))
end
love.graphics.polygon(
'fill',
unpackEach(
self:bottomGridPoint(1, 1),
self:bottomGridPoint(self.board.width + 1, 1),
self:bottomGridPoint(self.board.width + 1, self.board.width + 1),
self:bottomGridPoint(1, self.board.width + 1)
)
)
end
love.graphics.setColor(hexToRgba(B_BG_BLOCKED_COLOR))
for side = 1,4 do
if self.board:isSideBlocked(side) then
self:drawSide(side)
end
end
end
)
function BoardRenderer:drawSquareGraphics()
for t, r, color in self.board:iterOccupiedSquares() do
love.graphics.setColor(hexToRgba(color .. "99"))
self:drawSquare(t, r)
end
end
function BoardRenderer:drawGridLineGraphics()
local gl = self:gridLayout()
-- Lines up the side of the bucket
love.graphics.setColor(hexToRgba(B_GRID_COLOR))
for t=1,self.board.circumf do
love.graphics.line(
gl.upperGridPositions[t][0][1],
gl.upperGridPositions[t][0][2],
gl.upperGridPositions[t][self.board.depth][1],
gl.upperGridPositions[t][self.board.depth][2]
)
end
-- Inner grid lines
for x=2,self.board.width do
love.graphics.line(unpackEach(
gl:bottomGridPoint(x, 1),
gl:bottomGridPoint(x, self.board.width+1)
))
end
for y=2,self.board.width do
love.graphics.line(unpackEach(
gl:bottomGridPoint(1, y),
gl:bottomGridPoint(self.board.width+1, y)
))
end
-- Outer, highlighted grid lines
love.graphics.setColor(hexToRgba(B_GRID_HIGHLIGHT_COLOR))
love.graphics.line(unpackEach(
gl:bottomGridPoint(1, 1),
gl:bottomGridPoint(1, self.board.width+1)
))
love.graphics.line(unpackEach(
gl:bottomGridPoint(self.board.width+1, 1),
gl:bottomGridPoint(self.board.width+1, self.board.width+1)
))
love.graphics.line(unpackEach(
gl:bottomGridPoint(1, 1),
gl:bottomGridPoint(self.board.width+1, 1)
))
love.graphics.line(unpackEach(
gl:bottomGridPoint(1, self.board.width+1),
gl:bottomGridPoint(self.board.width+1, self.board.width+1)
))
-- Horizontal grid lines on outside of bucket
for r=1,self.board.depth do
local points = std.list:clone()
points:insert(lerp2(
gl.upperGridPositions[self.board.circumf][0],
gl.upperGridPositions[self.board.circumf][self.board.depth],
r/self.board.depth
))
for t=1,self.board.circumf do
points:insert(lerp2(
gl.upperGridPositions[t][0],
gl.upperGridPositions[t][self.board.depth],
r/self.board.depth
))
end
if r == self.board.depth then
love.graphics.setColor(hexToRgba(B_GRID_HIGHLIGHT_COLOR))
else
love.graphics.setColor(hexToRgba(B_GRID_COLOR))
end
love.graphics.line(unpackEachv(points))
end
end
BoardRenderer.renderGridGraphics = memoizedCanvasRenderer(
function(self) return self.board.gridGeneration, ui.shape end,
function(self)
self:drawSquareGraphics()
self:drawGridLineGraphics()
end
)
BoardRenderer.renderPieceGraphics = memoizedCanvasRenderer(
function(self) return self.board.pieceGeneration, ui.shape end,
function(self)
if not self.board.piece then return end
love.graphics.setColor(hexToRgba(self.board.piece.color))
for t, r in self.board:iterPieceSquares() do
self:drawSquare(t, r)
end
end
)
function BoardRenderer:draw()
drawLayers(
self:renderBackgroundGraphics(),
self:renderGridAnimationsGraphics(),
self:renderPieceGraphics(),
self:renderGridGraphics()
)
end
PieceHintRenderer = Renderer:clone()
function PieceHintRenderer:new(getNextPiece)
return self:extend({
getNextPiece = getNextPiece,
})
end
function PieceHintRenderer:fontDescriptions()
return {
main = {"fonts/AlegreyaSansSC-Light.ttf", 6},
}
end
function drawPiece(p, pieceX, pieceY, gridSize)
local xPadding = gridSize * ((4 - p.width) / 2)
love.graphics.setColor(hexToRgba(p.color .. 'bb'))
for x = 1,p.width do
for y = 1,p.height do
if p[x + p.xOffset][y + p.yOffset] then
love.graphics.rectangle(
'fill',
pieceX + xPadding + (x - 1) * gridSize,
pieceY + (y - 1) * gridSize,
gridSize - 1,
gridSize - 1
)
end
end
end
end
function PieceHintRenderer:draw()
local s = ui.shape
local xOffset = ui.isMobile and s:pct(10) or 0
local width = s:pct(20)
local hintX = s.fullWidth - s:pct(5) - width - xOffset
love.graphics.printf(
"Next",
self:fonts().main,
hintX,
s:pct(3),
width,
"center"
)
local p = self.getNextPiece()
local pieceY = s:pct(10)
local gridSize = width / 4
drawPiece(p, hintX, pieceY, gridSize)
end
ScoreRenderer = Renderer:clone()
function ScoreRenderer:new(getLevel, getScore, getClearedLines)
return self:extend({
getLevel = getLevel,
getScore = getScore,
getClearedLines = getClearedLines,
})
end
function ScoreRenderer:fontDescriptions()
return {
title = {"fonts/AlegreyaSansSC-Light.ttf", 6},
main = {"fonts/AlegreyaSansSC-Medium.ttf", 5},
}
end
function ScoreRenderer:draw()
local s = ui.shape
local width = s:pct(15)
local addPct = s:pctAccum()
love.graphics.setColor(1, 1, 1, 1)
love.graphics.printf(
"Level",
self:fonts().title,
s:pct(5),
addPct(3),
width,
"left"
)
love.graphics.printf(
self.getLevel(),
self:fonts().main,
s:pct(5),
addPct(5.5),
width,
"left"
)
love.graphics.printf(
"Score",
self:fonts().title,
s:pct(5),
addPct(6.5),
width,
"left"
)
love.graphics.printf(
self.getScore(),
self:fonts().main,
s:pct(5),
addPct(5.5),
width,
"left"
)
love.graphics.printf(
"Lines",
self:fonts().title,
s:pct(5),
addPct(6.5),
width,
"left"
)
love.graphics.printf(
self.getClearedLines(),
self:fonts().main,
s:pct(5),
addPct(5.5),
width,
"left"
)
end
StartRenderer = Renderer:clone()
function StartRenderer:fontDescriptions()
return {
header = {"fonts/AlegreyaSansSC-Light.ttf", 10},
start = {"fonts/AlegreyaSansSC-Light.ttf", 5},
}
end
function StartRenderer:draw()
local s = ui.shape
love.graphics.printf(
"Bucket",
self:fonts().header,
s.cx - s:pct(50),
s.cy - s:pct(20),
s.smallest,
"center"
)
love.graphics.printf(
ui.isMobile and 'Tap to start' or 'Press Space to start',
self:fonts().start,
s.cx - s:pct(50),
s.cy + s:pct(10),
s.smallest,
"center"
)
end
LossRenderer = Renderer:clone()
function LossRenderer:new(getOpacity)
return self:extend({
getOpacity = getOpacity,
})
end
function LossRenderer:fontDescriptions()
return {
main = {"fonts/AlegreyaSansSC-Light.ttf", 10},
start = {"fonts/AlegreyaSansSC-Light.ttf", 5},
}
end
function LossRenderer:draw()
local s = ui.shape
love.graphics.setColor(0.1, 0, 0, self.getOpacity() * .9)
love.graphics.rectangle(
'fill',
0,
0,
s.fullWidth,
s.fullHeight
)
love.graphics.setColor(1, 1, 1, self.getOpacity())
love.graphics.printf(
"Game Over",
self:fonts().main,
s.cx - s:pct(50),
s.cy - s:pct(15),
s.smallest,
"center"
)
love.graphics.printf(
ui.isMobile and 'Tap to restart' or 'Press Space to restart',
self:fonts().start,
s.cx - s:pct(50),
s.cy + s:pct(5),
s.smallest,
"center"
)
end
PauseRenderer = Renderer:clone()
function PauseRenderer:new(getOpacity)
return self:extend({
getOpacity = getOpacity,
})
end
function PauseRenderer:fontDescriptions()
return {
main = {"fonts/AlegreyaSansSC-Light.ttf", 10},
start = {"fonts/AlegreyaSansSC-Light.ttf", 5},
}
end
function PauseRenderer:draw()
local s = ui.shape
love.graphics.setColor(0.15, 0.15, 0, self.getOpacity() * 0.7)
love.graphics.rectangle(
'fill',
0,
0,
s.fullWidth,
s.fullHeight
)
love.graphics.setColor(1, 1, 1, self.getOpacity())
love.graphics.printf(
"Paused",
self:fonts().main,
s.cx - s:pct(50),
s.cy - s:pct(15),
s.smallest,
"center"
)
love.graphics.printf(
ui.isMobile and 'Tap to continue' or 'Press Space to continue',
self:fonts().start,
s.cx - s:pct(50),
s.cy + s:pct(5),
s.smallest,
"center"
)
end
ButtonsRenderer = Renderer:clone()
function ButtonsRenderer:new(getButtons)
return self:extend({
getButtons = getButtons,
})
end
function ButtonsRenderer:fontDescriptions()
return {
main = {"fonts/bootstrap-icons.ttf", 8},
}
end
function ButtonsRenderer:draw()
local s = ui.shape
love.graphics.setColor(1, 1, 1, .8)
for _, button in ipairs(self.getButtons()) do
love.graphics.rectangle(
'line',
button.x,
button.y + s:pct(.5),
button.width,
button.height,
s:pct(1),
s:pct(1)
)
if button.label ~= nil then
love.graphics.printf(
button.label,
self:fonts().main,
button.x,
button.y + button.height / 2 - s:pct(3),
button.width,
"center"
)
end
end
end
PieceGalleryRenderer = Renderer:clone()
function PieceGalleryRenderer:new(pieces)
return self:extend({
pieces = pieces,
})
end
function PieceGalleryRenderer:draw()
local s = ui.shape
local rows = 1
local cellSize, columns
while true do
cellSize = s.fullHeight / rows
columns = math.floor(s.fullWidth / cellSize)
if rows * columns >= #self.pieces then
break
else
rows = rows + 1
end
end
local gridSize = cellSize / 6
local cellPadding = gridSize / 2
local pieceI = 1
for row = 1,rows do
local pieceY = (row - 1) * cellSize
for column = 1,columns do
local pieceX = (column - 1) * cellSize
drawPiece(self.pieces[pieceI], pieceX, pieceY, gridSize)
pieceI = pieceI + 1
if pieceI > #self.pieces then
break
end
end
if pieceI > #self.pieces then
break
end
end
end