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
/
SKAction+Extensions.swift
61 lines (52 loc) · 2.45 KB
/
SKAction+Extensions.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
61
//
// SKAction+Extensions.swift
// Bomberman
//
// Created by Wolfgang Schreurs on 07/05/16.
//
//
import SpriteKit
extension SKAction {
class func shake(_ initialPosition:CGPoint, duration:Float, amplitudeX:Int = 12, amplitudeY:Int = 3) -> SKAction {
let startingX = initialPosition.x
let startingY = initialPosition.y
let numberOfShakes = duration / 0.015
var actionsArray:[SKAction] = []
for _ in 1...Int(numberOfShakes) {
let newXPos = startingX + CGFloat(arc4random_uniform(UInt32(amplitudeX))) - CGFloat(amplitudeX / 2)
let newYPos = startingY + CGFloat(arc4random_uniform(UInt32(amplitudeY))) - CGFloat(amplitudeY / 2)
actionsArray.append(SKAction.move(to: CGPoint(x: newXPos, y: newYPos), duration: 0.015))
}
actionsArray.append(SKAction.move(to: initialPosition, duration: 0.015))
return SKAction.sequence(actionsArray)
}
class func animation(forEntity entity: Entity,
configuration: AnimationConfiguration,
state: State) -> [SKAction] {
var actions = [SKAction]()
if let visualComponent = entity.component(ofType: VisualComponent.self) {
if configuration.delay > 0 {
let wait = SKAction.wait(forDuration: configuration.delay)
actions.append(wait)
}
let animRange = configuration.animRangeForDirection(entity.direction)
if animRange.count > 0 {
let frameCount = animRange.count * configuration.repeatCount
let timePerFrame = configuration.duration / Double(frameCount)
let sprites = Array(visualComponent.sprites[animRange])
let anim = SKAction.animate(with: sprites, timePerFrame: timePerFrame)
if configuration.repeatCount > 1 {
let repeatAnim = SKAction.repeat(anim, count: configuration.repeatCount)
actions.append(repeatAnim)
} else {
actions.append(anim)
}
} else {
let anim = state.defaultAnimation(withDuration: configuration.duration,
repeatCount: configuration.repeatCount)
actions.append(anim)
}
}
return actions
}
}