你是对的,状态是实体相关的,而不是组件相关的。将状态机直接放入实体中,或者创建一个所有实体都继承自的实体基类。
class VisualEntityBase : GKEntity, VisualEntity {
var node: SKSpriteNode!
var stateMachine: GKStateMachine!
// MARK: Initialization
init(imageNamed: String, atStartPosition: CGPoint) {
super.init()
// Initialise Texture
let texture = SKTexture(imageNamed: imageNamed)
// Initialise Node
self.node = SKSpriteNode(texture: texture, size: texture.size())
self.node.position = atStartPosition
// Initialise StateMachine
self.stateMachine = GKStateMachine(states: [
VisualEntityIdle(),
VisualEntityPendingMove(),
VisualEntityMoving()
])
self.stateMachine.enter(VisualEntityIdle.self)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}