This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigComponent.swift
285 lines (219 loc) · 9.44 KB
/
ConfigComponent.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// ConfigComponent.swift
// Bomberman
//
// Created by Wolfgang Schreurs on 29/04/16.
//
//
import GameplayKit
import CoreGraphics
enum CreatureType {
case monster
case boss
case undefined
}
enum AttackDirection {
case axial // only along x-axis & y-axis
case radial
}
class ConfigComponent: GKComponent {
fileprivate(set) var configFilePath: String
fileprivate(set) var textureFile = String()
fileprivate(set) var spriteSize = CGSize()
// World unit size of the entity - used for sprites that are bigger than 1 world unit.
fileprivate(set) var wuSize = Size(width: 1, height: 1)
fileprivate(set) var speed: CGFloat = 1.0
fileprivate(set) var lives: Int = 0
fileprivate(set) var health: Int = 0
fileprivate(set) var hitDamage: Int = 1
// TODO: We need to modify spawn timers for bombs, therefore this property can be modified.
// Figure out cleaner solution.
var spawnAnimation = AnimationConfiguration()
fileprivate(set) var floatAnimation = AnimationConfiguration()
fileprivate(set) var destroyAnimation = AnimationConfiguration()
fileprivate(set) var hitAnimation = AnimationConfiguration()
fileprivate(set) var cheerAnimation = AnimationConfiguration()
fileprivate(set) var decayAnimation = AnimationConfiguration()
fileprivate(set) var moveAnimation = AnimationConfiguration()
fileprivate(set) var attackAnimation = AnimationConfiguration()
fileprivate(set) var destroySound: String?
fileprivate(set) var attackSound: String?
fileprivate(set) var spawnSound: String?
fileprivate(set) var hitSound: String?
fileprivate(set) var cheerSound: String?
fileprivate(set) var projectile: String?
// TODO: It's better for creatureType to be irrelevant. Adjusted mechanics should be
// configurable through the config file instead.
fileprivate(set) var creatureType: CreatureType = .undefined
fileprivate(set) var attackDirection: AttackDirection = .axial
fileprivate(set) var states: [State]?
fileprivate(set) var collisionCategories = EntityCategory.Nothing
// MARK: - Initialization
init(json: [String: AnyObject], configFileUrl: URL) {
self.configFilePath = configFileUrl.deletingLastPathComponent().path
super.init()
if let moveJson = json["movement"] as? [String: AnyObject] {
if let animationJson = moveJson["animation"] as? [String: AnyObject] {
self.moveAnimation = AnimationConfiguration(json: animationJson)
}
}
if let attackJson = json["attack"] as? [String: AnyObject] {
parseAttackJson(attackJson)
if let animationJson = attackJson["animation"] as? [String: AnyObject] {
self.attackAnimation = AnimationConfiguration(json: animationJson)
}
}
if let spriteJson = json["sprite"] as? [String: AnyObject] {
parseSpriteJson(spriteJson)
}
if let statesJson = json["states"] as? [String] {
parseStatesJson(statesJson)
}
if let creatureJson = json["creature"] as? [String: AnyObject] {
parseCreatureJson(creatureJson)
}
if let spawnJson = json["spawn"] as? [String: AnyObject] {
parseSpawnJson(spawnJson)
if let animationJson = spawnJson["animation"] as? [String: AnyObject] {
self.spawnAnimation = AnimationConfiguration(json: animationJson)
}
}
if let cheerJson = json["cheer"] as? [String: AnyObject] {
parseCheerJson(cheerJson)
if let animationJson = cheerJson["animation"] as? [String: AnyObject] {
self.cheerAnimation = AnimationConfiguration(json: animationJson)
}
}
if let hitJson = json["hit"] as? [String: AnyObject] {
parseHitJson(hitJson)
if let animationJson = hitJson["animation"] as? [String: AnyObject] {
self.hitAnimation = AnimationConfiguration(json: animationJson)
}
}
if let floatJson = json["float"] as? [String: AnyObject] {
if let animationJson = floatJson["animation"] as? [String: AnyObject] {
self.floatAnimation = AnimationConfiguration(json: animationJson)
}
}
if let decayJson = json["decay"] as? [String: AnyObject] {
if let animationJson = decayJson["animation"] as? [String: AnyObject] {
self.decayAnimation = AnimationConfiguration(json: animationJson)
}
}
if let destroyJson = json["destroy"] as? [String: AnyObject] {
parseDestroyJson(destroyJson)
if let animationJson = destroyJson["animation"] as? [String: AnyObject] {
self.destroyAnimation = AnimationConfiguration(json: animationJson)
}
}
if let collisionCategoriesJson = json["collisionCategories"] as? [String] {
parseCollisionCategoriesJson(collisionCategoriesJson)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Public
// MARK: - Private
fileprivate func parseStatesJson(_ json: [String]) {
var states = [State]()
for stateJson in json {
switch stateJson {
case "roam": states.append(MoveState())
case "destroy": states.append(DestroyState())
case "spawn": states.append(SpawnState())
case "attack": states.append(AttackState())
case "hit": states.append(HitState())
case "control": states.append(ControlState())
case "float": states.append(FloatState())
case "cheer": states.append(CheerState())
case "decay": states.append(DecayState())
default: print("unknown state: \(stateJson)")
}
}
if states.count > 0 {
self.states = states
}
}
fileprivate func parseCollisionCategoriesJson(_ json: [String]) {
self.collisionCategories = EntityCategory.categoriesForStrings(json)
}
fileprivate func parseDestroyJson(_ json: [String: AnyObject]) {
self.destroySound = soundFromJson(json)
}
fileprivate func parseHitJson(_ json: [String: AnyObject]) {
self.hitSound = soundFromJson(json)
self.hitDamage = json["damage"] as? Int ?? 1
}
fileprivate func parseCheerJson(_ json: [String: AnyObject]) {
self.cheerSound = soundFromJson(json)
}
fileprivate func parseSpawnJson(_ json: [String: AnyObject]) {
self.spawnSound = soundFromJson(json)
}
fileprivate func parseCreatureJson(_ json: [String: AnyObject]) {
if let livesJson = json["lives"] as? Int {
self.lives = max(livesJson - 1, 0) // 3 lives => life 0, life 1, life 2
}
if let healthJson = json["health"] as? Int {
self.health = max(healthJson, 0)
}
if let typeJson = json["type"] as? String {
switch typeJson {
case "monster": self.creatureType = .monster
case "boss": self.creatureType = .boss
default: self.creatureType = .undefined
}
}
}
fileprivate func parseSpriteJson(_ json: [String: AnyObject]) {
if let atlasJson = json["atlas"] as? String {
self.textureFile = atlasJson
}
if let speedJson = json["speed"] as? CGFloat {
self.speed = speedJson
}
if let width = json["width"] as? Int {
if let height = json["height"] as? Int {
self.spriteSize = CGSize(width: width, height: height)
}
}
let wu_width = json["wu_width"] as? Int
let wu_height = json["wu_height"] as? Int
self.wuSize = Size(width: wu_width ?? 1, height: wu_height ?? 1)
}
fileprivate func parseAttackJson(_ json: [String: AnyObject]) {
self.attackSound = soundFromJson(json)
if let projectileJson = json["projectile"] as? String {
self.projectile = projectileJson
}
if let directionJson = json["direction"] as? String {
var attackDirection: AttackDirection
switch directionJson {
case "any": attackDirection = .radial
case "axial": fallthrough
default: attackDirection = .axial
}
self.attackDirection = attackDirection
}
}
fileprivate func soundFromJson(_ json: [String: AnyObject]) -> String? {
var sound: String?
if let soundJson = json["sound"] as? String {
sound = soundJson
}
return sound
}
// Returned a CountableRange before.
fileprivate func animRangeFromJson(_ json: [String: AnyObject]) -> Range<Int> {
var range = Range(0 ..< 0)
if let animJson = json["anim"] as? [Int] {
if animJson.count == 2 {
let location = animJson[0]
let length = animJson[1]
range = Range(location ..< (location + length))
}
}
return range
}
}