Skip to content

Commit

Permalink
Polished code sample
Browse files Browse the repository at this point in the history
  • Loading branch information
eleev committed Sep 7, 2018
1 parent 20ed6f3 commit d7d529d
Showing 1 changed file with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import SpriteKit

protocol Interactable { /* empty */ }
protocol Destructable { /* empty */ }
protocol Destructible { /* empty */ }
protocol PositionUpdatable { /* empty */ }

class BunnyEnemy: SKSpriteNode, Interactable, Destructable, PositionUpdatable {
class BunnyEnemy: SKSpriteNode, Interactable, Destructible {

// MARK: - Properties

Expand All @@ -24,7 +24,7 @@ class BunnyEnemy: SKSpriteNode, Interactable, Destructable, PositionUpdatable {
}
}

class Player: SKSpriteNode, PositionUpdatable {
class Player: SKSpriteNode, Destructible, PositionUpdatable {

// MARK: - Initializers

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

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]
}
Expand All @@ -62,12 +68,16 @@ struct InteractionSystem {

struct DestructionSystem {

typealias DestructableSprite = SKSpriteNode & Destructable
// MARK: - Properties

typealias DestructibleSprite = SKSpriteNode & Destructible

private(set) var destructables: [DestructableSprite]
private(set) var destructibles: [DestructibleSprite]

// MARK: - Initializers

init() {
destructables = []
destructibles = []
}

private lazy var animationSequence: SKAction = {
Expand All @@ -77,29 +87,37 @@ struct DestructionSystem {
return sequence
}()

mutating func add(destructable: DestructableSprite) {
destructables += [destructable]
// MARK: - Methods

mutating func add(destructible: DestructibleSprite) {
destructibles += [destructible]
}

mutating func destroy() {
for destructable in destructables {
destructable.run(animationSequence)
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]
}
Expand Down Expand Up @@ -168,9 +186,15 @@ class Scene: SKScene {
// ...
// At some point, enemies are added to the Destruction System, this piece of code simulates that:
if currentTime == 3840578 {
destructionSystem.add(destructable: bunny)
destructionSystem.add(destructable: bunnyFat)
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
Expand Down

0 comments on commit d7d529d

Please sign in to comment.