End the game when new pieces can't be placed
This commit is contained in:
parent
600768ea5d
commit
0dd8bd79cb
|
@ -60,6 +60,10 @@ struct ContentView: View {
|
||||||
|
|
||||||
func startTimer() {
|
func startTimer() {
|
||||||
self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { (_) in
|
self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { (_) in
|
||||||
|
guard case .playing(_) = self.controller.state else {
|
||||||
|
self.stopTimer()
|
||||||
|
return
|
||||||
|
}
|
||||||
self.controller.step()
|
self.controller.step()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,6 +123,10 @@ struct ContentView: View {
|
||||||
|
|
||||||
func startTimer() {
|
func startTimer() {
|
||||||
self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { (_) in
|
self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { (_) in
|
||||||
|
guard case .playing(_) = self.controller.state else {
|
||||||
|
self.stopTimer()
|
||||||
|
return
|
||||||
|
}
|
||||||
self.controller.step()
|
self.controller.step()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,10 +32,6 @@ public class GameController: ObservableObject {
|
||||||
@Published public var score = 0
|
@Published public var score = 0
|
||||||
var previousPieceWasTetris = false
|
var previousPieceWasTetris = false
|
||||||
|
|
||||||
public var ended: Bool {
|
|
||||||
return (0..<width).first(where: { board[$0, 0] }) != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
public init() {
|
public init() {
|
||||||
self.board = GameBoard(width: width, height: height)
|
self.board = GameBoard(width: width, height: height)
|
||||||
self.currentPiece = nil
|
self.currentPiece = nil
|
||||||
|
@ -48,13 +44,20 @@ public class GameController: ObservableObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
func nextPiece() {
|
func nextPiece() {
|
||||||
let tetromino = nextTetrominoes.removeFirst()
|
let tetromino = nextTetrominoes.first!
|
||||||
currentPiece = GamePiece(tetromino: tetromino, topLeft: ((width - tetromino.shape.count) / 2, 0))
|
let nextPiece = GamePiece(tetromino: tetromino, topLeft: ((width - tetromino.shape.count) / 2, 0))
|
||||||
|
|
||||||
|
if overlapsAny(nextPiece) {
|
||||||
|
state = .ended
|
||||||
|
} else {
|
||||||
|
nextTetrominoes.removeFirst()
|
||||||
if currentBag.isEmpty {
|
if currentBag.isEmpty {
|
||||||
generateBag()
|
generateBag()
|
||||||
}
|
}
|
||||||
nextTetrominoes.append(currentBag.removeFirst())
|
nextTetrominoes.append(currentBag.removeFirst())
|
||||||
|
|
||||||
|
currentPiece = nextPiece
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateBag() {
|
func generateBag() {
|
||||||
|
@ -70,8 +73,10 @@ public class GameController: ObservableObject {
|
||||||
clearLines()
|
clearLines()
|
||||||
nextPiece()
|
nextPiece()
|
||||||
|
|
||||||
|
if case .playing(.dropped) = state {
|
||||||
state = .playing(.normal)
|
state = .playing(.normal)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func clearLines() {
|
func clearLines() {
|
||||||
var cleared = 0
|
var cleared = 0
|
||||||
|
@ -194,6 +199,7 @@ public class GameController: ObservableObject {
|
||||||
public enum GameState {
|
public enum GameState {
|
||||||
case waitingForStart
|
case waitingForStart
|
||||||
case playing(PlayState)
|
case playing(PlayState)
|
||||||
|
case ended
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum PlayState {
|
public enum PlayState {
|
||||||
|
|
Loading…
Reference in New Issue