This repository was 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
Expand file tree
/
Copy pathIndicator.swift
More file actions
84 lines (63 loc) · 2.14 KB
/
Indicator.swift
File metadata and controls
84 lines (63 loc) · 2.14 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// Indicator.swift
// Bomberman
//
// Created by Wolfgang Schreurs on 14/08/16.
//
//
import SpriteKit
enum IndicatorDirection {
case left
case right
}
class Indicator : SKShapeNode {
fileprivate(set) var direction: IndicatorDirection = .left
fileprivate(set) var size: CGSize = CGSize()
convenience init(direction: IndicatorDirection) {
self.init(size: CGSize(width: 10, height: 15), direction: direction)
}
init(size: CGSize, direction: IndicatorDirection) {
self.direction = direction
self.size = size
super.init()
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
// MARK: - Private
fileprivate func commonInit() {
strokeColor = SKColor.white
fillColor = SKColor.white
var points: [(x: CGFloat, y: CGFloat)]
switch direction {
case .left:
points = [(size.width, size.height), (0, size.height / 2), (size.width, 0)]
case .right:
points = [(0, size.height), (0, 0), (size.width, size.height / 2)]
}
self.path = pathForPoints(points)
}
fileprivate func pathForPoints(_ points: [(x: CGFloat, y: CGFloat)]) -> CGPath {
let path = CGMutablePath()
for point in points {
if point == points.first! {
path.move(to: CGPoint(x: point.x, y: point.y))
} else {
path.addLine(to: CGPoint(x: point.x, y: point.y))
}
}
path.closeSubpath()
return path
}
// MARK: - Public
func runScaleAnimation(_ fromValue: CGFloat, toValue: CGFloat) {
assert(fromValue != toValue, "fromValue should be different from toValue")
let scaleTo = SKAction.scale(to: toValue, duration: 1.0)
let scaleFrom = SKAction.scale(to: fromValue, duration: 1.0)
let sequence = SKAction.sequence([scaleTo, scaleFrom])
let scaleRepeat = SKAction.repeatForever(sequence)
run(scaleRepeat)
}
}