57 lines
1.7 KiB
Swift
57 lines
1.7 KiB
Swift
//
|
|
// 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 size: CGFloat
|
|
let tetromino: Tetromino
|
|
let color: Color
|
|
let border: Bool
|
|
|
|
public init(size: CGFloat, tetromino: Tetromino, color: Color? = nil, border: Bool = true) {
|
|
self.size = size
|
|
self.tetromino = tetromino
|
|
self.color = color ?? tetromino.color
|
|
self.border = border
|
|
}
|
|
|
|
public var body: some View {
|
|
GridView(rows: tetromino.shape.count, columns: tetromino.shape.first!.count) { (col, row, _) in
|
|
if row < self.tetromino.shape.count && col < self.tetromino.shape[row].count && self.tetromino.shape[row][col] {
|
|
if self.border {
|
|
Rectangle()
|
|
.foregroundColor(self.color)
|
|
.frame(width: self.size, height: self.size)
|
|
.border(self.tetromino.borderColor, width: 6)
|
|
} else {
|
|
Rectangle()
|
|
.foregroundColor(self.color)
|
|
.frame(width: self.size, height: self.size)
|
|
}
|
|
} else {
|
|
Rectangle()
|
|
.foregroundColor(.clear)
|
|
.frame(width: self.size, height: self.size)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct TetrominoView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
Group {
|
|
TetrominoView(size: 50, tetromino: .t)
|
|
TetrominoView(size: 50, tetromino: .i)
|
|
TetrominoView(size: 50, tetromino: .z)
|
|
TetrominoView(size: 50, tetromino: .j)
|
|
}
|
|
}
|
|
}
|