2
2
import SpriteKit
3
3
4
4
protocol Interactable { /* empty */ }
5
- protocol Destructable { /* empty */ }
5
+ protocol Destructible { /* empty */ }
6
6
protocol PositionUpdatable { /* empty */ }
7
7
8
- class BunnyEnemy : SKSpriteNode , Interactable , Destructable , PositionUpdatable {
8
+ class BunnyEnemy : SKSpriteNode , Interactable , Destructible {
9
9
10
10
// MARK: - Properties
11
11
@@ -24,7 +24,7 @@ class BunnyEnemy: SKSpriteNode, Interactable, Destructable, PositionUpdatable {
24
24
}
25
25
}
26
26
27
- class Player : SKSpriteNode , PositionUpdatable {
27
+ class Player : SKSpriteNode , Destructible , PositionUpdatable {
28
28
29
29
// MARK: - Initializers
30
30
@@ -41,14 +41,20 @@ class Player: SKSpriteNode, PositionUpdatable {
41
41
42
42
struct InteractionSystem {
43
43
44
+ // MARK: - Properties
45
+
44
46
typealias InteractableSprite = SKSpriteNode & Interactable
45
47
46
48
private( set) var interactables : [ InteractableSprite ]
47
49
50
+ // MARK: - Initializers
51
+
48
52
init ( ) {
49
53
interactables = [ ]
50
54
}
51
55
56
+ // MARK: - Methods
57
+
52
58
mutating func add( interactable: InteractableSprite ) {
53
59
interactables += [ interactable]
54
60
}
@@ -62,12 +68,16 @@ struct InteractionSystem {
62
68
63
69
struct DestructionSystem {
64
70
65
- typealias DestructableSprite = SKSpriteNode & Destructable
71
+ // MARK: - Properties
72
+
73
+ typealias DestructibleSprite = SKSpriteNode & Destructible
66
74
67
- private( set) var destructables : [ DestructableSprite ]
75
+ private( set) var destructibles : [ DestructibleSprite ]
76
+
77
+ // MARK: - Initializers
68
78
69
79
init ( ) {
70
- destructables = [ ]
80
+ destructibles = [ ]
71
81
}
72
82
73
83
private lazy var animationSequence : SKAction = {
@@ -77,29 +87,37 @@ struct DestructionSystem {
77
87
return sequence
78
88
} ( )
79
89
80
- mutating func add( destructable: DestructableSprite ) {
81
- destructables += [ destructable]
90
+ // MARK: - Methods
91
+
92
+ mutating func add( destructible: DestructibleSprite ) {
93
+ destructibles += [ destructible]
82
94
}
83
95
84
96
mutating func destroy( ) {
85
- for destructable in destructables {
86
- destructable . run ( animationSequence)
97
+ for destructible in destructibles {
98
+ destructible . run ( animationSequence)
87
99
}
88
100
}
89
101
}
90
102
91
103
struct PositionUpdatableSystem {
92
104
105
+ // MARK: - Properties
106
+
93
107
typealias PositionUpdatableSprite = SKSpriteNode & PositionUpdatable
94
108
95
109
private( set) var updatables : [ PositionUpdatableSprite ]
96
110
var inputSource : InputSource
97
111
112
+ // MARK: - Initializers
113
+
98
114
init ( inputSource: InputSource ) {
99
115
self . inputSource = inputSource
100
116
updatables = [ ]
101
117
}
102
118
119
+ // MARK: - Methods
120
+
103
121
mutating func add( updatable: PositionUpdatableSprite ) {
104
122
updatables += [ updatable]
105
123
}
@@ -168,9 +186,15 @@ class Scene: SKScene {
168
186
// ...
169
187
// At some point, enemies are added to the Destruction System, this piece of code simulates that:
170
188
if currentTime == 3840578 {
171
- destructionSystem. add ( destructable : bunny)
172
- destructionSystem. add ( destructable : bunnyFat)
189
+ destructionSystem. add ( destructible : bunny)
190
+ destructionSystem. add ( destructible : bunnyFat)
173
191
}
192
+
193
+ // Later on, a bunny killed our player, so we add the player to the Destruction System to be destroyed:
194
+ destructionSystem. add ( destructible: player)
195
+
196
+ // At the end of the update loop we call the destroy method to delegate the destruction to the corresponding system
197
+ destructionSystem. destroy ( )
174
198
}
175
199
176
200
// MARK: - Touch handling
0 commit comments