2020-05-24 21:56:11 -06:00
|
|
|
//
|
|
|
|
// Block.swift
|
|
|
|
// Privyet
|
|
|
|
//
|
|
|
|
// Created by Amy Bowersox on 5/23/20.
|
|
|
|
// Copyright © 2020 Erbosoft Metaverse Design Solutions. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import SpriteKit
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Colors that may be applied to blocks (whcih controls the sprite image they use)
|
2020-05-27 13:54:16 -06:00
|
|
|
enum BlockColor: Int, CustomStringConvertible, CaseIterable {
|
|
|
|
case Blue = 0, Orange, Purple, Red, Teal, Yellow, Green
|
2020-05-24 21:56:11 -06:00
|
|
|
|
|
|
|
var spriteName: String {
|
|
|
|
switch self {
|
|
|
|
case .Blue:
|
|
|
|
return "blue"
|
|
|
|
case .Orange:
|
|
|
|
return "orange"
|
|
|
|
case .Purple:
|
|
|
|
return "purple"
|
|
|
|
case .Red:
|
|
|
|
return "red"
|
|
|
|
case .Teal:
|
|
|
|
return "teal"
|
|
|
|
case .Yellow:
|
|
|
|
return "yellow"
|
2020-05-27 13:54:16 -06:00
|
|
|
case .Green:
|
|
|
|
return "green"
|
2020-05-24 21:56:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var description: String {
|
|
|
|
return self.spriteName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 21:04:10 -06:00
|
|
|
// Rectangles in block coordinates
|
|
|
|
class BlockRect {
|
|
|
|
var left: Int
|
|
|
|
var top: Int
|
|
|
|
var right: Int
|
|
|
|
var bottom: Int
|
|
|
|
|
|
|
|
init(left: Int, top: Int, right: Int, bottom: Int) {
|
|
|
|
self.left = left
|
|
|
|
self.top = top
|
|
|
|
self.right = right
|
|
|
|
self.bottom = bottom
|
|
|
|
}
|
|
|
|
|
|
|
|
convenience init(x: Int, y: Int) {
|
|
|
|
self.init(left: x, top: y, right: x, bottom: y)
|
|
|
|
}
|
|
|
|
|
|
|
|
var width: Int {
|
|
|
|
return right - left + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var height: Int {
|
|
|
|
return bottom - top + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
static func +(lhs: BlockRect, rhs: BlockRect) -> BlockRect {
|
|
|
|
return BlockRect(left: min(lhs.left, rhs.left), top: min(lhs.top, rhs.top), right: max(lhs.right, rhs.right), bottom: max(lhs.bottom, rhs.bottom))
|
|
|
|
}
|
|
|
|
|
|
|
|
static func +=(lhs: BlockRect, rhs: BlockRect) -> BlockRect {
|
|
|
|
lhs.left = min(lhs.left, rhs.left)
|
|
|
|
lhs.top = min(lhs.top, rhs.top)
|
|
|
|
lhs.right = max(lhs.right, rhs.right)
|
|
|
|
lhs.bottom = max(lhs.bottom, rhs.bottom)
|
|
|
|
return lhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:10:50 -06:00
|
|
|
// Represents a single block in the game.
|
2020-05-24 21:56:11 -06:00
|
|
|
class Block: Hashable, CustomStringConvertible {
|
|
|
|
// Constants
|
|
|
|
let color: BlockColor
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
var column: Int
|
|
|
|
var row: Int
|
|
|
|
var sprite: SKSpriteNode?
|
|
|
|
|
|
|
|
var spriteName: String {
|
|
|
|
return color.spriteName
|
|
|
|
}
|
|
|
|
|
|
|
|
static func == (lhs: Block, rhs: Block) -> Bool {
|
|
|
|
return lhs.column == rhs.column && lhs.row == rhs.row
|
|
|
|
&& lhs.color.rawValue == rhs.color.rawValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(column)
|
|
|
|
hasher.combine(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
var description: String {
|
|
|
|
return "\(color): [\(column), \(row)]"
|
|
|
|
}
|
|
|
|
|
|
|
|
init(column: Int, row: Int, color: BlockColor) {
|
|
|
|
self.column = column
|
|
|
|
self.row = row
|
|
|
|
self.color = color
|
|
|
|
}
|
|
|
|
}
|