Last active
August 29, 2015 14:14
-
-
Save chrisschreiner/bc0bab4f1a03139080d4 to your computer and use it in GitHub Desktop.
SceneStateMachine V0.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SceneStateMachine.swift | |
// Troup | |
// | |
// Created by Chris Patrick Schreiner on 04.02.15. | |
// | |
// | |
import Foundation | |
import SpriteKit | |
import HexNode | |
protocol PState { | |
func handleMouseDown(e: NSEvent) | |
func handleMouseMove(e: NSEvent) | |
func handleMouseUp(e: NSEvent) | |
func handleKeyboardModifierFlags(m: NSEventModifierFlags) | |
} | |
protocol PStateMachineContext { | |
func changeState(newState:PState) | |
var scene: MyScene {get} | |
var view: MySKView {get} | |
} | |
class StateMachineContext: PState, PStateMachineContext { | |
var state: PState? | |
var scene: MyScene | |
var view: MySKView | |
init(scene: MyScene, view: MySKView) { | |
self.scene = scene | |
self.view = view | |
state = IdleState(self) | |
} | |
func handleMouseDown(e:NSEvent) { | |
state?.handleMouseDown(e) | |
} | |
func handleMouseMove(e:NSEvent) { | |
state?.handleMouseMove(e) | |
} | |
func handleMouseUp(e: NSEvent) { | |
state?.handleMouseUp(e) | |
} | |
func handleKeyboardModifierFlags(m: NSEventModifierFlags) { | |
state?.handleKeyboardModifierFlags(m) | |
} | |
func changeState(newState:PState) { | |
state = newState | |
} | |
} | |
class IdleState:RootState { | |
override init(_ context:PStateMachineContext) { | |
super.init(context) | |
} | |
override func handleMouseDown(e: NSEvent) { | |
if let (node,location) = getNodeAtEvent(e) { | |
if let node = node { | |
changeState(HoldingObjectState(context, holdingNode: node, location:location)) | |
} | |
} | |
} | |
private func parseMouseMoveEvent(e:NSEvent) { | |
let location = view.convertPoint(e.locationInNode(scene), fromView:nil) | |
let n = scene.nodeAtPoint(location) | |
if n is HexNode { | |
println(n) | |
} | |
} | |
private func getNodeAtEvent(e:NSEvent) -> (HexNode?,CGPoint)? { | |
let location = view.convertPoint(e.locationInNode(scene), fromView:nil) | |
let n = scene.nodeAtPoint(location) | |
if n is HexNode { | |
return (n as? HexNode,location) | |
} | |
return (nil,CGPointZero) | |
} | |
} | |
class HoldingObjectState: RootState { | |
var holdingNode: HexNode | |
var originalLocation: CGPoint | |
var physicsBody: SKPhysicsBody? | |
init(_ context: PStateMachineContext, holdingNode: HexNode, location:CGPoint) { | |
self.holdingNode = holdingNode | |
self.originalLocation = location | |
super.init(context) | |
//store physics body before removing it | |
self.physicsBody = self.holdingNode.physicsBody | |
self.holdingNode.physicsBody = nil | |
} | |
override func handleMouseMove(e: NSEvent) { | |
holdingNode.position = e.locationInNode(scene.worldNode!) | |
} | |
override func handleMouseUp(e: NSEvent) { | |
changeState(IdleState(context)) | |
//restore physics body | |
holdingNode.physicsBody = physicsBody | |
} | |
} | |
class RootState:PState { | |
var context: PStateMachineContext | |
lazy var scene: MyScene = { | |
[unowned self] in | |
return self.context.scene | |
}() | |
lazy var view: MySKView = { | |
[unowned self] in | |
return self.context.view | |
}() | |
init(_ context:PStateMachineContext) { | |
self.context = context | |
} | |
func handleMouseDown(e:NSEvent) {} | |
func handleMouseMove(e:NSEvent) {} | |
func handleMouseUp(e: NSEvent) {} | |
func handleKeyboardModifierFlags(m: NSEventModifierFlags) {} | |
func changeState(newState:PState) { | |
context.changeState(newState) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment