-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack.gd
117 lines (87 loc) · 2.72 KB
/
track.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
extends Node2D
const C = 0.551915024494
const BASE_SIZE = 100.0
var segment_scene = preload("segment.tscn")
var next_segments = []
# Called when the node enters the scene tree for the first time.
func _ready():
add_segment(straight(BASE_SIZE))
generate_segments()
func create_loop(s):
return [
straight(2*s),
curve_right_up(s),
curve_up_left(s),
curve_left_down(s),
curve_down_right(s),
straight(2*s)
]
func add_segment(segment:Segment):
var last_point = Vector2.ZERO
if get_child_count() != 0:
var last_segment = get_last_segment()
last_segment.next_segment = segment
segment.prev_segment = last_segment
last_point = last_segment.to_global(last_segment.last_point())
segment.position = last_point
add_child(segment)
func get_first_segment():
return get_children().front()
func get_last_segment():
return get_children().back()
func generate_segments():
if get_child_count() < 64:
if randi() % 2 == 0:
next_segments.append(straight(BASE_SIZE))
else:
next_segments += create_loop((randi() % 5 * .1 + .6) * BASE_SIZE)
for seg in next_segments:
add_segment(seg)
next_segments.clear()
func clear_old_segments(xmin:float):
for segment in get_children():
if segment.position.x < xmin:
segment.queue_free()
func curve_right_up(s):
var c = C * s
var curve = segment_scene.instance()
curve.type_name = Segment.CURVE_RIGHT_UP
curve.add_point(Vector2.ZERO, Vector2.ZERO, Vector2(c,0))
curve.add_point(Vector2(s, -s), Vector2(0, c))
return curve
func curve_up_left(s):
var c = C * s
var curve = segment_scene.instance()
curve.type_name = Segment.CURVE_UP_LEFT
curve.add_point(Vector2.ZERO, Vector2.ZERO, Vector2(0,-c))
curve.add_point(Vector2(-s, -s), Vector2(c, 0))
return curve
func curve_left_down(s):
var c = C * s
var curve = segment_scene.instance()
curve.type_name = Segment.CURVE_LEFT_DOWN
curve.add_point(Vector2.ZERO, Vector2.ZERO, Vector2(-c,0))
curve.add_point(Vector2(-s, s), Vector2(0, -c))
return curve
func curve_down_right(s):
var c = C * s
var curve = segment_scene.instance()
curve.type_name = Segment.CURVE_DOWN_RIGHT
curve.add_point(Vector2.ZERO, Vector2.ZERO, Vector2(0, c))
curve.add_point(Vector2(s,s), Vector2(-c, 0))
curve.score_multi = 1 / (s/BASE_SIZE)
return curve
func straight(s):
var curve = segment_scene.instance()
curve.type_name = Segment.STRAIGHT
curve.add_point(Vector2.ZERO)
curve.add_point(Vector2(s, 0))
return curve
func loop_back(s):
var c = C * s
var curve = segment_scene.instance()
curve.type_name = Segment.LOOP_BACK
curve.add_point(Vector2.ZERO, Vector2.ZERO, Vector2(0, -c))
curve.add_point(Vector2(-s, -s), Vector2(c, 0), Vector2(-c, 0))
curve.add_point(Vector2(-2*s, 0), Vector2(0, -c))
return curve