From 4174bfc21c60ec5e79c77d49863d36608d32858b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 15 Oct 2019 23:17:47 -0400 Subject: [PATCH] Add piece holding --- Tetris/ContentView.swift | 22 ++++++++++-- TetrisKit/GameController.swift | 34 ++++++++++++++----- TetrisKit/GamePiece.swift | 4 +-- TetrisUI/GridView.swift | 4 +-- TetrisUI/TetrominoView.swift | 61 ++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 15 deletions(-) create mode 100644 TetrisUI/TetrominoView.swift diff --git a/Tetris/ContentView.swift b/Tetris/ContentView.swift index defbd56..8ab759a 100644 --- a/Tetris/ContentView.swift +++ b/Tetris/ContentView.swift @@ -22,8 +22,20 @@ struct ContentView: View { var body: some View { GeometryReader { (geometry) in VStack { + HStack { + VStack(spacing: 0) { + Text("Held") + if self.controller.heldTetromino != nil { + Group { + TetrominoView(tetromino: self.controller.heldTetromino!) + }.frame(width: 50, height: 50, alignment: .center) + } else { + Rectangle().foregroundColor(.clear).frame(width: 50, height: 50) + } + } + } BoardView(board: self.$controller.board, currentPiece: self.$controller.currentPiece, droppedPiece: self.$controller.currentPieceAtDropPoint) - .aspectRatio(CGSize(width: 10, height: 16), contentMode: .fit) + .aspectRatio(CGSize(width: self.controller.width, height: self.controller.height), contentMode: .fit) .onAppear(perform: self.startTimer) .onDisappear(perform: self.stopTimer) .onTapGesture(perform: self.onTap) @@ -32,9 +44,15 @@ struct ContentView: View { // .gesture(horizDragGesture.simultaneously(with: verticalDragGesture)) HStack { + Spacer() + Button(action: self.controller.hold) { + Image(systemName: "arrow.up.square.fill").resizable().frame(width: 50, height: 50) + } + Spacer() Button(action: self.onTap) { Image(systemName: "goforward").resizable().frame(width: 50, height: 50) } + Spacer() } HStack { Button(action: self.controller.left) { @@ -107,7 +125,7 @@ struct ContentView: View { } func onSwipeUp() { - // hold + self.controller.hold() } func onSwipeDown() { diff --git a/TetrisKit/GameController.swift b/TetrisKit/GameController.swift index 21291c6..9e25f47 100644 --- a/TetrisKit/GameController.swift +++ b/TetrisKit/GameController.swift @@ -24,6 +24,7 @@ public class GameController: ObservableObject { } } @Published public var currentPieceAtDropPoint: GamePiece? + @Published public var heldTetromino: Tetromino? public var ended: Bool { return (0..: View where Cell: View { var body: some View { GeometryReader { (geometry) in VStack(spacing: 0) { - ForEach(0.. Color { + if row < tetromino.shape.count && col < tetromino.shape[row].count && tetromino.shape[row][col] { + return tetromino.color + } else { + return .clear + } + } +} + +struct TetrominoView_Previews: PreviewProvider { + static var previews: some View { + Group { + TetrominoView(tetromino: .t) + TetrominoView(tetromino: .i) + TetrominoView(tetromino: .z) + TetrominoView(tetromino: .j) + } + } +}