-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMapScene.gd
859 lines (659 loc) · 24.8 KB
/
MapScene.gd
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
extends Node3D
class_name Map
const CURRENT_FORMAT_VERSION = 3
var StartScene = preload("res://Start.tscn")
@onready
var PropPlacer = preload("res://PropPlacer.tscn")
var trackPieces: Node3D
var checkPointSystem: Node3D
var props: Node3D
var trackName: String = "track_" + str(Time.get_datetime_string_from_system().replace(":", "-"))
var lapCount: int = 3
var trackId: String = ""
var author: String = ""
var autoSaveInterval: float = 12
@onready
# var roadMaterial = preload("res://Tracks/RacetrackMaterial.tres")
var materials = [
preload("res://Tracks/AsphaltMaterial.tres"), # ROAD
preload("res://Track Props/GrassMaterial.tres"), # GRASS
preload("res://Track Props/DirtMaterial.tres"), # DIRT
preload("res://Track Props/BoosterMaterial.tres"), # BOOSTER
preload("res://Track Props/BoosterMaterialReversed.tres") # REVERSE BOOSTER
]
var frictions = [
1.0, # ROAD
0.3, # GRASS
0.3, # DIRT
1.0, # BOOSTER
1.0 # REVERSE BOOSTER
]
var accelerationMultipliers = [
1.0, # ROAD
0.2, # GRASS
1.0, # DIRT
3.0, # BOOSTER
3.0 # REVERSE BOOSTER
]
var smokeParticles =[
true, # ROAD
true, # GRASS
false, # DIRT
true, # BOOSTER
true # REVERSE BOOSTER
]
var operationStack: Array = []
var lastOperationIndex: int = -1
const PLACED = 0
const REMOVED = 1
const UPDATED = 2
const PLACED_START = 3
const PLACED_CP = 4
const REMOVED_CP = 5
const UPDATED_CP = 6
const PLACED_PROP = 7
const REMOVED_PROP = 8
const UPDATED_PROP = 9
signal undidLastOperation()
signal redidLastOperation()
signal noOperationToBeUndone()
signal noOperationToBeRedone()
signal canUndo(value: bool)
signal canRedo(value: bool)
# signal loadFailed()
signal mapLoaded(trackName: String, lapCount: int)
signal loaded()
var mapLoadSuccess: bool = false
var start: Start
const START_MAGIC_VECTOR = Vector3(0.134, 1.224, -0.0788)
const START_OFFSET = Vector3(0, 9.15, 0)
# @export_file("*.json")
# var loadFrom: String:
# set(newFile):
# loadFrom = loadFromChanged(newFile)
# print("loadFrom changed to: ", loadFrom)
# # emit_signal("loadFromChanged", loadFrom)
# # load(loadFrom
# get:
# return loadFrom
# func loadFromChanged(newFile: String) -> String:
# if newFile == "":
# return newFile
# if is_node_ready():
# loadMap(newFile)
# return newFile
# return newFile
var connectionPoints: Array[Dictionary] = []
# Called when the node enters the scene tree for the first time.
func _ready():
trackPieces = %TrackPieces
checkPointSystem = %CheckPointSystem
props = %Props
if find_child("Start") != null:
start = find_child("Start")
else:
start = StartScene.instantiate()
add_child(start)
start.global_position = START_MAGIC_VECTOR
start.visible = false
# if loadFrom != "":
# loadMap("user://tracks/downloaded/65018d2f76dbc79627323b22.json") # hagyma
# loadMap("user://tracks/downloaded/6535a6a65e9a84298fc74678.json") # Flat Circuit
# loadMap("user://tracks/downloaded/650c73d0c3b8efa6383dde32.json") # Chamption's Track
# loadMap("user://tracks/downloaded/657382dee74a38eada2f8d54.json") # Experimental #1
setupAutoSave()
func setupAutoSave():
var timer = Timer.new()
timer.wait_time = autoSaveInterval * 60
timer.one_shot = false
add_child(timer)
timer.start()
timer.timeout.connect(autoSave)
func autoSave():
saveToJSON(true)
# add
func add(prefabMesher: PrefabMesher):
var prefab: PrefabProperties = prefabMesher.objectFromData()
addPrefab(prefab)
storeAddPrefab(prefab)
var editing: bool = false
func addPrefab(prefab: PrefabProperties, _prefabPosition: Vector3 = Vector3.INF, _prefabRotation: Vector3 = Vector3.INF):
trackPieces.add_child(prefab)
var prefabData = prefab.prefabData
# if prefabPosition != Vector3.INF:
# prefab.global_position = prefabPosition
# if prefabRotation != Vector3.INF:
# prefab.global_rotation = prefabRotation
# prefab.global_position = prefabData["position"]
# prefab.global_rotation = prefabData["rotation"]
prefab.global_position = Vector3(prefabData["positionX"], prefabData["positionY"], prefabData["positionZ"])
prefab.global_rotation = Vector3(0, prefabData["rotation"], 0)
prefab.mesh.surface_set_material(0, materials[prefabData["roadType"]])
prefab.friction = frictions[prefabData["roadType"]]
prefab.accelerationPenalty = accelerationMultipliers[prefabData["roadType"]]
prefab.smokeParticles = smokeParticles[prefabData["roadType"]]
prefab.create_trimesh_collision()
if editing:
var collider: CollisionShape3D = prefab.get_child(0).get_child(0)
collider.shape.backface_collision = true
setEditorCollision()
else:
setIngameCollision()
prefab.calculateCorners()
tryAddingConnectionPoints(prefab.topLeft, prefab.topRight, prefab.bottomLeft, prefab.bottomRight)
unvalidate()
func setEditorCollision():
%Scenery.setEditorCollision()
func setIngameCollision():
%Scenery.setIngameCollision()
func tryAddingConnectionPoints(topLeft: Vector3, topRight: Vector3, bottomLeft: Vector3, bottomRight: Vector3):
var frontFound = false
var backFound = false
for connectionPoint in connectionPoints:
if (connectionPoint["right"].is_equal_approx(topLeft) && connectionPoint["left"].is_equal_approx(topRight)):
connectionPoints.erase(connectionPoint)
frontFound = true
break
elif (connectionPoint["right"].is_equal_approx(bottomRight) && connectionPoint["left"].is_equal_approx(bottomLeft)):
connectionPoints.erase(connectionPoint)
backFound = true
break
if !frontFound:
connectionPoints.append({
"left": topLeft,
"right": topRight,
})
if !backFound:
connectionPoints.append({
"left": bottomRight,
"right": bottomLeft,
})
func storeAddPrefab(prefab: PrefabProperties):
safeResizeStack()
operationStack.append({
"prefab": prefab,
"operation": PLACED
})
lastOperationIndex += 1
emitUndoRedoSignals()
func addStart(startObject):
var oldPosition = start.global_position
var oldRotation = start.global_rotation
updateStartPosition(startObject.global_position + START_OFFSET, startObject.global_rotation)
# updateStartPosition(startObject.global_position, startObject.global_rotation)
storeAddStartObject(oldPosition, oldRotation, start.global_position, start.global_rotation)
func updateStartPosition(newPosition: Vector3, newRotation: Vector3):
start.global_position = newPosition
start.global_rotation = newRotation
start.visible = start.global_position != START_MAGIC_VECTOR
unvalidate()
func storeAddStartObject(oldPosition: Vector3, oldRotation: Vector3, newPosition: Vector3, newRotation: Vector3):
safeResizeStack()
operationStack.append({
"oldPosition": oldPosition,
"oldRotation": oldRotation,
"newPosition": newPosition,
"newRotation": newRotation,
"operation": PLACED_START
})
lastOperationIndex += 1
emitUndoRedoSignals()
func addCheckPoint(propPlacer: PropPlacer):
var checkPointObject = propPlacer.getCheckPointObject()
addCheckPointObject(checkPointObject, propPlacer.global_position, propPlacer.global_rotation)
storeAddCheckPointObject(checkPointObject, propPlacer.global_position, propPlacer.global_rotation)
func addCheckPointObject(checkPointObject: Area3D, checkPointPos: Vector3, checkPointRotation: Vector3):
checkPointSystem.add_child(checkPointObject)
checkPointObject.global_position = checkPointPos
checkPointObject.global_rotation = checkPointRotation
unvalidate()
func storeAddCheckPointObject(checkPointObject: Area3D, checkPointPos: Vector3, checkPointRotation: Vector3):
safeResizeStack()
operationStack.append({
"checkPointObject": checkPointObject,
"position": checkPointPos,
"rotation": checkPointRotation,
"operation": PLACED_CP
})
lastOperationIndex += 1
emitUndoRedoSignals()
func addProp(propPlacer: PropPlacer):
var propObject = propPlacer.getPropObject()
addPropObject(propObject, propPlacer.global_position, propPlacer.global_rotation)
storeAddPropObject(propObject, propPlacer.global_position, propPlacer.global_rotation)
func addPropObject(propObject: Node3D, propPos: Vector3, propRotation: Vector3):
props.add_child(propObject)
propObject.global_position = propPos
propObject.global_rotation = propRotation
unvalidate()
func storeAddPropObject(propObject: Node3D, propPos: Vector3, propRotation: Vector3):
safeResizeStack()
operationStack.append({
"propObject": propObject,
"position": propPos,
"rotation": propRotation,
"operation": PLACED_PROP
})
lastOperationIndex += 1
emitUndoRedoSignals()
# remove
func remove(prefab: PrefabProperties):
removePrefab(prefab)
storeRemovePrefab(prefab)
func removePrefab(prefab: PrefabProperties):
for child in prefab.get_children(true):
child.queue_free()
trackPieces.remove_child(prefab)
recalculateConnectionPoints()
unvalidate()
func storeRemovePrefab(prefab: PrefabProperties):
safeResizeStack()
operationStack.append({
"prefab": prefab,
"operation": REMOVED
})
lastOperationIndex += 1
emitUndoRedoSignals()
func removeStart():
var oldPosition = start.global_position
var oldRotation = start.global_rotation
updateStartPosition(START_MAGIC_VECTOR, Vector3.ZERO)
storeAddStartObject(oldPosition, oldRotation, start.global_position, start.global_rotation)
func removeCheckPoint(checkPointObject: Area3D):
storeRemoveCheckPointObject(checkPointObject, checkPointObject.global_position, checkPointObject.global_rotation)
removeCheckPointObject(checkPointObject)
func removeCheckPointObject(checkPointObject: Area3D):
checkPointSystem.remove_child(checkPointObject)
unvalidate()
func storeRemoveCheckPointObject(checkPointObject: Area3D, checkPointPos: Vector3, checkPointRotation: Vector3):
safeResizeStack()
operationStack.append({
"checkPointObject": checkPointObject,
"position": checkPointPos,
"rotation": checkPointRotation,
"operation": REMOVED_CP
})
lastOperationIndex += 1
emitUndoRedoSignals()
func removeProp(propObject: Node3D):
storeRemovePropObject(propObject, propObject.global_position, propObject.global_rotation)
removePropObject(propObject)
func removePropObject(propObject: Node3D):
props.remove_child(propObject)
unvalidate()
func storeRemovePropObject(propObject: Node3D, propPos: Vector3, propRotation: Vector3):
safeResizeStack()
operationStack.append({
"propObject": propObject,
"position": propPos,
"rotation": propRotation,
"operation": REMOVED_PROP
})
lastOperationIndex += 1
emitUndoRedoSignals()
# update
func update(oldPrefab: PrefabProperties, prefabMesher: PrefabMesher):
var prefab = prefabMesher.objectFromData()
if oldPrefab.equals(prefab):
return
removePrefab(oldPrefab)
addPrefab(prefab, prefabMesher.position, prefabMesher.rotation)
storeUpdatePrefab(oldPrefab, prefab)
func storeUpdatePrefab(oldPrefab: PrefabProperties, newPrefab: PrefabProperties):
safeResizeStack()
operationStack.append({
"oldPrefab": oldPrefab,
"newPrefab": newPrefab,
"operation": UPDATED
})
lastOperationIndex += 1
emitUndoRedoSignals()
func updateCheckPoint(oldCheckPointObject: Area3D, propPlacer: PropPlacer):
# var checkPointObject = propPlacer.getCheckPointObject()
# flipping order, because the old object is the one that is already in the scene
storeUpdateCheckPointObject(oldCheckPointObject, propPlacer.global_position, propPlacer.global_rotation)
updateCheckPointObject(oldCheckPointObject, propPlacer.global_position, propPlacer.global_rotation)
func updateCheckPointObject(checkPointObject: Area3D, checkPointPos: Vector3, checkPointRotation: Vector3):
checkPointObject.global_position = checkPointPos
checkPointObject.global_rotation = checkPointRotation
unvalidate()
func storeUpdateCheckPointObject(checkPointObject: Area3D, checkPointPos: Vector3, checkPointRotation: Vector3):
safeResizeStack()
operationStack.append({
"checkPointObject": checkPointObject,
"oldPosition": checkPointObject.global_position,
"oldRotation": checkPointObject.global_rotation,
"newPosition": checkPointPos,
"newRotation": checkPointRotation,
"operation": UPDATED_CP
})
lastOperationIndex += 1
emitUndoRedoSignals()
func updateProp(oldPropObject: Node3D, propPlacer: PropPlacer):
# var propObject = propPlacer.getPropObject()
# flipping order, because the old object is the one that is already in the scene
var textureName = propPlacer.currentBillboardTexture
# var texture = propPlacer.billboardTextures[max(0, index)]
var texture = TextureLoader.billboardTextures[textureName if textureName != "Custom" else "PipeRacerLanguages"]
var url = propPlacer.currentImageUrl
storeUpdatePropObject(oldPropObject, propPlacer.global_position, propPlacer.global_rotation, textureName, texture, url)
updatePropObject(oldPropObject, propPlacer.global_position, propPlacer.global_rotation, textureName, texture, url)
func updatePropObject(propObject: Node3D, propPos: Vector3, propRotation: Vector3, textureName: String, texture: Texture, url: String):
propObject.global_position = propPos
propObject.global_rotation = propRotation
propObject.setTexture(texture, textureName, url)
unvalidate()
func storeUpdatePropObject(propObject: Node3D, propPos: Vector3, propRotation: Vector3, textureName: String, texture: Texture, url: String):
safeResizeStack()
operationStack.append({
"propObject": propObject,
"oldPosition": propObject.global_position,
"oldRotation": propObject.global_rotation,
"oldTextureName": propObject.billboardTextureName,
"oldTexture": propObject.billboardTexture,
"oldImageUrl": propObject.billboardTextureUrl,
"newPosition": propPos,
"newRotation": propRotation,
"newTextureName": textureName,
"newTexture": texture,
"newImageUrl": url,
"operation": UPDATED_PROP
})
lastOperationIndex += 1
emitUndoRedoSignals()
# misc
func recalculateConnectionPoints():
connectionPoints.clear()
for child in trackPieces.get_children():
var prefab = child
tryAddingConnectionPoints(prefab.topLeft, prefab.topRight, prefab.bottomLeft, prefab.bottomRight)
# operatioin handler
func safeResizeStack():
for i in range(lastOperationIndex + 1, operationStack.size()):
var operation = operationStack[i]
match operation["operation"]:
# this prefab will be lost after resize
PLACED:
operation["prefab"].queue_free()
# REMOVED: no need to do anything
# new prefab will never be needed
UPDATED:
operation["newPrefab"].queue_free()
# PLACED_START: no need to do anything
# this checkpoint will be lost after resize
PLACED_CP:
operation["checkPointObject"].queue_free()
# REMOVED_CP: no need to do anything
# UPDATED_CP: no need to do anything
PLACED_PROP:
operation["propObject"].queue_free()
# REMOVED_PROP: no need to do anything
# UPDATED_PROP: no need to do anything
if operationStack.size() > lastOperationIndex + 1:
operationStack.resize(lastOperationIndex + 1)
func ableToUndo():
return lastOperationIndex >= 0
func ableToRedo():
return lastOperationIndex < operationStack.size() - 1
func undo():
if !ableToUndo():
emitUndoRedoSignals()
noOperationToBeUndone.emit()
return
var lastOperation = operationStack[lastOperationIndex]
if lastOperation["operation"] == PLACED:
# for child in lastOperation["prefab"].get_children(true):
# child.queue_free()
# trackPieces.remove_child(lastOperation["prefab"])
removePrefab(lastOperation["prefab"])
elif lastOperation["operation"] == REMOVED:
addPrefab(lastOperation["prefab"])
elif lastOperation["operation"] == UPDATED:
removePrefab(lastOperation["newPrefab"])
addPrefab(lastOperation["oldPrefab"])
elif lastOperation["operation"] == PLACED_START:
updateStartPosition(lastOperation["oldPosition"], lastOperation["oldRotation"])
elif lastOperation["operation"] == PLACED_CP:
removeCheckPointObject(lastOperation["checkPointObject"])
elif lastOperation["operation"] == REMOVED_CP:
addCheckPointObject(lastOperation["checkPointObject"], lastOperation["position"], lastOperation["rotation"])
elif lastOperation["operation"] == UPDATED_CP:
updateCheckPointObject(lastOperation["checkPointObject"], lastOperation["oldPosition"], lastOperation["oldRotation"])
elif lastOperation["operation"] == PLACED_PROP:
removePropObject(lastOperation["propObject"])
elif lastOperation["operation"] == REMOVED_PROP:
addPropObject(lastOperation["propObject"], lastOperation["position"], lastOperation["rotation"])
elif lastOperation["operation"] == UPDATED_PROP:
updatePropObject(lastOperation["propObject"], lastOperation["oldPosition"], lastOperation["oldRotation"], lastOperation["oldTextureName"], lastOperation["oldTexture"], lastOperation["oldImageUrl"])
else:
return
lastOperationIndex -= 1
undidLastOperation.emit()
emitUndoRedoSignals()
func redo():
if !ableToRedo():
emitUndoRedoSignals()
noOperationToBeRedone.emit()
return
lastOperationIndex += 1
var lastOperation = operationStack[lastOperationIndex]
if lastOperation["operation"] == PLACED:
addPrefab(lastOperation["prefab"])
elif lastOperation["operation"] == REMOVED:
removePrefab(lastOperation["prefab"])
elif lastOperation["operation"] == UPDATED:
removePrefab(lastOperation["oldPrefab"])
addPrefab(lastOperation["newPrefab"])
elif lastOperation["operation"] == PLACED_START:
updateStartPosition(lastOperation["newPosition"], lastOperation["newRotation"])
elif lastOperation["operation"] == PLACED_CP:
addCheckPointObject(lastOperation["checkPointObject"], lastOperation["position"], lastOperation["rotation"])
elif lastOperation["operation"] == REMOVED_CP:
removeCheckPointObject(lastOperation["checkPointObject"])
elif lastOperation["operation"] == UPDATED_CP:
updateCheckPointObject(lastOperation["checkPointObject"], lastOperation["newPosition"], lastOperation["newRotation"])
elif lastOperation["operation"] == PLACED_PROP:
addPropObject(lastOperation["propObject"], lastOperation["position"], lastOperation["rotation"])
elif lastOperation["operation"] == REMOVED_PROP:
removePropObject(lastOperation["propObject"])
elif lastOperation["operation"] == UPDATED_PROP:
updatePropObject(lastOperation["propObject"], lastOperation["newPosition"], lastOperation["newRotation"], lastOperation["newTextureName"], lastOperation["newTexture"], lastOperation["newImageUrl"])
else:
lastOperationIndex -= 1
return
redidLastOperation.emit()
emitUndoRedoSignals()
func clearMap():
for child in trackPieces.get_children():
child.queue_free()
for child in checkPointSystem.get_children():
child.queue_free()
for child in props.get_children():
child.queue_free()
# trackPieces.get_child(0).queue_free()
# checkPointSystem.get_child(0).queue_free()
start.visible = false
operationStack.clear()
lastOperationIndex = -1
func save():
saveToJSON()
var validated: bool = false
var bestTotalTime: int = -1
var bestTotalReplay: String = ""
var bestLapTime: int = -1
var bestLapReplay: String = ""
func unvalidate():
validated = false
bestTotalTime = -1
bestTotalReplay = ""
bestLapTime = -1
bestLapReplay = ""
func saveToJSON(autosave: bool = false):
var trackData = {
"format": CURRENT_FORMAT_VERSION,
"trackName": trackName,
"lapCount": lapCount,
"trackPieces": [],
"start": {
# "position": start.global_position,
# "rotation": start.global_rotation
"positionX": start.global_position.x,
"positionY": start.global_position.y,
"positionZ": start.global_position.z,
# "rotationX": start.global_rotation.x,
# "rotationY": start.global_rotation.y,
# "rotationZ": start.global_rotation.z
"rotation": start.global_rotation.y
},
"checkPoints": [],
"props": [],
"validated": validated,
"bestTotalTime": bestTotalTime,
"bestTotalReplay": bestTotalReplay,
"bestLapTime": bestLapTime,
"bestLapReplay": bestLapReplay,
# "author": GlobalProperties.PLAYER_NAME
}
for child in trackPieces.get_children():
# var prefabData = child.prefabData
trackData["trackPieces"].append(child.prefabData)
for child in checkPointSystem.get_children():
trackData["checkPoints"].append({
# "position": child.global_position,
# "rotation": child.global_rotation
"positionX": child.global_position.x,
"positionY": child.global_position.y,
"positionZ": child.global_position.z,
# "rotationX": child.global_rotation.x,
# "rotationY": child.global_rotation.y,
# "rotationZ": child.global_rotation.z
"rotation": child.global_rotation.y
})
for child in props.get_children():
trackData["props"].append({
# "position": child.global_position,
# "rotation": child.global_rotation
"positionX": child.global_position.x,
"positionY": child.global_position.y,
"positionZ": child.global_position.z,
# "rotationX": child.global_rotation.x,
# "rotationY": child.global_rotation.y,
# "rotationZ": child.global_rotation.z
"rotation": child.global_rotation.y,
"textureName": child.billboardTextureName,
"imageUrl": child.billboardTextureUrl
})
# var fileHandler = FileAccess.new()
var path = "user://tracks/local/" + trackName + ".json"
if autosave:
path = "user://tracks/autosave/" + trackName + "_" + str(Time.get_datetime_string_from_system().replace(":", "-")) + ".json"
var fileHandler = FileAccess.open(path, FileAccess.WRITE)
if fileHandler == null:
print("Error opening file to save into")
return
fileHandler.store_string(JSON.stringify(trackData, "\t"))
func loadMap(fileName: String) -> bool:
if fileName.split(".")[-1] == "json":
return loadFromJSON(fileName)
return false
func loadFromJSON(fileName: String) -> bool:
var path = fileName
if !fileName.begins_with("user://tracks/local/") && !fileName.begins_with("user://tracks/downloaded/"):
path = "user://tracks/local/" + fileName
if fileName.begins_with("user://tracks/downloaded/"):
trackId = fileName.split("/")[-1].split(".")[0]
else:
trackId = ""
var fileHandler = FileAccess.open(path, FileAccess.READ)
if fileHandler == null:
print("Error opening file to load from", path)
return false
var trackData = JSON.parse_string(fileHandler.get_as_text())
if trackData == null:
print("Error parsing JSON when loading map")
return false
if trackData.has("author"):
author = trackData["author"]
else:
author = ""
if trackData["format"] != CURRENT_FORMAT_VERSION:
print("Error loading map: wrong format version")
if !path.begins_with("user://tracks/downloaded/"):
fileHandler.close()
MapUpdater.updateMap(trackData, path)
fileHandler = FileAccess.open(path, FileAccess.READ)
trackData = JSON.parse_string(fileHandler.get_as_text())
else:
return false
clearMap()
trackName = trackData["trackName"]
lapCount = trackData["lapCount"]
if !trackData.has("trackPieces"):
print("Error loading map: no trackPieces")
return false
var prefabMesher = PrefabMesher.new()
add_child(prefabMesher)
for prefabData in trackData["trackPieces"]:
var prefab = prefabMesher.objectFromData(prefabData)
addPrefab(prefab)
prefabMesher.queue_free()
if !trackData.has("start"):
print("Error loading map: no start")
return false
var startPosData = trackData["start"]
updateStartPosition(Vector3(startPosData["positionX"], startPosData["positionY"], startPosData["positionZ"]), Vector3(0, startPosData["rotation"], 0))
if !trackData.has("checkPoints"):
print("Error loading map: no checkPoints")
return false
var propPlacer = PropPlacer.instantiate()
add_child(propPlacer)
var checkpointIndex = 0
for checkPointData in trackData["checkPoints"]:
var checkPointObject = propPlacer.getCheckPointObject()
checkPointObject.index = checkpointIndex
addCheckPointObject(checkPointObject, Vector3(checkPointData["positionX"], checkPointData["positionY"], checkPointData["positionZ"]), Vector3(0, checkPointData["rotation"], 0))
checkpointIndex += 1
if trackData.has("props"):
for propData in trackData["props"]:
var imageUrl = propData["imageUrl"] if propData.has("imageUrl") else ""
var propObject = propPlacer.getPropObject(propData["textureName"], imageUrl)
addPropObject(propObject, Vector3(propData["positionX"], propData["positionY"], propData["positionZ"]), Vector3(0, propData["rotation"], 0))
else:
print("No props on this track")
propPlacer.queue_free()
print("Loaded track: " + trackData["trackName"])
# load validation details
validated = false
if trackData.has("validated"):
validated = trackData["validated"]
bestTotalTime = trackData["bestTotalTime"]
bestTotalReplay = trackData["bestTotalReplay"]
bestLapTime = trackData["bestLapTime"]
bestLapReplay = trackData["bestLapReplay"]
# mapLoadSuccess = true
# loaded.emit()
mapLoaded.emit(trackData["trackName"], trackData["lapCount"])
return true
func getCheckpoints():
return checkPointSystem.get_children()
func getCheckpointCount():
return checkPointSystem.get_child_count()
func getConnectionPoints() -> Array[Dictionary]:
return connectionPoints
func setIngame():
# %Scenery/%Proper.disabled = false
# %Scenery/%Flat.disabled = true
%Scenery.setIngameCollision()
func emitUndoRedoSignals():
canUndo.emit(ableToUndo())
canRedo.emit(ableToRedo())
func setNewValidationTime(totalTime: int, bestLap: int, replay: String):
validated = true
if bestTotalTime == -1 || totalTime < bestTotalTime:
bestTotalTime = totalTime
bestTotalReplay = replay
save()
if bestLapTime == -1 || bestLap < bestLapTime:
bestLapTime = bestLap
bestLapReplay = replay
save()