Skip to content

Commit d7d529d

Browse files
committed
Polished code sample
1 parent 20ed6f3 commit d7d529d

File tree

1 file changed

+36
-12
lines changed
  • Common Design Patterns/Structural/Marker/Marker.playground

1 file changed

+36
-12
lines changed

Common Design Patterns/Structural/Marker/Marker.playground/Contents.swift

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import SpriteKit
33

44
protocol Interactable { /* empty */ }
5-
protocol Destructable { /* empty */ }
5+
protocol Destructible { /* empty */ }
66
protocol PositionUpdatable { /* empty */ }
77

8-
class BunnyEnemy: SKSpriteNode, Interactable, Destructable, PositionUpdatable {
8+
class BunnyEnemy: SKSpriteNode, Interactable, Destructible {
99

1010
// MARK: - Properties
1111

@@ -24,7 +24,7 @@ class BunnyEnemy: SKSpriteNode, Interactable, Destructable, PositionUpdatable {
2424
}
2525
}
2626

27-
class Player: SKSpriteNode, PositionUpdatable {
27+
class Player: SKSpriteNode, Destructible, PositionUpdatable {
2828

2929
// MARK: - Initializers
3030

@@ -41,14 +41,20 @@ class Player: SKSpriteNode, PositionUpdatable {
4141

4242
struct InteractionSystem {
4343

44+
// MARK: - Properties
45+
4446
typealias InteractableSprite = SKSpriteNode & Interactable
4547

4648
private(set) var interactables: [InteractableSprite]
4749

50+
// MARK: - Initializers
51+
4852
init() {
4953
interactables = []
5054
}
5155

56+
// MARK: - Methods
57+
5258
mutating func add(interactable: InteractableSprite) {
5359
interactables += [interactable]
5460
}
@@ -62,12 +68,16 @@ struct InteractionSystem {
6268

6369
struct DestructionSystem {
6470

65-
typealias DestructableSprite = SKSpriteNode & Destructable
71+
// MARK: - Properties
72+
73+
typealias DestructibleSprite = SKSpriteNode & Destructible
6674

67-
private(set) var destructables: [DestructableSprite]
75+
private(set) var destructibles: [DestructibleSprite]
76+
77+
// MARK: - Initializers
6878

6979
init() {
70-
destructables = []
80+
destructibles = []
7181
}
7282

7383
private lazy var animationSequence: SKAction = {
@@ -77,29 +87,37 @@ struct DestructionSystem {
7787
return sequence
7888
}()
7989

80-
mutating func add(destructable: DestructableSprite) {
81-
destructables += [destructable]
90+
// MARK: - Methods
91+
92+
mutating func add(destructible: DestructibleSprite) {
93+
destructibles += [destructible]
8294
}
8395

8496
mutating func destroy() {
85-
for destructable in destructables {
86-
destructable.run(animationSequence)
97+
for destructible in destructibles {
98+
destructible.run(animationSequence)
8799
}
88100
}
89101
}
90102

91103
struct PositionUpdatableSystem {
92104

105+
// MARK: - Properties
106+
93107
typealias PositionUpdatableSprite = SKSpriteNode & PositionUpdatable
94108

95109
private(set) var updatables: [PositionUpdatableSprite]
96110
var inputSource: InputSource
97111

112+
// MARK: - Initializers
113+
98114
init(inputSource: InputSource) {
99115
self.inputSource = inputSource
100116
updatables = []
101117
}
102118

119+
// MARK: - Methods
120+
103121
mutating func add(updatable: PositionUpdatableSprite) {
104122
updatables += [updatable]
105123
}
@@ -168,9 +186,15 @@ class Scene: SKScene {
168186
// ...
169187
// At some point, enemies are added to the Destruction System, this piece of code simulates that:
170188
if currentTime == 3840578 {
171-
destructionSystem.add(destructable: bunny)
172-
destructionSystem.add(destructable: bunnyFat)
189+
destructionSystem.add(destructible: bunny)
190+
destructionSystem.add(destructible: bunnyFat)
173191
}
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()
174198
}
175199

176200
// MARK: - Touch handling

0 commit comments

Comments
 (0)