/
Contents.swift
54 lines (38 loc) 路 1.34 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
import Foundation
import SpriteKit
protocol Prototype {
func clone() -> Prototype
}
enum PatrolBehaviorType {
case land
case air
case underground
}
class AIPatrolCharacter: Prototype {
// MARK: - Properties
private(set) var name: String
private var sprite: SKSpriteNode?
var patrolBehaviorType: PatrolBehaviorType
// MARK: - Initializers
init(name: String, sprite: SKSpriteNode?, patrolBehaviorType: PatrolBehaviorType) {
self.name = name
self.sprite = sprite
self.patrolBehaviorType = patrolBehaviorType
}
// MARK: - Conformance to Prototype prtocol
func clone() -> Prototype {
return AIPatrolCharacter(name: name, sprite: sprite, patrolBehaviorType: patrolBehaviorType)
}
}
extension AIPatrolCharacter: CustomStringConvertible {
var description: String {
return "name: \(name), sprite: \(sprite as Any), patrol behavior type: \(patrolBehaviorType)"
}
}
//: Usage:
let landPatroller = AIPatrolCharacter(name: "Patroller", sprite: SKSpriteNode(fileNamed: "Patroller.png"), patrolBehaviorType: .land)
print("Land Patroller: ", landPatroller)
let airPatroller = landPatroller.clone() as? AIPatrolCharacter
airPatroller?.patrolBehaviorType = .air
print("Air Patroller: ", airPatroller as Any)
print("Land Patroller: ", landPatroller)