This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttackState.swift
60 lines (49 loc) · 1.9 KB
/
AttackState.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// AttackState.swift
// Bomberman
//
// Created by Wolfgang Schreurs on 02/05/16.
//
//
import SpriteKit
import GameplayKit
class AttackState: State {
override func updateForEntity(_ entity: Entity, configComponent: ConfigComponent, visualComponent: VisualComponent, didUpdate: @escaping () -> Void) {
var actions = [SKAction]()
// append sound
if let attackSound = configComponent.attackSound {
let filePath = configComponent.configFilePath.stringByAppendingPathComponent(attackSound)
let play = playAction(forFileAtPath: filePath, spriteNode: visualComponent.spriteNode)
actions.append(play)
}
// append animation
let attackAnimRange = attackAnimRangeForCurrentDirection()
if attackAnimRange.count > 0 {
let totalTime: Double = 1.0
let sprites = Array(visualComponent.sprites[attackAnimRange])
let timePerFrame = totalTime / Double(sprites.count)
let anim = SKAction.animate(with: sprites, timePerFrame: timePerFrame)
actions.append(anim)
}
let completion = {
didUpdate()
entity.delegate?.entityDidAttack(entity)
}
if actions.count > 0 {
visualComponent.spriteNode.run(SKAction.sequence(actions), completion: completion)
} else {
completion()
}
}
override func canMoveDuringUpdate() -> Bool {
return false
}
// MARK: - Private
fileprivate func attackAnimRangeForCurrentDirection() -> CountableRange<Int> {
var animRange = 0 ..< 0
if let entity = self.entity, let configComponent = entity.component(ofType: ConfigComponent.self) {
animRange = configComponent.attackAnimation.animRangeForDirection(entity.direction)
}
return animRange
}
}