Tetris/TetrisUI/BoardView.swift

50 lines
1.6 KiB
Swift
Raw Normal View History

2019-10-15 16:24:58 +00:00
//
// BoardView.swift
// Tetris
//
// Created by Shadowfacts on 10/14/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import SwiftUI
import TetrisKit
public struct BoardView: View {
@Binding var board: GameBoard
@Binding var currentPiece: GamePiece?
@Binding var droppedPiece: GamePiece?
public init(board: Binding<GameBoard>, currentPiece: Binding<GamePiece?>, droppedPiece: Binding<GamePiece?>) {
self._board = board
self._currentPiece = currentPiece
self._droppedPiece = droppedPiece
}
public var body: some View {
GeometryReader { (geometry) in
ZStack(alignment: .topLeading) {
TilesView(board: self.$board)
if self.currentPiece != nil {
GamePieceView(size: min(geometry.size.width / CGFloat(self.board.width), geometry.size.height / CGFloat(self.board.height)), piece: self.currentPiece!)
}
if self.droppedPiece != nil {
GamePieceView(size: min(geometry.size.width / CGFloat(self.board.width), geometry.size.height / CGFloat(self.board.height)), piece: self.droppedPiece!, color: .gray, border: false)
}
}
2019-10-15 16:24:58 +00:00
}
}
}
struct BoardView_Previews: PreviewProvider {
@ObservedObject static var controller: GameController = {
var c = GameController()
c.currentPiece = GamePiece(tetromino: .t)
return c
}()
static var previews: some View {
BoardView(board: $controller.board, currentPiece: $controller.currentPiece, droppedPiece: $controller.currentPieceAtDropPoint)
}
}