2019-10-15 16:24:58 +00:00
|
|
|
//
|
|
|
|
// GameController.swift
|
|
|
|
// TetrisKit
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 10/13/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
public class GameController: ObservableObject {
|
|
|
|
|
|
|
|
public let width = 10
|
2019-10-16 18:22:46 +00:00
|
|
|
public let height = 20
|
2019-10-15 16:24:58 +00:00
|
|
|
|
2019-10-16 18:43:41 +00:00
|
|
|
public var state: GameState = .waitingForStart
|
2019-10-15 22:56:53 +00:00
|
|
|
|
2019-10-15 16:24:58 +00:00
|
|
|
@Published public var board: GameBoard
|
|
|
|
|
|
|
|
@Published public var currentPiece: GamePiece? {
|
|
|
|
didSet {
|
|
|
|
updateCurrentPieceAtDropPoint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Published public var currentPieceAtDropPoint: GamePiece?
|
2019-10-17 01:39:30 +00:00
|
|
|
@Published public var nextTetrominoes: [Tetromino] = []
|
2019-10-16 03:17:47 +00:00
|
|
|
@Published public var heldTetromino: Tetromino?
|
2019-10-16 21:49:16 +00:00
|
|
|
|
2019-10-17 01:39:30 +00:00
|
|
|
var currentBag: [Tetromino] = []
|
|
|
|
|
2019-10-16 21:49:16 +00:00
|
|
|
@Published public var score = 0
|
|
|
|
var previousPieceWasTetris = false
|
2019-10-15 16:24:58 +00:00
|
|
|
|
|
|
|
public var ended: Bool {
|
|
|
|
return (0..<width).first(where: { board[$0, 0] }) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
public init() {
|
|
|
|
self.board = GameBoard(width: width, height: height)
|
|
|
|
self.currentPiece = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
public func start() {
|
2019-10-15 22:56:53 +00:00
|
|
|
state = .playing(.normal)
|
2019-10-17 01:39:30 +00:00
|
|
|
generateBag()
|
2019-10-15 16:24:58 +00:00
|
|
|
nextPiece()
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextPiece() {
|
2019-10-16 18:39:18 +00:00
|
|
|
let tetromino = nextTetrominoes.removeFirst()
|
2019-10-16 03:17:47 +00:00
|
|
|
currentPiece = GamePiece(tetromino: tetromino, topLeft: ((width - tetromino.shape.count) / 2, 0))
|
2019-10-17 01:39:30 +00:00
|
|
|
|
|
|
|
if currentBag.isEmpty {
|
|
|
|
generateBag()
|
|
|
|
}
|
|
|
|
nextTetrominoes.append(currentBag.removeFirst())
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateBag() {
|
|
|
|
currentBag = Array(0..<Tetromino.allCases.count).shuffled().map { Tetromino.allCases[$0] }
|
|
|
|
if nextTetrominoes.isEmpty {
|
|
|
|
nextTetrominoes = Array(currentBag[0..<3])
|
|
|
|
currentBag.removeFirst(3)
|
|
|
|
}
|
2019-10-15 16:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func finalizePiece() {
|
|
|
|
self.board.set(piece: currentPiece!)
|
|
|
|
clearLines()
|
|
|
|
nextPiece()
|
2019-10-15 22:56:53 +00:00
|
|
|
|
2019-10-16 03:17:47 +00:00
|
|
|
state = .playing(.normal)
|
2019-10-15 16:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func clearLines() {
|
2019-10-16 21:49:16 +00:00
|
|
|
var cleared = 0
|
2019-10-15 16:24:58 +00:00
|
|
|
var row = height - 1
|
|
|
|
while row >= 0 {
|
2019-10-15 21:52:16 +00:00
|
|
|
if board.rowFull(row) {
|
2019-10-15 16:24:58 +00:00
|
|
|
board.tiles.remove(at: row)
|
2019-10-16 21:49:16 +00:00
|
|
|
cleared += 1
|
2019-10-15 16:24:58 +00:00
|
|
|
}
|
|
|
|
row -= 1
|
|
|
|
}
|
2019-10-16 21:49:16 +00:00
|
|
|
for _ in 0..<cleared {
|
2019-10-15 21:52:16 +00:00
|
|
|
board.tiles.insert(Array(repeating: nil, count: width), at: 0)
|
2019-10-15 16:24:58 +00:00
|
|
|
}
|
2019-10-16 21:49:16 +00:00
|
|
|
|
|
|
|
if cleared == 4 {
|
|
|
|
score += previousPieceWasTetris ? 1200 : 800
|
|
|
|
previousPieceWasTetris = true
|
|
|
|
} else if cleared == 3 {
|
|
|
|
score += 500
|
2019-10-17 01:39:30 +00:00
|
|
|
previousPieceWasTetris = false
|
2019-10-16 21:49:16 +00:00
|
|
|
} else if cleared == 2 {
|
|
|
|
score += 300
|
2019-10-17 01:39:30 +00:00
|
|
|
previousPieceWasTetris = false
|
2019-10-16 21:49:16 +00:00
|
|
|
} else if cleared == 1 {
|
|
|
|
score += 100
|
2019-10-17 01:39:30 +00:00
|
|
|
previousPieceWasTetris = false
|
|
|
|
} else {
|
|
|
|
previousPieceWasTetris = false
|
2019-10-16 21:49:16 +00:00
|
|
|
}
|
2019-10-15 16:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func step() {
|
2019-10-15 22:56:53 +00:00
|
|
|
guard case .playing(_) = state,
|
|
|
|
let currentPiece = currentPiece else { return }
|
|
|
|
|
2019-10-15 16:24:58 +00:00
|
|
|
let modifiedPiece = currentPiece.moved(by: (0, 1))
|
|
|
|
if overlapsAny(modifiedPiece) {
|
|
|
|
finalizePiece()
|
|
|
|
} else {
|
|
|
|
self.currentPiece = modifiedPiece
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func rotate(direction: RotationDirection) {
|
2019-10-16 03:17:47 +00:00
|
|
|
guard case let .playing(playState) = state,
|
|
|
|
playState != .dropped,
|
2019-10-15 22:56:53 +00:00
|
|
|
let currentPiece = currentPiece else { return }
|
|
|
|
|
2019-10-15 16:24:58 +00:00
|
|
|
let modifiedPiece = currentPiece.rotated(direction)
|
|
|
|
if !overlapsAny(modifiedPiece) {
|
|
|
|
self.currentPiece = modifiedPiece
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func left() {
|
2019-10-16 03:17:47 +00:00
|
|
|
guard case let .playing(playState) = state,
|
|
|
|
playState != .dropped,
|
2019-10-15 22:56:53 +00:00
|
|
|
let currentPiece = currentPiece else { return }
|
2019-10-15 16:24:58 +00:00
|
|
|
let modifiedPiece = currentPiece.moved(by: (-1, 0))
|
|
|
|
if !overlapsAny(modifiedPiece) {
|
|
|
|
self.currentPiece = modifiedPiece
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func right() {
|
2019-10-16 03:17:47 +00:00
|
|
|
guard case let .playing(playState) = state,
|
|
|
|
playState != .dropped,
|
2019-10-15 22:56:53 +00:00
|
|
|
let currentPiece = currentPiece else { return }
|
2019-10-15 16:24:58 +00:00
|
|
|
let modifiedPiece = currentPiece.moved(by: (1, 0))
|
|
|
|
if !overlapsAny(modifiedPiece) {
|
|
|
|
self.currentPiece = modifiedPiece
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func drop() {
|
2019-10-16 03:17:47 +00:00
|
|
|
guard case let .playing(playState) = state, playState != .dropped else { return }
|
2019-10-15 16:24:58 +00:00
|
|
|
currentPiece = currentPieceAtDropPoint
|
2019-10-15 22:56:53 +00:00
|
|
|
state = .playing(.dropped)
|
2019-10-15 16:24:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 03:17:47 +00:00
|
|
|
public func hold() {
|
|
|
|
guard case .playing(.normal) = state,
|
|
|
|
let currentPiece = currentPiece else { return }
|
|
|
|
if let heldTetromino = heldTetromino {
|
|
|
|
self.heldTetromino = currentPiece.tetromino
|
|
|
|
self.currentPiece = GamePiece(tetromino: heldTetromino, topLeft: ((width - heldTetromino.shape.count) / 2, 0))
|
|
|
|
} else {
|
|
|
|
heldTetromino = currentPiece.tetromino
|
|
|
|
nextPiece()
|
|
|
|
}
|
|
|
|
state = .playing(.switched)
|
|
|
|
}
|
|
|
|
|
2019-10-15 22:56:53 +00:00
|
|
|
private func updateCurrentPieceAtDropPoint() {
|
2019-10-15 16:24:58 +00:00
|
|
|
guard let currentPiece = currentPiece else { return }
|
|
|
|
var prev = currentPiece
|
|
|
|
currentPieceAtDropPoint = currentPiece
|
|
|
|
while !overlapsAny(currentPieceAtDropPoint!) {
|
|
|
|
prev = currentPieceAtDropPoint!
|
|
|
|
currentPieceAtDropPoint = currentPieceAtDropPoint!.moved(by: (0, 1))
|
|
|
|
}
|
|
|
|
currentPieceAtDropPoint = prev
|
|
|
|
}
|
|
|
|
|
|
|
|
public func overlapsAny(_ piece: GamePiece) -> Bool {
|
|
|
|
let (left, top) = piece.topLeft
|
|
|
|
for y in 0..<piece.tiles.count {
|
|
|
|
for x in 0..<piece.tiles.first!.count where piece.tiles[y][x] {
|
|
|
|
if top + y >= height || left + x < 0 || left + x >= width || board[left + x, top + y] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-10-15 22:56:53 +00:00
|
|
|
|
2019-10-16 18:43:41 +00:00
|
|
|
public enum GameState {
|
2019-10-15 22:56:53 +00:00
|
|
|
case waitingForStart
|
|
|
|
case playing(PlayState)
|
|
|
|
}
|
|
|
|
|
2019-10-16 18:43:41 +00:00
|
|
|
public enum PlayState {
|
2019-10-15 22:56:53 +00:00
|
|
|
case normal
|
|
|
|
case dropped
|
2019-10-16 03:17:47 +00:00
|
|
|
case switched
|
2019-10-15 22:56:53 +00:00
|
|
|
}
|