/
Structs.swift
256 lines (216 loc) · 6.76 KB
/
Structs.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
//
// Structs.swift
// Aeon Garden
//
// Created by Brad Root on 10/4/19.
// Copyright © 2019 Brad Root. All rights reserved.
//
import Foundation
import SpriteKit
protocol DataStoreProtocol {
func saveTank(_ tank: Tank)
func getTanks(completion: @escaping ([Tank]) -> Void)
func saveCreature(_ creature: Creature)
func deleteCreature(_ creature: Creature)
func getCreatures(completion: @escaping ([Creature]) -> Void)
}
struct TankSettings {
let foodMaxAmount: Int
let foodHealthRestorationBaseValue: Float
let foodSpawnRate: Int
let creatureInitialAmount: Int
let creatureMinimumAmount: Int
let creatureSpawnRate: Int
let creatureBirthSuccessRate: Float
let backgroundParticleBirthrate: Int
let backgroundParticleLifetime: Int
}
struct Tank {
let uuid: UUID
let tankTime: Float
let deathCount: Int
let birthCount: Int
let tankSettings: TankSettings
let creatures: [Creature]
let food: [Food]
let bubbles: [Bubble]
static func from(_ scene: AeonTankScene) -> Tank {
guard let tankSettings = scene.tankSettings else { fatalError("Tank cannot lack TankSettings.") }
var creatures: [Creature] = []
for creatureNode in scene.creatureNodes {
creatures.append(Creature.from(creatureNode))
}
var food: [Food] = []
for foodNode in scene.foodNodes {
food.append(Food.from(foodNode))
}
var bubbles: [Bubble] = []
for bubbleNode in scene.bubbleNodes {
bubbles.append(Bubble.from(bubbleNode))
}
return Tank(
uuid: scene.uuid,
tankTime: Float(scene.tankTime),
deathCount: scene.deathCount,
birthCount: scene.birthCount,
tankSettings: tankSettings,
creatures: creatures,
food: food,
bubbles: bubbles
)
}
func restore(to scene: AeonTankScene) {
scene.tankSettings = tankSettings
scene.tankTime = TimeInterval(tankTime)
scene.deathCount = deathCount
scene.birthCount = birthCount
scene.uuid = uuid
for creature in creatures {
creature.put(in: scene)
}
for food in food {
food.put(in: scene)
}
for bubble in bubbles {
bubble.put(in: scene)
}
}
func save() {
#if os(iOS) || os(tvOS)
CoreDataStore.standard.saveTank(self)
#endif
}
static func getAll(completion: @escaping ([Tank]) -> Void) {
#if os(iOS) || os(tvOS)
CoreDataStore.standard.getTanks { (tanks) in
completion(tanks)
}
#else
completion([])
#endif
}
}
struct Creature {
let uuid: UUID
let firstName: String
let lastName: String
let limbs: [Limb]
let movementSpeed: Float
let turnSpeed: Float
let sizeModifier: Float
let primaryHue: Float
var isFavorite: Bool
var lifeTime: Float
var currentHealth: Float
let positionX: Float
let positionY: Float
static func from(_ node: AeonCreatureNode) -> Creature {
var limbs: [Limb] = []
let limbNodes: [AeonLimbNode] = [node.limbOne, node.limbTwo, node.limbThree, node.limbFour]
for limbNode in limbNodes {
limbs.append(Limb.from(limbNode))
}
return Creature(
uuid: node.uuid,
firstName: node.firstName,
lastName: node.lastName,
limbs: limbs,
movementSpeed: Float(node.movementSpeed),
turnSpeed: Float(node.turnSpeed),
sizeModifier: Float(node.sizeModififer),
primaryHue: Float(node.primaryHue),
isFavorite: node.isFavorite,
lifeTime: Float(node.lifeTime),
currentHealth: Float(node.currentHealth),
positionX: Float(node.position.x),
positionY: Float(node.position.y)
)
}
func toNode() -> AeonCreatureNode {
let creatureNode = AeonCreatureNode(with: self)
return creatureNode
}
func put(in scene: AeonTankScene) {
let creatureNode = AeonCreatureNode(with: self)
scene.addChild(creatureNode)
creatureNode.position = CGPoint(
x: CGFloat(positionX),
y: CGFloat(positionY)
)
creatureNode.born()
}
}
struct Limb {
let shape: BodyPart
let hue: Float
let blend: Float
let brightness: Float
let saturation: Float
let limbWidth: Int
let wiggleFactor: Float
let wiggleMoveFactor: Float
let wiggleMoveBackFactor: Float
let wiggleActionDuration: Float
let wiggleActionBackDuration: Float
let wiggleActionMovementDuration: Float
let wiggleActionMovementBackDuration: Float
let limbzRotation: Float
let positionX: Float
let positionY: Float
static func from(_ node: AeonLimbNode) -> Limb {
return Limb(
shape: node.shape,
hue: Float(node.hue),
blend: Float(node.blend),
brightness: Float(node.brightness),
saturation: Float(node.saturation),
limbWidth: node.limbWidth,
wiggleFactor: Float(node.wiggleFactor),
wiggleMoveFactor: Float(node.wiggleMoveFactor),
wiggleMoveBackFactor: Float(node.wiggleMoveBackFactor),
wiggleActionDuration: Float(node.wiggleActionDuration),
wiggleActionBackDuration: Float(node.wiggleActionBackDuration),
wiggleActionMovementDuration: Float(node.wiggleActionMovementDuration),
wiggleActionMovementBackDuration: Float(node.wiggleActionMovementBackDuration),
limbzRotation: Float(node.limbzRotation),
positionX: Float(node.position.x),
positionY: Float(node.position.y)
)
}
}
struct Food {
let positionX: Float
let positionY: Float
static func from(_ node: AeonFoodNode) -> Food {
return Food(
positionX: Float(node.position.x),
positionY: Float(node.position.y)
)
}
func put(in scene: AeonTankScene) {
let foodNode = AeonFoodNode()
scene.addChild(foodNode)
foodNode.position = CGPoint(
x: CGFloat(positionX),
y: CGFloat(positionY)
)
}
}
struct Bubble {
let positionX: Float
let positionY: Float
static func from(_ node: AeonBubbleNode) -> Bubble {
return Bubble(
positionX: Float(node.position.x),
positionY: Float(node.position.y)
)
}
func put(in scene: AeonTankScene) {
let bubbleNode = AeonBubbleNode()
scene.addChild(bubbleNode)
bubbleNode.position = CGPoint(
x: CGFloat(positionX),
y: CGFloat(positionY)
)
}
}