// // 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 var topLeft: (Int, Int) public internal(set) var rotation: Double = 0 public internal(set) var tiles: [[Bool]] public init(tetromino: Tetromino, topLeft: (Int, Int) = (0, 0)) { self.tetromino = tetromino self.tiles = tetromino.shape self.topLeft = topLeft } mutating func rotate(direction: RotationDirection) { switch direction { case .clockwise: self.tiles.rotateClockwise() rotation += 90 case .counterClockwise: self.tiles.rotateCounterclockwise() rotation -= 90 } } } 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 } }