Tetris/TetrisKit/GamePiece.swift

51 lines
1.3 KiB
Swift
Raw Normal View History

2019-10-15 16:24:58 +00:00
//
// 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
2019-10-15 21:26:06 +00:00
public var topLeft: (Int, Int)
public internal(set) var rotation: Double = 0
2019-10-15 16:24:58 +00:00
public internal(set) var tiles: [[Bool]]
2019-10-16 03:17:47 +00:00
public init(tetromino: Tetromino, topLeft: (Int, Int) = (0, 0)) {
2019-10-15 16:24:58 +00:00
self.tetromino = tetromino
self.tiles = tetromino.shape
2019-10-16 03:17:47 +00:00
self.topLeft = topLeft
2019-10-15 16:24:58 +00:00
}
mutating func rotate(direction: RotationDirection) {
switch direction {
case .clockwise:
self.tiles.rotateClockwise()
rotation += 90
2019-10-15 16:24:58 +00:00
case .counterClockwise:
self.tiles.rotateCounterclockwise()
rotation -= 90
2019-10-15 16:24:58 +00:00
}
}
}
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
}
}