Skip to content

Refactor loops into reusable helper blocks #1

Description

@ltfschoen

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)
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions