Opportunity to refactor and reuse loops by adding addFormattedStartAnimsToScene and addFormattedStopAnimsToScene helper blocks/closures to GameCharacter class, passing the relevant animations array and gameScene as arguments.
Using Swift this could be achieved with:
// Helper Blocks
private var addFormattedStartAnimsToScene: (NSArray, SCNScene) -> Void = { (animsArray, gameScene) in
var i: Int = 1
for var animation in animsArray {
let key: NSString = NSString(format: "ANIM_\(i)", i)
gameScene.rootNode.addAnimation(animation as! CAAnimation, forKey: key as String)
i++
}
}
private var addFormattedStopAnimsToScene: (NSArray, SCNScene) -> Void = { (animsArray, gameScene) in
for var i = 0; i < animsArray.count; i++ {
let key: NSString = NSString(format: "ANIM_\(i)", i+1)
gameScene.rootNode.removeAnimationForKey(key as String, fadeOutDuration: 1.0)
}
}
// MARK: Move Animations
func startMoveAnimInScene(scene: SCNScene) {
self.addFormattedStartAnimsToScene(self.charMoveAnims!, scene)
self.actionState = .Move
}
func stopMoveAnimInScene(scene: SCNScene) {
self.addFormattedStopAnimsToScene(self.charMoveAnims!, scene)
}
// MARK: Idle Animations
func startIdleAnimInScene(scene: SCNScene) {
self.addFormattedStartAnimsToScene(self.charIdleAnims!, scene)
self.actionState = .Idle
}
func stopIdleAnimInScene(scene: SCNScene) {
self.addFormattedStopAnimsToScene(self.charIdleAnims!, scene)
}
Opportunity to refactor and reuse loops by adding
addFormattedStartAnimsToSceneandaddFormattedStopAnimsToScenehelper blocks/closures to GameCharacter class, passing the relevant animations array and gameScene as arguments.Using Swift this could be achieved with: