-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathContents.swift
212 lines (144 loc) · 5.9 KB
/
Contents.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import SpriteKit
protocol Interactable { /* empty */ }
protocol Destructible { /* empty */ }
protocol PositionUpdatable { /* empty */ }
class BunnyEnemy: SKSpriteNode, Interactable, Destructible {
// MARK: - Properties
var movementSpeed: CGFloat
// MARK: - Initializers
init(position: CGPoint, texture: SKTexture, movementSpeed: CGFloat) {
self.movementSpeed = movementSpeed
super.init(texture: texture, color: .clear, size: texture.size())
self.position = position
}
required init?(coder aDecoder: NSCoder) {
fatalError("required init?(coder aDecoder: NSCoder) has not been implemented")
}
}
class Player: SKSpriteNode, Destructible, PositionUpdatable {
// MARK: - Initializers
init(position: CGPoint, texture: SKTexture) {
super.init(texture: texture, color: .clear, size: texture.size())
self.position = position
}
required init?(coder aDecoder: NSCoder) {
fatalError("required init?(coder aDecoder: NSCoder) has not been implemented")
}
}
struct InteractionSystem {
// MARK: - Properties
typealias InteractableSprite = SKSpriteNode & Interactable
private(set) var interactables: [InteractableSprite]
// MARK: - Initializers
init() {
interactables = []
}
// MARK: - Methods
mutating func add(interactable: InteractableSprite) {
interactables += [interactable]
}
func touchesBegan(touches: Set<UITouch>, with event: UIEvent?) {
for interactable in interactables {
interactable.touchesBegan(touches, with: event)
}
}
}
struct DestructionSystem {
// MARK: - Properties
typealias DestructibleSprite = SKSpriteNode & Destructible
private(set) var destructibles: [DestructibleSprite]
// MARK: - Initializers
init() {
destructibles = []
}
private lazy var animationSequence: SKAction = {
let fadeOut = SKAction.fadeOut(withDuration: 1.0)
let remove = SKAction.removeFromParent()
let sequence = SKAction.sequence([fadeOut, remove])
return sequence
}()
// MARK: - Methods
mutating func add(destructible: DestructibleSprite) {
destructibles += [destructible]
}
mutating func destroy() {
for destructible in destructibles {
destructible.run(animationSequence)
}
}
}
struct PositionUpdatableSystem {
// MARK: - Properties
typealias PositionUpdatableSprite = SKSpriteNode & PositionUpdatable
private(set) var updatables: [PositionUpdatableSprite]
var inputSource: InputSource
// MARK: - Initializers
init(inputSource: InputSource) {
self.inputSource = inputSource
updatables = []
}
// MARK: - Methods
mutating func add(updatable: PositionUpdatableSprite) {
updatables += [updatable]
}
mutating func update() {
for updatable in updatables {
updatable.position = inputSource.move(currentPosition: updatable.position)
}
}
}
protocol InputSource {
func move(currentPosition position: CGPoint) -> CGPoint
func rotate(currentAngle angle: CGFloat) -> CGFloat
}
struct CharacterMovement: InputSource {
func move(currentPosition position: CGPoint) -> CGPoint {
print("Character-specific logic for movement from position: ", position)
return .zero // implement character movement logic and return right CGPoint position
}
func rotate(currentAngle angle: CGFloat) -> CGFloat {
print("Character-specific rotatation from angle: ", angle)
return 0.0 // implement character rotation logic and return right CGFloat angle
}
}
class Scene: SKScene {
// MARK: - Properties
let player = Player(position: CGPoint(x: 50, y: 120), texture: SKTexture(imageNamed: "player.png"))
let bunny = BunnyEnemy(position: CGPoint(x: 140, y: 165), texture: SKTexture(imageNamed: "bunny-mad.png"), movementSpeed: 3.24)
let bunnyFat = BunnyEnemy(position: CGPoint(x: 180, y: 165), texture: SKTexture(imageNamed: "bunny-fat"), movementSpeed: 1.03)
var interactionSystem = InteractionSystem()
var positionUpdatableSystem: PositionUpdatableSystem?
var destructionSystem = DestructionSystem()
let characterMovementController = CharacterMovement()
// MARK: - Lifecycle
override func didMove(to view: SKView) {
super.didMove(to: view)
physicsWorld.contactDelegate = self
positionUpdatableSystem = PositionUpdatableSystem(inputSource: characterMovementController)
interactionSystem.add(interactable: bunny)
interactionSystem.add(interactable: bunnyFat)
positionUpdatableSystem?.add(updatable: player)
}
override func update(_ currentTime: TimeInterval) {
positionUpdatableSystem?.update()
// ...
// At some point, enemies are added to the Destruction System, this piece of code simulates that:
if currentTime == 3840578 {
destructionSystem.add(destructible: bunny)
destructionSystem.add(destructible: bunnyFat)
}
// Later on, a bunny killed our player, so we add the player to the Destruction System to be destroyed:
destructionSystem.add(destructible: player)
// At the end of the update loop we call the destroy method to delegate the destruction to the corresponding system
destructionSystem.destroy()
}
// MARK: - Touch handling
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
interactionSystem.touchesBegan(touches: touches, with: event)
}
}
extension Scene: SKPhysicsContactDelegate {
override func didSimulatePhysics() {
destructionSystem.destroy()
}
}