|
| 1 | +require 'config' |
| 2 | +require 'raylib' |
| 3 | +require 'piece' |
| 4 | + |
| 5 | +global Board = @record{ |
| 6 | + cells: Color[HORZ_CELLS][VERT_CELLS] |
| 7 | +} |
| 8 | + |
| 9 | +function Board:piece_collides(piece: Piece*) |
| 10 | + for iy=0,piece.size-1 do |
| 11 | + for ix=0,piece.size-1 do |
| 12 | + local tx, ty = piece.x+ix, piece.y+iy |
| 13 | + if piece.layout[iy][ix] ~= 0 then |
| 14 | + if tx < 0 or tx >= HORZ_CELLS or ty < 0 or ty >= VERT_CELLS then |
| 15 | + return true |
| 16 | + end |
| 17 | + if self.cells[ty][tx].a ~= 0 then |
| 18 | + return true |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + end |
| 23 | + return false |
| 24 | +end |
| 25 | + |
| 26 | +function Board:place_piece(piece: Piece*) |
| 27 | + for iy=0,piece.size-1 do |
| 28 | + for ix=0,piece.size-1 do |
| 29 | + local tx, ty = piece.x+ix, piece.y+iy |
| 30 | + if piece.layout[iy][ix] ~= 0 then |
| 31 | + self.cells[ty][tx] = piece.color |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +function Board:clear_lines() |
| 38 | + local num_clears = 0 |
| 39 | + for y=0,VERT_CELLS-1 do |
| 40 | + -- the if line y is full |
| 41 | + local full = true |
| 42 | + for x=0,HORZ_CELLS-1 do |
| 43 | + if self.cells[y][x].a == 0 then |
| 44 | + full = false |
| 45 | + break |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + -- slide lines down |
| 50 | + if full then |
| 51 | + num_clears = num_clears + 1 |
| 52 | + for ny=y,1,-1 do |
| 53 | + for x=0,HORZ_CELLS-1 do |
| 54 | + self.cells[ny][x] = self.cells[ny-1][x] |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + return num_clears |
| 60 | +end |
| 61 | + |
| 62 | +local function draw_grid() |
| 63 | + local GRID_COLOR: Color <const> = {r=0x11,g=0x11,b=0x11,a=0xff} |
| 64 | + local GAP_COLOR: Color <const> = DARKGRAY |
| 65 | + local GAP_COLOR2: Color <const> = Raylib.Fade(DARKGRAY, 0.2) |
| 66 | + |
| 67 | + Raylib.DrawRectangle(GRID_OFFSET_X, GRID_OFFSET_Y, |
| 68 | + GRID_WIDTH, GRID_HEIGHT, |
| 69 | + GRID_COLOR) |
| 70 | + for iy=0,VERT_CELLS do |
| 71 | + local x = GRID_OFFSET_X - 1 |
| 72 | + local w = GRID_WIDTH + 2 |
| 73 | + local y = GRID_OFFSET_Y + iy*CELL_SIZE |
| 74 | + if iy == VERT_CELLS then y = y - 1 end |
| 75 | + Raylib.DrawRectangle(x, y-1, w, 3, GAP_COLOR2) |
| 76 | + Raylib.DrawRectangle(x, y, w, 1, GAP_COLOR) |
| 77 | + end |
| 78 | + for ix=0,HORZ_CELLS do |
| 79 | + local x = GRID_OFFSET_X + ix*CELL_SIZE |
| 80 | + local y = GRID_OFFSET_Y - 1 |
| 81 | + local h = GRID_HEIGHT + 2 |
| 82 | + if ix == HORZ_CELLS then x = x - 1 end |
| 83 | + Raylib.DrawRectangle(x-1, y, 3, h, GAP_COLOR2) |
| 84 | + Raylib.DrawRectangle(x, y, 1, h, GAP_COLOR) |
| 85 | + end |
| 86 | + Raylib.DrawRectangleLines(GRID_OFFSET_X-1, GRID_OFFSET_Y-1, |
| 87 | + GRID_WIDTH+2, GRID_HEIGHT+2, |
| 88 | + GRID_COLOR) |
| 89 | +end |
| 90 | + |
| 91 | +function Board:draw() |
| 92 | + draw_grid() |
| 93 | + for iy=0,VERT_CELLS-1 do |
| 94 | + for ix=0,HORZ_CELLS-1 do |
| 95 | + local x = GRID_OFFSET_X + ix*CELL_SIZE |
| 96 | + local y = GRID_OFFSET_Y + iy*CELL_SIZE |
| 97 | + Piece.draw_cell(x, y, self.cells[iy][ix], false) |
| 98 | + end |
| 99 | + end |
| 100 | +end |
| 101 | + |
| 102 | +function Board:draw_piece(piece: Piece*, shallow: boolean) |
| 103 | + local x = GRID_OFFSET_X + piece.x*CELL_SIZE |
| 104 | + local y = GRID_OFFSET_Y + piece.y*CELL_SIZE |
| 105 | + piece:draw(x, y, shallow) |
| 106 | +end |
0 commit comments