48 lines
1.1 KiB
Swift
48 lines
1.1 KiB
Swift
//
|
|
// GamePiece.swift
|
|
// TetrisKit
|
|
//
|
|
// Created by Shadowfacts on 10/13/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public struct GamePiece {
|
|
public let tetromino: Tetromino
|
|
public internal(set) var topLeft: (Int, Int)
|
|
public internal(set) var tiles: [[Bool]]
|
|
|
|
public init(tetromino: Tetromino) {
|
|
self.tetromino = tetromino
|
|
self.tiles = tetromino.shape
|
|
self.topLeft = (0, 0)
|
|
}
|
|
|
|
mutating func rotate(direction: RotationDirection) {
|
|
switch direction {
|
|
case .clockwise:
|
|
self.tiles.rotateClockwise()
|
|
case .counterClockwise:
|
|
self.tiles.rotateCounterclockwise()
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum RotationDirection {
|
|
case clockwise, counterClockwise
|
|
}
|
|
|
|
extension GamePiece {
|
|
public func moved(by: (Int, Int)) -> GamePiece {
|
|
var modified = self
|
|
modified.topLeft = (topLeft.0 + by.0, topLeft.1 + by.1)
|
|
return modified
|
|
}
|
|
public func rotated(_ direction: RotationDirection) -> GamePiece {
|
|
var modified = self
|
|
modified.rotate(direction: direction)
|
|
return modified
|
|
}
|
|
}
|