Different colors for tetrominoes

This commit is contained in:
Shadowfacts 2019-10-15 17:52:16 -04:00
parent c948ecd587
commit dec8131898
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 70 additions and 16 deletions

View File

@ -12,41 +12,43 @@ public struct GameBoard {
public let width: Int
public let height: Int
public internal(set) var tiles: [[Bool]]
public internal(set) var tiles: [[Tetromino?]]
public init(width: Int, height: Int) {
self.width = width
self.height = height
tiles = (1...height).map { _ in
Array(repeating: false, count: width)
Array(repeating: nil, count: width)
}
}
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]
}
mutating func set(piece: GamePiece) {
public mutating func set(piece: GamePiece) {
let (left, top) = piece.topLeft
for y in 0..<piece.tiles.count where y + top < height {
for x in 0..<piece.tiles.first!.count where x + left < width {
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 {
self.tiles[y][x] = true
self.tiles[y][x] = nil
}
}
mutating func unset(tiles: [(Int, Int)]) {
for (x, y) in tiles {
self.tiles[y][x] = false
}
func rowFull(_ row: Int) -> Bool {
return tiles[row].allSatisfy({ $0 != nil })
}
}

View File

@ -48,16 +48,15 @@ public class GameController: ObservableObject {
}
func clearLines() {
let fullRow = Array(repeating: true, count: width)
var row = height - 1
while row >= 0 {
if board.tiles[row] == fullRow {
if board.rowFull(row) {
board.tiles.remove(at: row)
}
row -= 1
}
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)
}
}

View File

@ -18,11 +18,21 @@ struct CurrentPieceView: View {
var body: some View {
GridView(rows: self.boardHeight, columns: self.boardWidth) { (col, row, size) in
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)
}
}
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 {
guard let currentPiece = self.currentPiece else { return false }
let (left, top) = currentPiece.topLeft

View File

@ -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
}
}
}

View File

@ -16,7 +16,7 @@ struct TilesView: View {
GridView(rows: board.height, columns: board.width) { (col, row, size) in
Rectangle()
.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 {
@State static var board: GameBoard = {
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
}()