Show upcoming tetrominoes
This commit is contained in:
parent
ea339380e9
commit
496f061059
|
@ -22,25 +22,35 @@ struct ContentView: View {
|
||||||
var body: some View {
|
var body: some View {
|
||||||
GeometryReader { (geometry) in
|
GeometryReader { (geometry) in
|
||||||
VStack {
|
VStack {
|
||||||
HStack {
|
Spacer()
|
||||||
Text("Held:")
|
HStack(alignment: .top, spacing: 8) {
|
||||||
if self.controller.heldTetromino != nil {
|
VStack {
|
||||||
Group {
|
Text("Held")
|
||||||
TetrominoView(tetromino: self.controller.heldTetromino!)
|
if self.controller.heldTetromino != nil {
|
||||||
}.frame(width: 50, height: 50, alignment: .center)
|
Group {
|
||||||
} else {
|
TetrominoView(tetromino: self.controller.heldTetromino!)
|
||||||
Rectangle().foregroundColor(.clear).frame(width: 50, height: 50)
|
}.frame(width: 50, height: 50, alignment: .center)
|
||||||
|
} else {
|
||||||
|
Rectangle().foregroundColor(.clear).frame(width: 50, height: 50)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
.padding(.leading, 8)
|
||||||
|
BoardView(board: self.$controller.board, currentPiece: self.$controller.currentPiece, droppedPiece: self.$controller.currentPieceAtDropPoint)
|
||||||
|
.aspectRatio(CGSize(width: self.controller.width, height: self.controller.height), contentMode: .fit)
|
||||||
|
.onAppear(perform: self.startTimer)
|
||||||
|
.onDisappear(perform: self.stopTimer)
|
||||||
|
.onTapGesture(perform: self.onTap)
|
||||||
|
.gesture(self.horizDragGesture(geometry: geometry).simultaneously(with: self.verticalDragGesture))
|
||||||
|
// .gesture(ExclusiveGesture(horizDragGesture, verticalDragGesture))
|
||||||
|
// .gesture(horizDragGesture.simultaneously(with: verticalDragGesture))
|
||||||
|
|
||||||
|
VStack {
|
||||||
|
Text("Next")
|
||||||
|
NextTetromioesView(tetrominoes: self.controller.nextTetrominoes).frame(width: 50)
|
||||||
|
}
|
||||||
|
.padding(.trailing, 8)
|
||||||
}
|
}
|
||||||
BoardView(board: self.$controller.board, currentPiece: self.$controller.currentPiece, droppedPiece: self.$controller.currentPieceAtDropPoint)
|
Spacer()
|
||||||
.aspectRatio(CGSize(width: self.controller.width, height: self.controller.height), contentMode: .fit)
|
|
||||||
.onAppear(perform: self.startTimer)
|
|
||||||
.onDisappear(perform: self.stopTimer)
|
|
||||||
.onTapGesture(perform: self.onTap)
|
|
||||||
.gesture(self.horizDragGesture(geometry: geometry).simultaneously(with: self.verticalDragGesture))
|
|
||||||
// .gesture(ExclusiveGesture(horizDragGesture, verticalDragGesture))
|
|
||||||
// .gesture(horizDragGesture.simultaneously(with: verticalDragGesture))
|
|
||||||
|
|
||||||
HStack {
|
HStack {
|
||||||
Button(action: { self.controller.rotate(direction: .counterClockwise) }) {
|
Button(action: { self.controller.rotate(direction: .counterClockwise) }) {
|
||||||
Image(systemName: "gobackward").resizable().frame(width: 50, height: 50)
|
Image(systemName: "gobackward").resizable().frame(width: 50, height: 50)
|
||||||
|
@ -55,6 +65,7 @@ struct ContentView: View {
|
||||||
.frame(width: 150, height: 150)
|
.frame(width: 150, height: 150)
|
||||||
.padding(.trailing, 25)
|
.padding(.trailing, 25)
|
||||||
}
|
}
|
||||||
|
Spacer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ public class GameController: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Published public var currentPieceAtDropPoint: GamePiece?
|
@Published public var currentPieceAtDropPoint: GamePiece?
|
||||||
|
@Published public var nextTetrominoes: [Tetromino] = [.random(), .random(), .random()]
|
||||||
@Published public var heldTetromino: Tetromino?
|
@Published public var heldTetromino: Tetromino?
|
||||||
|
|
||||||
public var ended: Bool {
|
public var ended: Bool {
|
||||||
|
@ -41,8 +42,9 @@ public class GameController: ObservableObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
func nextPiece() {
|
func nextPiece() {
|
||||||
let tetromino = Tetromino.random()
|
let tetromino = nextTetrominoes.removeFirst()
|
||||||
currentPiece = GamePiece(tetromino: tetromino, topLeft: ((width - tetromino.shape.count) / 2, 0))
|
currentPiece = GamePiece(tetromino: tetromino, topLeft: ((width - tetromino.shape.count) / 2, 0))
|
||||||
|
nextTetrominoes.append(.random())
|
||||||
}
|
}
|
||||||
|
|
||||||
func finalizePiece() {
|
func finalizePiece() {
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
//
|
||||||
|
// NextTetrominoesView.swift
|
||||||
|
// Tetris
|
||||||
|
//
|
||||||
|
// Created by Shadowfacts on 10/16/19.
|
||||||
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
import TetrisKit
|
||||||
|
|
||||||
|
public struct NextTetromioesView: View {
|
||||||
|
let tetrominoes: [Tetromino]
|
||||||
|
|
||||||
|
public init(tetrominoes: [Tetromino]) {
|
||||||
|
self.tetrominoes = tetrominoes
|
||||||
|
}
|
||||||
|
|
||||||
|
public var body: some View {
|
||||||
|
VStack {
|
||||||
|
ForEach(0..<self.tetrominoes.count, id: \.self) { (index) in
|
||||||
|
TetrominoView(tetromino: self.tetrominoes[index]).aspectRatio(1, contentMode: .fit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct NextTetrominoesView_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
NextTetromioesView(tetrominoes: [.t, .l, .z])
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue