-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtetrix.nelua
571 lines (516 loc) · 13.6 KB
/
tetrix.nelua
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
## pragma{nogc = true, noerrorloc = true}
global CELL_SIZE <comptime> = 12
global VERT_CELLS <comptime> = 20
global HORZ_CELLS <comptime> = 10
global GRID_WIDTH <comptime> = CELL_SIZE*HORZ_CELLS
global GRID_HEIGHT <comptime> = CELL_SIZE*VERT_CELLS
global SCREEN_WIDTH <comptime> = 256
global SCREEN_HEIGHT <comptime> = 256
global GRID_OFFSET_X <comptime> = (SCREEN_WIDTH - GRID_WIDTH)*3//4
global GRID_OFFSET_Y <comptime> = (SCREEN_HEIGHT - GRID_HEIGHT)//2
global CELL_MARGIN <comptime> = 1
global Color: type = @byte
require 'riv'
require 'math'
--------------------------------------------------------------------------------
-- Piece
local Piece = @record{
x: integer,
y: integer,
width: integer,
height: integer,
size: integer,
layout: [4][4]byte,
color: uint8
}
local PIECES: [7]Piece = {
{ size=4, layout={
{0,0,0,0},
{1,1,1,1},
{0,0,0,0},
{0,0,0,0},
},
width=4, height=3,
color=RIV_COLOR_LIGHTBLUE
},
{ size=3, layout={
{0,1,0,0},
{1,1,1,0},
{0,0,0,0},
{0,0,0,0},
},
width=3, height=2,
color=RIV_COLOR_PINK
},
{ size=3, layout={
{0,1,1,0},
{1,1,0,0},
{0,0,0,0},
{0,0,0,0},
},
width=3, height=2,
color=RIV_COLOR_GREEN
},
{ size=3, layout={
{1,1,0,0},
{0,1,1,0},
{0,0,0,0},
{0,0,0,0},
},
width=3, height=2,
color=RIV_COLOR_RED
},
{ size=3, layout={
{1,0,0,0},
{1,1,1,0},
{0,0,0,0},
{0,0,0,0},
},
width=3, height=2,
color=RIV_COLOR_LIGHTTEAL
},
{ size=3, layout={
{0,0,1,0},
{1,1,1,0},
{0,0,0,0},
{0,0,0,0},
},
width=3, height=2,
color=RIV_COLOR_ORANGE
},
{ size=2, layout={
{1,1,0,0},
{1,1,0,0},
{0,0,0,0},
{0,0,0,0},
},
width=2, height=2,
color=RIV_COLOR_YELLOW
}
}
function Piece.random_piece()
local index = riv_rand_uint(#PIECES-1)
local piece = PIECES[index]
piece.x = (HORZ_CELLS - piece.size) // 2
piece.y = 0
return piece
end
function Piece:rotate_left()
local layout = self.layout
for i=0,self.size-1 do
for j=0,self.size-1 do
self.layout[i][j] = layout[j][self.size-1-i]
end
end
end
function Piece:rotate_right()
local layout = self.layout
for i=0,self.size-1 do
for j=0,self.size-1 do
self.layout[j][self.size-1-i] = layout[i][j]
end
end
end
function Piece:translate(x: integer, y: integer)
self.x = self.x + x
self.y = self.y + y
end
function Piece.draw_cell(x: integer, y: integer, color: Color, shallow: boolean)
if color == 0 then return end
local x = x + CELL_MARGIN
local y = y + CELL_MARGIN
local width = CELL_SIZE - CELL_MARGIN
local height = CELL_SIZE - CELL_MARGIN
if not shallow then
riv_draw_rect_fill(x, y, width, height, color)
else
riv_draw_rect_fill(x, y, width, height, RIV_COLOR_SLATE)
riv_draw_rect_line(x, y, width, height, color)
end
end
function Piece:draw(x: integer, y: integer, shallow: boolean)
for iy=0,self.size-1 do
for ix=0,self.size-1 do
if self.layout[iy][ix] ~= 0 then
local x = x + ix*CELL_SIZE
local y = y + iy*CELL_SIZE
Piece.draw_cell(x, y, self.color, shallow)
end
end
end
end
--------------------------------------------------------------------------------
-- Board
local Board = @record{
cells: [VERT_CELLS][HORZ_CELLS]Color
}
function Board:piece_collides(piece: *Piece)
for iy=0,piece.size-1 do
for ix=0,piece.size-1 do
local tx, ty = piece.x+ix, piece.y+iy
if piece.layout[iy][ix] ~= 0 then
if tx < 0 or tx >= HORZ_CELLS or ty < 0 or ty >= VERT_CELLS then
return true
end
if self.cells[ty][tx] ~= 0 then
return true
end
end
end
end
return false
end
function Board:place_piece(piece: *Piece)
for iy=0,piece.size-1 do
for ix=0,piece.size-1 do
local tx, ty = piece.x+ix, piece.y+iy
if piece.layout[iy][ix] ~= 0 then
self.cells[ty][tx] = piece.color
end
end
end
end
function Board:clear_lines()
local num_clears = 0
for y=0,VERT_CELLS-1 do
-- the if line y is full
local full = true
for x=0,HORZ_CELLS-1 do
if self.cells[y][x] == 0 then
full = false
break
end
end
-- slide lines down
if full then
num_clears = num_clears + 1
for ny=y,1,-1 do
for x=0,HORZ_CELLS-1 do
self.cells[ny][x] = self.cells[ny-1][x]
end
end
end
end
return num_clears
end
local function draw_grid()
local GRID_COLOR: Color <const> = RIV_COLOR_DARKSLATE
local GAP_COLOR: Color <const> = RIV_COLOR_SLATE
riv_draw_rect_fill(GRID_OFFSET_X, GRID_OFFSET_Y, GRID_WIDTH, GRID_HEIGHT, GRID_COLOR)
for iy=0,VERT_CELLS do
local x = GRID_OFFSET_X - 1
local w = GRID_WIDTH + 2
local y = GRID_OFFSET_Y + iy*CELL_SIZE
if iy == VERT_CELLS then y = y - 1 end
riv_draw_rect_fill(x+1, y, w-2, 1, GAP_COLOR)
end
for ix=0,HORZ_CELLS do
local x = GRID_OFFSET_X + ix*CELL_SIZE
local y = GRID_OFFSET_Y - 1
local h = GRID_HEIGHT + 2
if ix == HORZ_CELLS then x = x - 1 end
riv_draw_rect_fill(x, y+1, 1, h-2, GAP_COLOR)
end
-- riv_draw_rect_line(GRID_OFFSET_X-1, GRID_OFFSET_Y-1, GRID_WIDTH+2, GRID_HEIGHT+2, GRID_COLOR)
end
function Board:draw()
draw_grid()
for iy=0,VERT_CELLS-1 do
for ix=0,HORZ_CELLS-1 do
local x = GRID_OFFSET_X + ix*CELL_SIZE
local y = GRID_OFFSET_Y + iy*CELL_SIZE
Piece.draw_cell(x, y, self.cells[iy][ix], false)
end
end
end
function Board:draw_piece(piece: *Piece, shallow: boolean)
local x = GRID_OFFSET_X + piece.x*CELL_SIZE
local y = GRID_OFFSET_Y + piece.y*CELL_SIZE
piece:draw(x, y, shallow)
end
--------------------------------------------------------------------------------
-- Timer
local fame_time: float32
local Timer = @record {
start_time: number
}
function Timer:elapsed(): number
return fame_time - self.start_time
end
function Timer:restart()
self.start_time = fame_time
end
function Timer.update_frame()
fame_time = riv.time
end
--------------------------------------------------------------------------------
-- Game
local Game = @record {
board: Board,
cur_piece: Piece,
next_piece: Piece,
preview_piece: Piece,
xmove_timer: Timer,
ymove_timer: Timer,
slide_vert_timer: Timer,
score: integer,
lines: integer,
level: integer,
hit_sound: riv_waveform_desc,
lineclear_sound: riv_waveform_desc,
levelup_sound: riv_waveform_desc
}
function Game:load_assets()
end
function Game:destroy_assets()
end
function Game:slide_piece_waydown(piece: *Piece)
for y=1,VERT_CELLS do
piece.y = piece.y + 1
if self.board:piece_collides(piece) then
break
end
end
piece.y = piece.y - 1
end
function Game:update_preview_piece()
self.preview_piece = self.cur_piece
self:slide_piece_waydown(self.preview_piece)
end
function Game:spawn_piece()
local piece = self.next_piece
if self.board:piece_collides(piece) then
return false
end
self.cur_piece = piece
self.next_piece = Piece.random_piece()
self:update_preview_piece()
return true
end
function Game:new_game()
self.board.cells = {}
self.score = 0
self.lines = 0
self.level = 1
self.slide_vert_timer:restart()
self.xmove_timer:restart()
self.ymove_timer:restart()
self.next_piece = Piece.random_piece()
self.cur_piece = Piece.random_piece()
self:update_preview_piece()
end
function Game:clear_lines()
local num_lines = self.board:clear_lines()
local points = 0
if num_lines == 4 then
points = 1200
elseif num_lines == 3 then
points = 300
elseif num_lines == 2 then
points = 100
elseif num_lines == 1 then
points = 40
end
if points > 0 then
riv_waveform(riv_waveform_desc{
type = RIV_WAVEFORM_NOISE,
attack = 0.1,
decay = 0.05,
sustain = 0.1,
release = 0.05,
start_frequency = 220,
end_frequency = 40,
amplitude = 0.25,
sustain_level = 0.8,
})
self.lines = self.lines + num_lines
local level = 1 + (self.lines // 10)
if self.level ~= level then
riv_waveform(riv_waveform_desc{
type = RIV_WAVEFORM_PULSE,
attack = 0.015,
decay = 0.05,
sustain = 0.4,
release = 0.8,
start_frequency = 80,
end_frequency = 440,
amplitude = 0.25,
sustain_level = 0.5,
})
self.level = level
end
self.score = self.score + points
end
end
function Game:slide_current_piece(xoff: integer, yoff: integer)
local piece = self.cur_piece
piece:translate(xoff, yoff)
if self.board:piece_collides(piece) then
return false
end
self.cur_piece = piece
self:update_preview_piece()
return true
end
function Game:fit_piece(piece: *Piece): boolean
if not self.board:piece_collides(piece) then
return true
end
local newpiece = $piece
for ix=1,2 do
newpiece.x = piece.x + ix
if not self.board:piece_collides(newpiece) then
piece.x = newpiece.x
return true
end
newpiece.x = piece.x - ix
if not self.board:piece_collides(newpiece) then
piece.x = newpiece.x
return true
end
end
newpiece = $piece
for iy=1,3 do
newpiece.y = piece.y - iy
if not self.board:piece_collides(newpiece) then
piece.y = newpiece.y
return true
end
end
return false
end
function Game:rotate_current_piece(left: boolean): boolean
local piece = self.cur_piece
if left then
piece:rotate_left()
else
piece:rotate_right()
end
if not self:fit_piece(piece) then
return false
end
self.cur_piece = piece
self:update_preview_piece()
return true
end
function Game:place_current_piece()
riv_waveform(riv_waveform_desc{
type = RIV_WAVEFORM_SINE,
attack = 0.01,
decay = 0.04,
sustain = 0.05,
release = 0.05,
start_frequency = 440,
end_frequency = 600,
amplitude = 0.25,
sustain_level = 0.5,
})
self.board:place_piece(self.cur_piece)
self:clear_lines()
if not self:spawn_piece() then
riv.quit = true
end
end
function Game:update()
local XMOVE_COOLDOWN = 0.08
local YMOVE_COOLDOWN = 0.05
local INTERVAL_DECAY = 0.03
local slide_interval = 0.5 - (self.level - 1)*INTERVAL_DECAY
-- check slide to the left/right
local colided = false
if self.xmove_timer:elapsed() >= XMOVE_COOLDOWN then
if riv.keys[RIV_GAMEPAD_LEFT].down then
self.xmove_timer:restart()
self:slide_current_piece(-1, 0)
elseif riv.keys[RIV_GAMEPAD_RIGHT].down then
self.xmove_timer:restart()
self:slide_current_piece(1, 0)
end
end
if self.ymove_timer:elapsed() >= YMOVE_COOLDOWN then
if riv.keys[RIV_GAMEPAD_DOWN].down then
self.ymove_timer:restart()
self.slide_vert_timer:restart()
if not self:slide_current_piece(0, 1) then
self:place_current_piece()
end
end
end
if riv.keys[RIV_GAMEPAD_UP].press then
self:rotate_current_piece(false)
elseif riv.keys[RIV_GAMEPAD_A1].press then
self:rotate_current_piece(true)
elseif riv.keys[RIV_GAMEPAD_A2].press then
self.slide_vert_timer:restart()
self:slide_piece_waydown(self.cur_piece)
self:place_current_piece()
end
-- slide down automatically
if self.slide_vert_timer:elapsed() >= slide_interval then
self.slide_vert_timer:restart()
if not self:slide_current_piece(0, 1) then
self:place_current_piece()
end
end
-- save score
local finished_str: string = riv.quit and 'true' or 'false'
riv.outcard_len = riv_snprintf(&riv.outcard[0], RIV_SIZE_OUTCARD,
[[JSON{"score":%d,"level":%d,"lines":%d,"frames":%u,"finished":%s}]],
self.score, self.level, self.lines, riv.frame, finished_str)
end
local function draw_centered_text(text: cstring, rect: riv_recti, fontsize: integer, fgcolor: Color)
riv_draw_text(text, RIV_SPRITESHEET_FONT_5X7, RIV_CENTER, rect.x + rect.width//2, rect.y + rect.height//2, fontsize, fgcolor)
end
local function draw_titled_number(title: string, num: integer, rect: riv_recti)
local text: cstring = riv_tprintf("%ld", num)
riv_draw_rect_fill(rect.x, rect.y, rect.width, rect.height, RIV_COLOR_DARKSLATE)
riv_draw_rect_line(rect.x, rect.y, rect.width, rect.height, RIV_COLOR_SLATE)
draw_centered_text(text, rect, 1, RIV_COLOR_WHITE)
local titlerect: riv_recti = rect
titlerect.y = titlerect.y - 16
draw_centered_text(title, titlerect, 1, RIV_COLOR_GOLD)
end
function Game:draw_score()
draw_titled_number('Score', self.score, {8, 16, 86, 16})
draw_titled_number('Level', self.level, {8, 48, 86, 16})
draw_titled_number('Lines', self.lines, {8, 80, 86, 16})
end
function Game:draw_next_piece()
local rect: riv_recti = {8, 112, 86, 48}
local titlerect: riv_recti = rect
titlerect.y = titlerect.y - 32
riv_draw_rect_fill(rect.x, rect.y, rect.width, rect.height, RIV_COLOR_DARKSLATE)
riv_draw_rect_line(rect.x, rect.y, rect.width, rect.height, RIV_COLOR_SLATE)
draw_centered_text('Next', titlerect, 1, RIV_COLOR_GOLD)
local x = rect.x + (rect.width - self.next_piece.width*CELL_SIZE) // 2
local y = rect.y + (rect.height - self.next_piece.height*CELL_SIZE) // 2
self.next_piece:draw(x, y, false)
end
function Game:draw_piece()
self.board:draw_piece(self.cur_piece, false)
self.board:draw_piece(self.preview_piece, true)
end
function Game:draw()
riv_clear(RIV_COLOR_DARKSLATE)
self:draw_score()
self.board:draw()
self:draw_next_piece()
self:draw_piece()
end
--------------------------------------------------------------------------------
-- Main
-- Initialize game
local game: Game
game:load_assets()
game:new_game()
local function frame()
Timer.update_frame()
game:update()
game:draw()
riv_present()
end
repeat
frame()
until riv.quit
-- Cleanup
game:destroy_assets()