Add pause

This commit is contained in:
Shadowfacts 2019-10-20 10:23:32 -04:00
parent 0dd8bd79cb
commit f1f0a49023
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 15 additions and 1 deletions

View File

@ -42,6 +42,8 @@ class TetrisHostingView: NSHostingView<ContentView> {
gameController.rotate(direction: .clockwise)
case kVK_DownArrow:
gameController.drop()
case kVK_Space:
gameController.togglePause()
default:
super.keyDown(with: event)
}

View File

@ -14,6 +14,7 @@ public class GameController: ObservableObject {
public let width = 10
public let height = 20
var paused = false
public var state: GameState = .waitingForStart
@Published public var board: GameBoard
@ -111,6 +112,7 @@ public class GameController: ObservableObject {
public func step() {
guard case .playing(_) = state,
!paused,
let currentPiece = currentPiece else { return }
let modifiedPiece = currentPiece.moved(by: (0, 1))
@ -123,6 +125,7 @@ public class GameController: ObservableObject {
public func rotate(direction: RotationDirection) {
guard case let .playing(playState) = state,
!paused,
playState != .dropped,
let currentPiece = currentPiece else { return }
@ -134,6 +137,7 @@ public class GameController: ObservableObject {
public func left() {
guard case let .playing(playState) = state,
!paused,
playState != .dropped,
let currentPiece = currentPiece else { return }
let modifiedPiece = currentPiece.moved(by: (-1, 0))
@ -145,6 +149,7 @@ public class GameController: ObservableObject {
public func right() {
guard case let .playing(playState) = state,
playState != .dropped,
!paused,
let currentPiece = currentPiece else { return }
let modifiedPiece = currentPiece.moved(by: (1, 0))
if !overlapsAny(modifiedPiece) {
@ -153,13 +158,16 @@ public class GameController: ObservableObject {
}
public func drop() {
guard case let .playing(playState) = state, playState != .dropped else { return }
guard case let .playing(playState) = state,
!paused,
playState != .dropped else { return }
currentPiece = currentPieceAtDropPoint
state = .playing(.dropped)
}
public func hold() {
guard case .playing(.normal) = state,
// !paused,
let currentPiece = currentPiece else { return }
if let heldTetromino = heldTetromino {
self.heldTetromino = currentPiece.tetromino
@ -171,6 +179,10 @@ public class GameController: ObservableObject {
state = .playing(.switched)
}
public func togglePause() {
paused = !paused
}
private func updateCurrentPieceAtDropPoint() {
guard let currentPiece = currentPiece else { return }
var prev = currentPiece