Skip to content

Instantly share code, notes, and snippets.

@veeneck
veeneck / updateService.swift
Created January 5, 2015 18:04
Update Loop Service
import SpriteKit
class UpdateService {
/// Handle on current SKScene
let scene : Level
/// Array of callbacks to be called each time the updateLoopRuns
var updateListeners : Array<((CFTimeInterval) -> ())> = []
@veeneck
veeneck / cache.swift
Last active August 29, 2015 14:12
Cache enumerateChildNodes
/// If cache is established
if self.updateListeners.count > 0 {
for listener in self.updateListeners {
listener(currentTime)
}
}
/// Rebuild the cache
else {
self.layers[WorldLayer.World.rawValue].enumerateChildNodesWithName("Character") {
node, stop in
@veeneck
veeneck / function2.swift
Created December 21, 2014 17:49
Swift oddity fix
func boundScaleToWindow(scale:CGFloat) -> CGFloat {
/// Right now, this is hard coded against a 2800x1880 image sitting in a 2048x1536 window whos center is 0.5/0.5
if(scale > 1.3) {
return 1.3
}
if(scale < 0.9) {
return 0.9
}
@veeneck
veeneck / function.swift
Created December 21, 2014 17:47
Swift oddities
func boundScaleToWindow(var scale:CGFloat) -> CGFloat {
/// Right now, this is hard coded against a 2800x1880 image sitting in a 2048x1536 window whos center is 0.5/0.5
if(scale > 1.3) {
scale = 1.3
}
if(scale < 0.9) {
scale = 0.9
}
@veeneck
veeneck / translate.swift
Created September 22, 2014 18:59
Translate matrix
// Our for loop inside of the wheel() function
for (index, unit) in enumerate(self.units) {
var vector = self.formation!.getVectorAtPosition(index)
// Translate unit to new point. Basically, subtract rotation center offset from units position
vector = CGVectorMake(vector.dx - self.translateX, vector.dy - self.translateY)
// Rotate the matrix at units position
var newX = vector.dx * cosRadian - vector.dy * sinRadian
var newY = vector.dx * sinRadian + vector.dy * cosRadian
@veeneck
veeneck / wheel_update.swift
Created September 22, 2014 17:49
Update to wheel
func updateWithTime(currentTime:NSTimeInterval) {
// Local boolean. Next step would be to use a state machine.
if(self.wheeling) {
self.wheel()
}
}
@veeneck
veeneck / wheel_center.swift
Last active August 29, 2015 14:06
Wheel around center point
// This would be the public method callled to start a wheel
func wheelToAngle(angle:CGFloat) {
self.wheelAngle = angle
self.wheeling = true
}
// This would run each game loop to perform the wheel
func wheel() {
// Hard code the speed of the turn. Each frame, move 0.003 radians until we reach target.
@veeneck
veeneck / closest.swift
Created September 8, 2014 18:29
Find closest unit to a position
func findClosestUnitToPosition(destination:CGPoint, var positionedUnits:Array<CharacterNode>) -> CharacterNode {
var closestUnit : CharacterNode? = nil
var closestDistance : CGFloat? = nil
// Loop over all of our units.
for(i, unit) in enumerate(self.units) {
// If the unit hasn't been moved already.
if(!contains(positionedUnits, unit)) {
@veeneck
veeneck / gist:81ed703521edb8128a52
Created September 8, 2014 18:21
Move into formation with priority
func moveIntoFormation() {
// New unit array. Starts empty, and once we assign a unit to a position,
// we'll add to this array.
var positionedUnits : Array<CharacterNode> = []
// Instead of looping over units, loop over unit count
for var index = 0; index < self.units.count; ++index {
// Get the vector from the formation class for that position.
@veeneck
veeneck / rotation.swift
Last active August 29, 2015 14:06
Rotate our formation.
func moveIntoFormation() {
// Loop over each unit in our Squad.
for (i, unit) in enumerate(self.units) {
// Get the vector from the formation class for that position.
// Just a simple accessor to the array.
let vector = self.formation!.getVectorAtPosition(i)
// Hard coded rotation by radian of 0.4.