-
Notifications
You must be signed in to change notification settings - Fork 1
/
StateMachine.swift
146 lines (108 loc) · 4.72 KB
/
StateMachine.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// StateMachine.swift
// KSwift
//
// Created by Adam Roth on 6/5/14.
// Copyright (c) 2014 Adam Roth, Inc. All rights reserved.
//
// Port of impactjs-statemachine (https://github.com/drhayes/impactjs-statemachine)
// with transitional animation enhnacements. Thanks David.
//
import Foundation
import SpriteKit
class StateMachine {
var initialState: String?
var currentState: String?
var previousState: String?
var states = Dictionary<String, State>()
var transitions = Dictionary<String, StateMachineTransition>()
var lastTransition: StateMachineTransition?
var entity: Character!
var history:String[] = [];
init(){
}
func setEntity(entity:Character){
self.entity = entity;
}
func addState(name:String, enter: (()->Void), update: (()->Void), exit: (()->Void)) {
self.states[ name ] = State(name: name, enter: enter, update: update, exit: exit);
if !self.initialState {
self.initialState = name
self.currentState = name
}
}
func getState(name:String) -> State {
return self.states[ name ]!
}
func addTransition(name: String, fromState: String, toState: String, predicate: (()->Bool)){
if( self.states[ fromState ] == nil ){
NSLog("Missing from state: %@", fromState);
}
if( self.states[ toState ] == nil ){
NSLog("Missing to state: %@", toState);
}
var transition = StateMachineTransition(name: name, fromState: fromState, toState: toState, predicate: predicate)
self.transitions[ name ] = transition
}
func getTransition(name:String) -> StateMachineTransition {
return self.transitions[ name ]!
}
func update(){
if( self.currentState == nil ){
self.currentState = self.initialState;
}
var state = self.getState(self.currentState!);
if( self.previousState != self.currentState ){
if( self.entity.anims.count > 0 ){
if( self.lastTransition != nil && self.entity.anims[ self.lastTransition!.name ] ){
// Transitional Animation
self.entity.currentAnim = self.entity.anims[ self.lastTransition!.name ]?;
self.entity.sprite.runAction( SKAction.repeatAction( SKAction.animateWithTextures(self.entity.anims[ self.lastTransition!.name]?.frames, timePerFrame: 0.05), count: 1), completion: {
if( state.enter != nil ){
state.enter(); // self.lastTransition
self.entity.currentAnim = self.entity.anims[ self.currentState! ]
// Enter Animation
self.entity.sprite.runAction( SKAction.repeatActionForever( SKAction.animateWithTextures(self.entity.anims[ self.currentState! ]?.frames, timePerFrame: 0.07)))
}
});
}else{
if( state.enter != nil ){
state.enter(); // self.lastTransition
// Enter animation
self.entity.currentAnim = self.entity.anims[ self.currentState! ]
self.entity.sprite.runAction( SKAction.repeatActionForever( SKAction.animateWithTextures(self.entity.anims[ self.currentState! ]?.frames, timePerFrame: 0.07)))
}
}
}
self.previousState = self.currentState;
self.history += self.currentState!;
}
if( state.update != nil ){
state.update();
}
// Interate through transitions
for t_struct in transitions {
var name = t_struct.0;
var transition = t_struct.1;
if( transition.fromState == self.currentState && transition.predicate() ){
self.lastTransition = transition;
if( state.exit != nil ){
state.exit();
}
self.currentState = transition.toState;
}
}
}
}
struct State {
var name: String
var enter: (()->Void)
var update: (()->Void)
var exit: (()->Void)
}
struct StateMachineTransition {
var name: String
var fromState: String
var toState: String
var predicate: (()->Bool)
}