Add pause
This commit is contained in:
parent
0dd8bd79cb
commit
f1f0a49023
|
@ -42,6 +42,8 @@ class TetrisHostingView: NSHostingView<ContentView> {
|
||||||
gameController.rotate(direction: .clockwise)
|
gameController.rotate(direction: .clockwise)
|
||||||
case kVK_DownArrow:
|
case kVK_DownArrow:
|
||||||
gameController.drop()
|
gameController.drop()
|
||||||
|
case kVK_Space:
|
||||||
|
gameController.togglePause()
|
||||||
default:
|
default:
|
||||||
super.keyDown(with: event)
|
super.keyDown(with: event)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ public class GameController: ObservableObject {
|
||||||
public let width = 10
|
public let width = 10
|
||||||
public let height = 20
|
public let height = 20
|
||||||
|
|
||||||
|
var paused = false
|
||||||
public var state: GameState = .waitingForStart
|
public var state: GameState = .waitingForStart
|
||||||
|
|
||||||
@Published public var board: GameBoard
|
@Published public var board: GameBoard
|
||||||
|
@ -111,6 +112,7 @@ public class GameController: ObservableObject {
|
||||||
|
|
||||||
public func step() {
|
public func step() {
|
||||||
guard case .playing(_) = state,
|
guard case .playing(_) = state,
|
||||||
|
!paused,
|
||||||
let currentPiece = currentPiece else { return }
|
let currentPiece = currentPiece else { return }
|
||||||
|
|
||||||
let modifiedPiece = currentPiece.moved(by: (0, 1))
|
let modifiedPiece = currentPiece.moved(by: (0, 1))
|
||||||
|
@ -123,6 +125,7 @@ public class GameController: ObservableObject {
|
||||||
|
|
||||||
public func rotate(direction: RotationDirection) {
|
public func rotate(direction: RotationDirection) {
|
||||||
guard case let .playing(playState) = state,
|
guard case let .playing(playState) = state,
|
||||||
|
!paused,
|
||||||
playState != .dropped,
|
playState != .dropped,
|
||||||
let currentPiece = currentPiece else { return }
|
let currentPiece = currentPiece else { return }
|
||||||
|
|
||||||
|
@ -134,6 +137,7 @@ public class GameController: ObservableObject {
|
||||||
|
|
||||||
public func left() {
|
public func left() {
|
||||||
guard case let .playing(playState) = state,
|
guard case let .playing(playState) = state,
|
||||||
|
!paused,
|
||||||
playState != .dropped,
|
playState != .dropped,
|
||||||
let currentPiece = currentPiece else { return }
|
let currentPiece = currentPiece else { return }
|
||||||
let modifiedPiece = currentPiece.moved(by: (-1, 0))
|
let modifiedPiece = currentPiece.moved(by: (-1, 0))
|
||||||
|
@ -145,6 +149,7 @@ public class GameController: ObservableObject {
|
||||||
public func right() {
|
public func right() {
|
||||||
guard case let .playing(playState) = state,
|
guard case let .playing(playState) = state,
|
||||||
playState != .dropped,
|
playState != .dropped,
|
||||||
|
!paused,
|
||||||
let currentPiece = currentPiece else { return }
|
let currentPiece = currentPiece else { return }
|
||||||
let modifiedPiece = currentPiece.moved(by: (1, 0))
|
let modifiedPiece = currentPiece.moved(by: (1, 0))
|
||||||
if !overlapsAny(modifiedPiece) {
|
if !overlapsAny(modifiedPiece) {
|
||||||
|
@ -153,13 +158,16 @@ public class GameController: ObservableObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func drop() {
|
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
|
currentPiece = currentPieceAtDropPoint
|
||||||
state = .playing(.dropped)
|
state = .playing(.dropped)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func hold() {
|
public func hold() {
|
||||||
guard case .playing(.normal) = state,
|
guard case .playing(.normal) = state,
|
||||||
|
// !paused,
|
||||||
let currentPiece = currentPiece else { return }
|
let currentPiece = currentPiece else { return }
|
||||||
if let heldTetromino = heldTetromino {
|
if let heldTetromino = heldTetromino {
|
||||||
self.heldTetromino = currentPiece.tetromino
|
self.heldTetromino = currentPiece.tetromino
|
||||||
|
@ -171,6 +179,10 @@ public class GameController: ObservableObject {
|
||||||
state = .playing(.switched)
|
state = .playing(.switched)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func togglePause() {
|
||||||
|
paused = !paused
|
||||||
|
}
|
||||||
|
|
||||||
private func updateCurrentPieceAtDropPoint() {
|
private func updateCurrentPieceAtDropPoint() {
|
||||||
guard let currentPiece = currentPiece else { return }
|
guard let currentPiece = currentPiece else { return }
|
||||||
var prev = currentPiece
|
var prev = currentPiece
|
||||||
|
|
Loading…
Reference in New Issue