Don't allow pieces to be moved after dropping
This commit is contained in:
parent
dec8131898
commit
2c3e4c9541
|
@ -14,6 +14,8 @@ public class GameController: ObservableObject {
|
|||
public let width = 10
|
||||
public let height = 16
|
||||
|
||||
var state: GameState = .waitingForStart
|
||||
|
||||
@Published public var board: GameBoard
|
||||
|
||||
@Published public var currentPiece: GamePiece? {
|
||||
|
@ -33,6 +35,7 @@ public class GameController: ObservableObject {
|
|||
}
|
||||
|
||||
public func start() {
|
||||
state = .playing(.normal)
|
||||
nextPiece()
|
||||
}
|
||||
|
||||
|
@ -45,6 +48,10 @@ public class GameController: ObservableObject {
|
|||
self.board.set(piece: currentPiece!)
|
||||
clearLines()
|
||||
nextPiece()
|
||||
|
||||
if case .playing(.dropped) = state {
|
||||
state = .playing(.normal)
|
||||
}
|
||||
}
|
||||
|
||||
func clearLines() {
|
||||
|
@ -61,7 +68,9 @@ public class GameController: ObservableObject {
|
|||
}
|
||||
|
||||
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))
|
||||
if overlapsAny(modifiedPiece) {
|
||||
finalizePiece()
|
||||
|
@ -71,7 +80,9 @@ public class GameController: ObservableObject {
|
|||
}
|
||||
|
||||
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)
|
||||
if !overlapsAny(modifiedPiece) {
|
||||
self.currentPiece = modifiedPiece
|
||||
|
@ -79,7 +90,8 @@ public class GameController: ObservableObject {
|
|||
}
|
||||
|
||||
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))
|
||||
if !overlapsAny(modifiedPiece) {
|
||||
self.currentPiece = modifiedPiece
|
||||
|
@ -87,7 +99,8 @@ public class GameController: ObservableObject {
|
|||
}
|
||||
|
||||
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))
|
||||
if !overlapsAny(modifiedPiece) {
|
||||
self.currentPiece = modifiedPiece
|
||||
|
@ -95,10 +108,12 @@ public class GameController: ObservableObject {
|
|||
}
|
||||
|
||||
public func drop() {
|
||||
guard case .playing(.normal) = state else { return }
|
||||
currentPiece = currentPieceAtDropPoint
|
||||
state = .playing(.dropped)
|
||||
}
|
||||
|
||||
func updateCurrentPieceAtDropPoint() {
|
||||
private func updateCurrentPieceAtDropPoint() {
|
||||
guard let currentPiece = currentPiece else { return }
|
||||
var prev = currentPiece
|
||||
currentPieceAtDropPoint = currentPiece
|
||||
|
@ -122,3 +137,13 @@ public class GameController: ObservableObject {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
enum GameState {
|
||||
case waitingForStart
|
||||
case playing(PlayState)
|
||||
}
|
||||
|
||||
enum PlayState {
|
||||
case normal
|
||||
case dropped
|
||||
}
|
||||
|
|
|
@ -82,7 +82,7 @@ extension TwoDimString {
|
|||
setColumn(controller.width + 1, to: "+" + String(repeating: "|", count: 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"
|
||||
}
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ extension TwoDimString {
|
|||
}
|
||||
|
||||
let controller = GameController()
|
||||
controller.currentPiece = GamePiece(tetromino: .l)
|
||||
controller.start()
|
||||
|
||||
func readMove() {
|
||||
print(TwoDimString(controller: controller))
|
||||
|
|
Loading…
Reference in New Issue