Don't allow pieces to be moved after dropping

This commit is contained in:
Shadowfacts 2019-10-15 18:56:53 -04:00
parent dec8131898
commit 2c3e4c9541
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 32 additions and 7 deletions

View File

@ -14,6 +14,8 @@ public class GameController: ObservableObject {
public let width = 10 public let width = 10
public let height = 16 public let height = 16
var state: GameState = .waitingForStart
@Published public var board: GameBoard @Published public var board: GameBoard
@Published public var currentPiece: GamePiece? { @Published public var currentPiece: GamePiece? {
@ -33,6 +35,7 @@ public class GameController: ObservableObject {
} }
public func start() { public func start() {
state = .playing(.normal)
nextPiece() nextPiece()
} }
@ -45,6 +48,10 @@ public class GameController: ObservableObject {
self.board.set(piece: currentPiece!) self.board.set(piece: currentPiece!)
clearLines() clearLines()
nextPiece() nextPiece()
if case .playing(.dropped) = state {
state = .playing(.normal)
}
} }
func clearLines() { func clearLines() {
@ -61,7 +68,9 @@ public class GameController: ObservableObject {
} }
public func step() { public func step() {
guard let currentPiece = currentPiece else { return } guard case .playing(_) = state,
let currentPiece = currentPiece else { return }
let modifiedPiece = currentPiece.moved(by: (0, 1)) let modifiedPiece = currentPiece.moved(by: (0, 1))
if overlapsAny(modifiedPiece) { if overlapsAny(modifiedPiece) {
finalizePiece() finalizePiece()
@ -71,7 +80,9 @@ public class GameController: ObservableObject {
} }
public func rotate(direction: RotationDirection) { public func rotate(direction: RotationDirection) {
guard let currentPiece = currentPiece else { return } guard case .playing(_) = state,
let currentPiece = currentPiece else { return }
let modifiedPiece = currentPiece.rotated(direction) let modifiedPiece = currentPiece.rotated(direction)
if !overlapsAny(modifiedPiece) { if !overlapsAny(modifiedPiece) {
self.currentPiece = modifiedPiece self.currentPiece = modifiedPiece
@ -79,7 +90,8 @@ public class GameController: ObservableObject {
} }
public func left() { public func left() {
guard let currentPiece = currentPiece else { return } guard case .playing(.normal) = state,
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) {
self.currentPiece = modifiedPiece self.currentPiece = modifiedPiece
@ -87,7 +99,8 @@ public class GameController: ObservableObject {
} }
public func right() { public func right() {
guard let currentPiece = currentPiece else { return } guard case .playing(.normal) = state,
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) {
self.currentPiece = modifiedPiece self.currentPiece = modifiedPiece
@ -95,10 +108,12 @@ public class GameController: ObservableObject {
} }
public func drop() { public func drop() {
guard case .playing(.normal) = state else { return }
currentPiece = currentPieceAtDropPoint currentPiece = currentPieceAtDropPoint
state = .playing(.dropped)
} }
func updateCurrentPieceAtDropPoint() { private func updateCurrentPieceAtDropPoint() {
guard let currentPiece = currentPiece else { return } guard let currentPiece = currentPiece else { return }
var prev = currentPiece var prev = currentPiece
currentPieceAtDropPoint = currentPiece currentPieceAtDropPoint = currentPiece
@ -122,3 +137,13 @@ public class GameController: ObservableObject {
} }
} }
enum GameState {
case waitingForStart
case playing(PlayState)
}
enum PlayState {
case normal
case dropped
}

View File

@ -82,7 +82,7 @@ extension TwoDimString {
setColumn(controller.width + 1, to: "+" + String(repeating: "|", count: controller.height) + "+") setColumn(controller.width + 1, to: "+" + String(repeating: "|", count: controller.height) + "+")
for y in 0..<controller.height { for y in 0..<controller.height {
for x in 0..<controller.width where controller.board.get(tile: (x, y)) { for x in 0..<controller.width where controller.board[x, y] {
self[x + 1, y + 1] = "X" self[x + 1, y + 1] = "X"
} }
} }
@ -99,7 +99,7 @@ extension TwoDimString {
} }
let controller = GameController() let controller = GameController()
controller.currentPiece = GamePiece(tetromino: .l) controller.start()
func readMove() { func readMove() {
print(TwoDimString(controller: controller)) print(TwoDimString(controller: controller))