small racing game im working on
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
extends Resource
class_name TrackResource
@export_group("Sun position")

func night() -> bool:
	return sun_x < -180 || sun_x > -7

## Sun x rotation
@export_range(-360, 360) var sun_x := -90
## Sun y rotation ( its a game, the sun rotates around us )
@export_range(-360, 360) var sun_y := 0
@export_group("", "")
## The height of the overview cam
@export var overview_height := 300.0
## The name of this track
@export var name: String = "":
	set(n):
		if name != n:
			name = n
			name_changed.emit(name)
## Offset the entire track
@export var offset := Vector3.UP
@export_group("Race")
## Num laps, 1 = go to finish and done
@export var laps := 1

var builtin := false # i could (should) use class overrides and stuff but im tired

signal name_changed(name: String)
signal saved

## should look something like:
## [codeblock]
## [
##   TrackObject {
##      scene: "res://assets/blocks/platform.tscn",
##      transform: Transform3D(0, 0, 1, 0),
##      material: "platform",
##      ...more fields i come up with
##    }
##  ]
## [/codeblock]
var blocks: Array[TrackObject]

func _init(p_blocks: Array[TrackObject]) -> void:
	blocks = p_blocks

static func from_d(d: Dictionary) -> TrackResource:
	var blocs: Array[TrackObject] = []
	for block in d.b:
		blocs.append(TrackObject.from_d(block))
	var obj := TrackResource.new(blocs)
	obj.sun_x = d.x
	obj.sun_y = d.y
	obj.overview_height = d.h
	obj.name = d.n
	obj.offset = d.o
	obj.laps = d.l
	return obj

## Creates a [TrackResource] from a file
static func _load(path: String) -> TrackResource:
	var res := GhostData._load_file(path)
	if res.is_empty():
		return null
	return TrackResource.from_d(res)

func to_d() -> Dictionary:
	var b: Array[Dictionary] = [] # i know map() exists, but it didnt work
	for i in blocks:
		b.append(i.exprt())
	return {
		x = sun_x,
		y = sun_y,
		h = overview_height,
		n = name,
		o = offset,
		l = laps,
		b = b
	}

func save(path: String) -> void:
	if builtin:
		BuiltinTrackSelect.store_all()
	else:
		GhostData._save_file(path, to_d())
	saved.emit()

func get_aabb() -> AABB:
	var box := AABB()
	for block in blocks:
		if block.live_node:
			box = box.merge(block.live_node.get_aabb())
	return box

func dup() -> TrackResource:
	var blocks_dup: Array[TrackObject] = []
	for block in blocks:
		blocks_dup.append(block.dup())
	var o := TrackResource.new(blocks_dup)
	o.sun_x = sun_x
	o.sun_y = sun_y
	o.overview_height = overview_height
	o.name = name
	o.offset = offset
	o.laps = laps
	return o

func bytes() -> PackedByteArray:
	return var_to_bytes(to_d())