Tetris/TetrisUI/TetrominoView.swift

61 lines
1.6 KiB
Swift
Raw Normal View History

2019-10-16 03:17:47 +00:00
//
// TetrominoView.swift
// Tetris
//
// Created by Shadowfacts on 10/15/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import SwiftUI
import TetrisKit
public struct TetrominoView: View {
let tetromino: Tetromino
public init(tetromino: Tetromino) {
self.tetromino = tetromino
}
public var body: some View {
GridView(rows: self.rows, columns: self.columns) { (col, row, size) in
2019-10-20 23:21:04 +00:00
if row < self.tetromino.shape.count && col < self.tetromino.shape[row].count && self.tetromino.shape[row][col] {
Rectangle()
.foregroundColor(self.tetromino.color)
.frame(width: size, height: size)
.border(self.tetromino.borderColor, width: 6)
} else {
Rectangle()
.foregroundColor(.clear)
.frame(width: size, height: size)
}
2019-10-16 03:17:47 +00:00
}
}
var rows: Int {
self.tetromino.shape.firstIndex(where: { row in
row.allSatisfy({ el in
!el
})
}) ?? self.tetromino.shape.count
}
var columns: Int {
self.tetromino.shape.map { row in
(row.firstIndex(where: { el in
!el
}) ?? row.count - 1) + 1
}.max() ?? self.tetromino.shape.first!.count
}
}
struct TetrominoView_Previews: PreviewProvider {
static var previews: some View {
Group {
TetrominoView(tetromino: .t)
TetrominoView(tetromino: .i)
TetrominoView(tetromino: .z)
TetrominoView(tetromino: .j)
}
}
}