-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameCommand.swift
301 lines (258 loc) · 8.4 KB
/
GameCommand.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
Representations for game events, related data, and their encoding.
*/
import Foundation
import simd
import SceneKit
/// - Tag: GameCommand
struct GameCommand {
var player: Player?
var action: Action
}
extension float3: BitStreamCodable {
init(from bitStream: inout ReadableBitStream) throws {
let x = try bitStream.readFloat()
let y = try bitStream.readFloat()
let z = try bitStream.readFloat()
self.init(x, y, z)
}
func encode(to bitStream: inout WritableBitStream) {
bitStream.appendFloat(x)
bitStream.appendFloat(y)
bitStream.appendFloat(z)
}
}
extension float4: BitStreamCodable {
init(from bitStream: inout ReadableBitStream) throws {
let x = try bitStream.readFloat()
let y = try bitStream.readFloat()
let z = try bitStream.readFloat()
let w = try bitStream.readFloat()
self.init(x, y, z, w)
}
func encode(to bitStream: inout WritableBitStream) {
bitStream.appendFloat(x)
bitStream.appendFloat(y)
bitStream.appendFloat(z)
bitStream.appendFloat(w)
}
}
extension float4x4: BitStreamCodable {
init(from bitStream: inout ReadableBitStream) throws {
self.init()
self.columns.0 = try float4(from: &bitStream)
self.columns.1 = try float4(from: &bitStream)
self.columns.2 = try float4(from: &bitStream)
self.columns.3 = try float4(from: &bitStream)
}
func encode(to bitStream: inout WritableBitStream) {
columns.0.encode(to: &bitStream)
columns.1.encode(to: &bitStream)
columns.2.encode(to: &bitStream)
columns.3.encode(to: &bitStream)
}
}
extension String: BitStreamCodable {
init(from bitStream: inout ReadableBitStream) throws {
let data = try bitStream.readData()
if let value = String(data: data, encoding: .utf8) {
self = value
} else {
throw BitStreamError.encodingError
}
}
func encode(to bitStream: inout WritableBitStream) throws {
if let data = data(using: .utf8) {
bitStream.append(data)
} else {
throw BitStreamError.encodingError
}
}
}
enum GameBoardLocation: BitStreamCodable {
case worldMapData(Data)
case manual
enum CodingKey: UInt32, CaseIterable {
case worldMapData
case manual
}
init(from bitStream: inout ReadableBitStream) throws {
let key: CodingKey = try bitStream.readEnum()
switch key {
case .worldMapData:
let data = try bitStream.readData()
self = .worldMapData(data)
case .manual:
self = .manual
}
}
func encode(to bitStream: inout WritableBitStream) {
switch self {
case .worldMapData(let data):
bitStream.appendEnum(CodingKey.worldMapData)
bitStream.append(data)
case .manual:
bitStream.appendEnum(CodingKey.manual)
}
}
}
enum BoardSetupAction: BitStreamCodable {
case requestBoardLocation
case boardLocation(GameBoardLocation)
enum CodingKey: UInt32, CaseIterable {
case requestBoardLocation
case boardLocation
}
init(from bitStream: inout ReadableBitStream) throws {
let key: CodingKey = try bitStream.readEnum()
switch key {
case .requestBoardLocation:
self = .requestBoardLocation
case .boardLocation:
let location = try GameBoardLocation(from: &bitStream)
self = .boardLocation(location)
}
}
func encode(to bitStream: inout WritableBitStream) {
switch self {
case .requestBoardLocation:
bitStream.appendEnum(CodingKey.requestBoardLocation)
case .boardLocation(let location):
bitStream.appendEnum(CodingKey.boardLocation)
location.encode(to: &bitStream)
}
}
}
struct GameVelocity {
var vector: float3
static var zero: GameVelocity { return GameVelocity(vector: float3()) }
}
extension GameVelocity: BitStreamCodable {
init(from bitStream: inout ReadableBitStream) throws {
vector = try float3(from: &bitStream)
}
func encode(to bitStream: inout WritableBitStream) {
vector.encode(to: &bitStream)
}
}
private let velocityCompressor = FloatCompressor(minValue: -50.0, maxValue: 50.0, bits: 16)
private let angularVelocityAxisCompressor = FloatCompressor(minValue: -1.0, maxValue: 1.0, bits: 12)
//struct MoveData {
// var velocity: float3
// var angular: Float
//
// init(velocity: float3, angular: Float) {
// self.velocity = velocity
// self.angular = angular
// }
//}
//
//extension MoveData: BitStreamCodable {
// init(from bitStream: inout ReadableBitStream) throws {
// velocity = try velocityCompressor.readFloat3(from: &bitStream)
// angular = try angularVelocityAxisCompressor.read(from: &bitStream)
// }
//
// func encode(to bitStream: inout WritableBitStream) throws {
// velocityCompressor.write(velocity, to: &bitStream)
// angularVelocityAxisCompressor.write(angular, to: &bitStream)
// }
//}
struct MoveData {
var velocity: GameVelocity
var angular: Float
}
extension MoveData: BitStreamCodable {
init(from bitStream: inout ReadableBitStream) throws {
velocity = try GameVelocity(from: &bitStream)
angular = try bitStream.readFloat()
}
func encode(to bitStream: inout WritableBitStream) throws {
velocity.encode(to: &bitStream)
bitStream.appendFloat(angular)
}
}
struct AddTankNodeAction {
var simdWorldTransform: float4x4
var eulerAngles: float3
}
extension AddTankNodeAction: BitStreamCodable {
init(from bitStream: inout ReadableBitStream) throws {
simdWorldTransform = try float4x4(from: &bitStream)
eulerAngles = try float3(from: &bitStream)
}
func encode(to bitStream: inout WritableBitStream) throws {
simdWorldTransform.encode(to: &bitStream)
eulerAngles.encode(to: &bitStream)
}
}
enum GameAction {
case joyStickMoved(MoveData)
case movement(MovementSyncData)
private enum CodingKey: UInt32, CaseIterable {
case move
// case fire
}
}
extension GameAction: BitStreamCodable {
func encode(to bitStream: inout WritableBitStream) throws {
// switch game action
switch self {
case .joyStickMoved(let data):
bitStream.appendEnum(CodingKey.move)
try data.encode(to: &bitStream)
case .movement(let data):
bitStream.appendEnum(CodingKey.move)
try data.encode(to: &bitStream)
}
}
init(from bitStream: inout ReadableBitStream) throws {
let key: CodingKey = try bitStream.readEnum()
switch key {
case .move:
let data = try MoveData(from: &bitStream)
self = .joyStickMoved(data)
}
}
}
enum Action {
case gameAction(GameAction)
case boardSetup(BoardSetupAction)
case addTank(AddTankNodeAction)
}
extension Action: BitStreamCodable {
private enum CodingKey: UInt32, CaseIterable {
case gameAction
case boardSetup
case addTank
}
func encode(to bitStream: inout WritableBitStream) throws {
switch self {
case .gameAction(let gameAction):
bitStream.appendEnum(CodingKey.gameAction)
try gameAction.encode(to: &bitStream)
case .boardSetup(let boardSetup):
bitStream.appendEnum(CodingKey.boardSetup)
boardSetup.encode(to: &bitStream)
case .addTank(let addTankAction):
bitStream.appendEnum(CodingKey.addTank)
try addTankAction.encode(to: &bitStream)
}
}
init(from bitStream: inout ReadableBitStream) throws {
let code: CodingKey = try bitStream.readEnum()
switch code {
case .gameAction:
let gameAction = try GameAction(from: &bitStream)
self = .gameAction(gameAction)
case .boardSetup:
let boardAction = try BoardSetupAction(from: &bitStream)
self = .boardSetup(boardAction)
case .addTank:
let addTankAction = try AddTankNodeAction(from: &bitStream)
self = .addTank(addTankAction)
}
}
}