Skip to content

Instantly share code, notes, and snippets.

@zaneclaes
Last active November 18, 2016 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaneclaes/879c30c51d5655100230 to your computer and use it in GitHub Desktop.
Save zaneclaes/879c30c51d5655100230 to your computer and use it in GitHub Desktop.
Coordinate System in Swift
//
// Coordinate.swift
// ForeverMaze
//
// Created by Zane Claes on 1/18/16.
// Copyright © 2016 inZania LLC. All rights reserved.
//
import Foundation
import SpriteKit
/************************************************************************
* Coordinate
* Provides a convenient interface for referring to a position on the map
* Translates between positions (UInt) and indicies (Int)
* Indicies are useful for wraparound data
***********************************************************************/
func + (p1: Coordinate, p2: Coordinate) -> Coordinate {
return Coordinate(xIndex: p1.xIndex + p2.xIndex, yIndex: p1.yIndex + p2.yIndex)
}
func - (p1: Coordinate, p2: Coordinate) -> Coordinate {
return Coordinate(xIndex: p1.xIndex - p2.xIndex, yIndex: p1.yIndex - p2.yIndex)
}
func + (p1: Coordinate, p2: (Int, Int)) -> Coordinate {
return Coordinate(xIndex: p1.xIndex + p2.0, yIndex: p1.yIndex + p2.1)
}
func - (p1: Coordinate, p2: (Int, Int)) -> Coordinate {
return Coordinate(xIndex: p1.xIndex - p2.0, yIndex: p1.yIndex - p2.1)
}
struct Coordinate : CustomStringConvertible, Hashable {
var x: UInt
var y: UInt
func getIndicies() -> (Int, Int) {
var x:Int = Int(self.x)
var y:Int = Int(self.y)
x = x >= Int(Config.worldSize.width) - Int(Config.screenTiles.width) ? (x - Int(Config.worldSize.width)) : x
y = y >= Int(Config.worldSize.height) - Int(Config.screenTiles.height) ? (y - Int(Config.worldSize.height)) : y
return (x, y)
}
var hashValue: Int {
return self.description.hashValue
}
var xIndex: Int {
return self.getIndicies().0
}
var yIndex: Int {
return self.getIndicies().1
}
var description:String {
return "\(x)x\(y)"
}
func willWrapAroundWorldX(newPos: Coordinate, worldSize: MapSize, threshold: UInt) -> Bool {
return (newPos.x < threshold && self.x >= worldSize.width - threshold) ||
(newPos.x >= worldSize.width - threshold && self.x < threshold)
}
func willWrapAroundWorldY(newPos: Coordinate, worldSize: MapSize, threshold: UInt) -> Bool {
return (newPos.y < threshold && self.y >= worldSize.height - threshold) ||
(newPos.y >= worldSize.height - threshold && self.y < threshold)
}
func willWrapAroundWorld(newPos: Coordinate, worldSize: MapSize, threshold: UInt) -> Bool {
return self.willWrapAroundWorldX(newPos, worldSize: worldSize, threshold: threshold) ||
self.willWrapAroundWorldY(newPos, worldSize: worldSize, threshold: threshold)
}
init(x: UInt, y: UInt) {
self.x = x
self.y = y
}
init(xIndex: Int, yIndex: Int) {
let xPos = xIndex < 0 ? UInt(Int(Config.worldSize.width) + xIndex) : UInt(xIndex)
self.x = xPos >= UInt(Config.worldSize.width) ? (xPos - UInt(Config.worldSize.width)) : xPos
let yPos = yIndex < 0 ? UInt(Int(Config.worldSize.height) + yIndex) : UInt(yIndex)
self.y = yPos >= UInt(Config.worldSize.height) ? (yPos - UInt(Config.worldSize.height)) : yPos
}
init(desc: String) {
let parts = desc.componentsSeparatedByString("x")
self.x = UInt(parts[0])!
self.y = UInt(parts[1])!
}
}
func ==(lhs: Coordinate, rhs: Coordinate) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment