Приключение/Приключение Shared/Sprites/Boss.swift

/*
  Copyright (C) 2014 Apple Inc. All Rights Reserved.
  See LICENSE.txt for this sample’s licensing information
  
  Abstract:
  
        Defines the class for the boss enemy character
      
*/
 
import SpriteKit
 
let kBossCollisionRadius: CGFloat = 40.0
let kBossChaseRadius: CGFloat = kBossCollisionRadius * 4.0
 
var kLoadSharedBossAssetsOnceToken: dispatch_once_t = 0
 
class Boss: EnemyCharacter {
    // MARK: Initialization
    
    convenience init(atPosition position: CGPoint) {
        let atlas = SKTextureAtlas(named: "Boss_Idle")
        let bossTexture = atlas.textureNamed("boss_idle_0001.png")
        self.init(texture: bossTexture, atPosition: position)
 
        movementSpeed = movementSpeed * 0.35
        animationSpeed = 1.0/35.0
 
        zPosition = -0.25
        name = "Boss"
 
        attacking = false
 
        let chaseIntelligence = ChaseArtificialIntelligence(character: self, target: nil)
        chaseIntelligence.attackRadius = kBossChaseRadius
        chaseIntelligence.maxAlertRadius = kBossChaseRadius * 4.0
        intelligence = chaseIntelligence
    }
 
    // MARK: Setup
    
    override func configurePhysicsBody() {
        // Assign the physics body; unwrap the physics body to configure it.
        physicsBody = SKPhysicsBody(circleOfRadius: kBossCollisionRadius)
 
        // Our object type for collisions.
        physicsBody!.categoryBitMask = ColliderType.GoblinOrBoss.rawValue
 
        // Collides with these objects.
        physicsBody!.collisionBitMask = ColliderType.all
 
        // We want notifications for colliding with these objects.
        physicsBody!.contactTestBitMask = ColliderType.Projectile.rawValue
    }
 
    // MARK: Scene Processing Support
 
    override func animationDidComplete(animationState: AnimationState) {
        super.animationDidComplete(animationState)
 
        if animationState == AnimationState.Death {
            removeAllActions()
            let actions = [
                SKAction.waitForDuration(3.0),
                SKAction.fadeOutWithDuration(2.0),
                SKAction.removeFromParent()
                /*,
                SKAction.runBlock {
                    characterScene.gameOver()
                }
                */
            ]
 
            runAction(SKAction.sequence(actions))
        }
    }
 
    override func collidedWith(other: SKPhysicsBody) {
        if dying {
            return
        }
 
        if (other.categoryBitMask & ColliderType.Projectile.rawValue) == ColliderType.Projectile.rawValue {
            requestedAnimation = AnimationState.GetHit
            let damage = 2.0
            let killed = applyDamage(damage, projectile: other.node)
 
            if killed {
                // Give the player some points
            }
        }
    }
 
    override func performDeath() {
        removeAllActions()
        super.performDeath()
    }
 
    // MARK: Asset Pre-loading
    
    class func loadSharedAssets() {
        dispatch_once(&kLoadSharedBossAssetsOnceToken) {
            self.idleAnimationFrames = loadFramesFromAtlasWithName("Boss_Idle")
            self.walkAnimationFrames = loadFramesFromAtlasWithName("Boss_Walk")
            self.attackAnimationFrames = loadFramesFromAtlasWithName("Boss_Attack")
            self.getHitAnimationFrames = loadFramesFromAtlasWithName("Boss_GetHit")
            self.deathAnimationFrames = loadFramesFromAtlasWithName("Boss_Death")
            self.damageEmitter = SKEmitterNode.nodeWithName("BossDamage")
 
            let actions = [
                SKAction.colorizeWithColor(SKColor.whiteColor(), colorBlendFactor: 1.0, duration: 0.0),
                SKAction.waitForDuration(0.5),
                SKAction.colorizeWithColorBlendFactor(0.0, duration: 0.1)
            ]
 
            self.damageAction = SKAction.sequence(actions)
        }
    }
}