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 {
|
2019-10-21 03:52:15 +00:00
|
|
|
let size: CGFloat
|
2019-10-16 03:17:47 +00:00
|
|
|
let tetromino: Tetromino
|
2019-10-21 03:52:15 +00:00
|
|
|
let color: Color
|
|
|
|
let border: Bool
|
2019-10-16 03:17:47 +00:00
|
|
|
|
2019-10-21 03:52:15 +00:00
|
|
|
public init(size: CGFloat, tetromino: Tetromino, color: Color? = nil, border: Bool = true) {
|
|
|
|
self.size = size
|
2019-10-16 03:17:47 +00:00
|
|
|
self.tetromino = tetromino
|
2019-10-21 03:52:15 +00:00
|
|
|
self.color = color ?? tetromino.color
|
|
|
|
self.border = border
|
2019-10-16 03:17:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var body: some View {
|
2019-10-21 03:52:15 +00:00
|
|
|
GridView(rows: tetromino.shape.count, columns: tetromino.shape.first!.count) { (col, row, _) 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] {
|
2019-10-21 03:52:15 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-10-20 23:21:04 +00:00
|
|
|
} else {
|
|
|
|
Rectangle()
|
|
|
|
.foregroundColor(.clear)
|
2019-10-21 03:52:15 +00:00
|
|
|
.frame(width: self.size, height: self.size)
|
2019-10-20 23:21:04 +00:00
|
|
|
}
|
2019-10-16 03:17:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TetrominoView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
2019-10-21 03:52:15 +00:00
|
|
|
TetrominoView(size: 50, tetromino: .t)
|
|
|
|
TetrominoView(size: 50, tetromino: .i)
|
|
|
|
TetrominoView(size: 50, tetromino: .z)
|
|
|
|
TetrominoView(size: 50, tetromino: .j)
|
2019-10-16 03:17:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|