Different colors for tetrominoes
This commit is contained in:
parent
c948ecd587
commit
dec8131898
|
@ -12,41 +12,43 @@ public struct GameBoard {
|
||||||
|
|
||||||
public let width: Int
|
public let width: Int
|
||||||
public let height: Int
|
public let height: Int
|
||||||
public internal(set) var tiles: [[Bool]]
|
public internal(set) var tiles: [[Tetromino?]]
|
||||||
|
|
||||||
public init(width: Int, height: Int) {
|
public init(width: Int, height: Int) {
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
tiles = (1...height).map { _ in
|
tiles = (1...height).map { _ in
|
||||||
Array(repeating: false, count: width)
|
Array(repeating: nil, count: width)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscript(x: Int, y: Int) -> Bool {
|
public subscript(x: Int, y: Int) -> Bool {
|
||||||
|
return self.tiles[y][x] != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
public subscript(x: Int, y: Int) -> Tetromino? {
|
||||||
return self.tiles[y][x]
|
return self.tiles[y][x]
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func set(piece: GamePiece) {
|
public mutating func set(piece: GamePiece) {
|
||||||
let (left, top) = piece.topLeft
|
let (left, top) = piece.topLeft
|
||||||
|
|
||||||
for y in 0..<piece.tiles.count where y + top < height {
|
for y in 0..<piece.tiles.count where y + top < height {
|
||||||
for x in 0..<piece.tiles.first!.count where x + left < width {
|
for x in 0..<piece.tiles.first!.count where x + left < width {
|
||||||
if piece.tiles[y][x] {
|
if piece.tiles[y][x] {
|
||||||
self.tiles[y + top][x + left] = true
|
self.tiles[y + top][x + left] = piece.tetromino
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public mutating func set(tiles: [(Int, Int)]) {
|
mutating func clear(tiles: [(Int, Int)]) {
|
||||||
for (x, y) in tiles {
|
for (x, y) in tiles {
|
||||||
self.tiles[y][x] = true
|
self.tiles[y][x] = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func unset(tiles: [(Int, Int)]) {
|
func rowFull(_ row: Int) -> Bool {
|
||||||
for (x, y) in tiles {
|
return tiles[row].allSatisfy({ $0 != nil })
|
||||||
self.tiles[y][x] = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,16 +48,15 @@ public class GameController: ObservableObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearLines() {
|
func clearLines() {
|
||||||
let fullRow = Array(repeating: true, count: width)
|
|
||||||
var row = height - 1
|
var row = height - 1
|
||||||
while row >= 0 {
|
while row >= 0 {
|
||||||
if board.tiles[row] == fullRow {
|
if board.rowFull(row) {
|
||||||
board.tiles.remove(at: row)
|
board.tiles.remove(at: row)
|
||||||
}
|
}
|
||||||
row -= 1
|
row -= 1
|
||||||
}
|
}
|
||||||
for _ in 0..<height - board.tiles.count {
|
for _ in 0..<height - board.tiles.count {
|
||||||
board.tiles.insert(Array(repeating: false, count: width), at: 0)
|
board.tiles.insert(Array(repeating: nil, count: width), at: 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,11 +18,21 @@ struct CurrentPieceView: View {
|
||||||
var body: some View {
|
var body: some View {
|
||||||
GridView(rows: self.boardHeight, columns: self.boardWidth) { (col, row, size) in
|
GridView(rows: self.boardHeight, columns: self.boardWidth) { (col, row, size) in
|
||||||
Rectangle()
|
Rectangle()
|
||||||
.foregroundColor(self.currentPieceAt(col, row) ? Color.blue : self.droppedPieceAt(col, row) ? Color.gray : Color.clear)
|
.foregroundColor(self.colorAt(col, row))
|
||||||
.frame(width: size, height: size)
|
.frame(width: size, height: size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func colorAt(_ col: Int, _ row: Int) -> Color {
|
||||||
|
if currentPieceAt(col, row) {
|
||||||
|
return self.currentPiece!.tetromino.color
|
||||||
|
} else if droppedPieceAt(col, row) {
|
||||||
|
return .gray
|
||||||
|
} else {
|
||||||
|
return .clear
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func currentPieceAt(_ col: Int, _ row: Int) -> Bool {
|
func currentPieceAt(_ col: Int, _ row: Int) -> Bool {
|
||||||
guard let currentPiece = self.currentPiece else { return false }
|
guard let currentPiece = self.currentPiece else { return false }
|
||||||
let (left, top) = currentPiece.topLeft
|
let (left, top) = currentPiece.topLeft
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
//
|
||||||
|
// Tetromino+Color.swift
|
||||||
|
// Tetris
|
||||||
|
//
|
||||||
|
// Created by Shadowfacts on 10/15/19.
|
||||||
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
import TetrisKit
|
||||||
|
|
||||||
|
fileprivate extension Color {
|
||||||
|
static let tetrominoAqua = Color(red: 0, green: 214/255, blue: 211/255)
|
||||||
|
static let tetrominoBlue = Color(red: 0, green: 79/255, blue: 214/255)
|
||||||
|
static let tetrominoOrange = Color(red: 255/255, green: 128/255, blue: 0)
|
||||||
|
static let tetrominoYellow = Color(red: 255/255, green: 229/255, blue: 0)
|
||||||
|
static let tetrominoGreen = Color(red: 0, green: 214/255, blue: 71/255)
|
||||||
|
static let tetrominoPurple = Color(red: 168/255, green: 0, blue: 214/255)
|
||||||
|
static let tetrominoRed = Color(red: 214/255, green: 0, blue: 61/255)
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Tetromino {
|
||||||
|
var color: Color {
|
||||||
|
switch self {
|
||||||
|
case .i:
|
||||||
|
return .tetrominoAqua
|
||||||
|
case .o:
|
||||||
|
return .tetrominoYellow
|
||||||
|
case .t:
|
||||||
|
return .tetrominoPurple
|
||||||
|
case .j:
|
||||||
|
return .tetrominoOrange
|
||||||
|
case .l:
|
||||||
|
return .tetrominoBlue
|
||||||
|
case .s:
|
||||||
|
return .tetrominoRed
|
||||||
|
case .z:
|
||||||
|
return .tetrominoGreen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,7 +16,7 @@ struct TilesView: View {
|
||||||
GridView(rows: board.height, columns: board.width) { (col, row, size) in
|
GridView(rows: board.height, columns: board.width) { (col, row, size) in
|
||||||
Rectangle()
|
Rectangle()
|
||||||
.frame(width: size, height: size)
|
.frame(width: size, height: size)
|
||||||
.foregroundColor(self.board[col, row] ? Color.red : Color.black)
|
.foregroundColor(self.board[col, row]?.color ?? Color.clear)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,9 @@ struct TilesView: View {
|
||||||
struct TilesView_Previews: PreviewProvider {
|
struct TilesView_Previews: PreviewProvider {
|
||||||
@State static var board: GameBoard = {
|
@State static var board: GameBoard = {
|
||||||
var b = GameBoard(width: 10, height: 16)
|
var b = GameBoard(width: 10, height: 16)
|
||||||
b.set(tiles: [(0, 15), (1, 15), (1, 14), (2, 14)])
|
var piece = GamePiece(tetromino: .z)
|
||||||
|
piece.topLeft = (0, 14)
|
||||||
|
b.set(piece: piece)
|
||||||
return b
|
return b
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue