Skip to content

Instantly share code, notes, and snippets.

@ramiresnas
Created March 28, 2019 14:59
Show Gist options
  • Save ramiresnas/4dc93e745366a0ebede234f2a7474161 to your computer and use it in GitHub Desktop.
Save ramiresnas/4dc93e745366a0ebede234f2a7474161 to your computer and use it in GitHub Desktop.
Máquina de estados
import GameplayKit
class JumpState : GKState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
let isValid = stateClass is WalkState.Type
if !isValid {
print("você só pode entrar no estado de andar")
}
return isValid
}
override func didEnter(from previousState: GKState?) {
jump()
}
private func jump() {
print("jumping")
}
}
class RunState : GKState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
let isValid = stateClass is WalkState.Type || stateClass is JumpState.Type
if !isValid {
print("você só pode entrar no estado de andar")
}
return isValid
}
override func didEnter(from previousState: GKState?) {
run()
}
private func run() {
print("runing")
}
}
class WalkState : GKState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
let isValide = stateClass is JumpState.Type || stateClass is RunState.Type
if !isValide {
print("você não pode entrar no estado de andar")
}
return isValide
}
override func didEnter(from previousState: GKState?) {
walk()
}
private func walk() {
print("walking")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment