-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.gd
194 lines (135 loc) · 4.58 KB
/
game.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
extends Node2D
var genpassed = 0
var viewbest = false
func _ready():
Engine.time_scale = 3
# WHY ARE OPTION BUTTONS BROKEY
$HUD/FitnessButton.add_item("Maximize distance", 0)
$HUD/FitnessButton.add_item("Minimize volume", 1)
$HUD/FitnessButton.add_item("Maximize wheels", 2)
$HUD/FitnessButton.add_item("balamced", 3)
$HUD/FitnessButton.add_item("Maximize weight", 4)
$HUD/selection.add_item("tournament", 0)
$HUD/selection.add_item("roulette", 0)
$HUD/Label.text = "MAX LIFE: "+ str(Genetic.max_life)
func _physics_process(_delta):
$HUD/gentimelabel.text = "Time left: " + str(stepify($gentimelimit.time_left,0.01))
if Input.is_action_just_pressed("space"):
get_tree().paused = !get_tree().paused
if get_tree().paused:
$HUD/Label3.text = "PAUSED"
else:
$HUD/Label3.text = ""
if Input.is_key_pressed(KEY_Q):
if $cam.zoom.x >= 0.2:
$cam.zoom -= Vector2(0.1,0.1)
else:
$cam.zoom = Vector2(0.1,0.1)
$HUD/ZoomLabel.text = "Zoom: "+ str($cam.zoom).split(',')[0].substr(1, 5)
$HUD/HScrollBar.value = $cam.zoom.x
elif Input.is_key_pressed(KEY_E):
$cam.zoom += Vector2(0.1,0.1)
$HUD/ZoomLabel.text = "Zoom: "+ str($cam.zoom).split(',')[0].substr(1, 5)
$HUD/HScrollBar.value = $cam.zoom.x
var mod = 1
if Input.is_key_pressed(KEY_SHIFT):
mod = 2
if Input.is_key_pressed(KEY_D):
$cam.position.x += 15*mod
elif Input.is_key_pressed(KEY_A):
$cam.position.x -= 15*mod
if Input.is_key_pressed(KEY_W):
$cam.position.y -= 15*mod
elif Input.is_key_pressed(KEY_S):
$cam.position.y += 15*mod
if viewbest:
var bestcardistance = -10000
var bestcar
for i in $cars.get_children():
if i.global_position.x - $start.rect_global_position.x >= bestcardistance:
bestcardistance = i.global_position.x - $start.rect_global_position.x
bestcar = i
for i in $cars.get_children():
i.modulate = Color(1,1,1,0.1)
if bestcar:
bestcar.modulate = Color(1,1,1,1)
else:
for i in $cars.get_children():
i.modulate = Color(1,1,1,1)
func _on_StartButton_pressed():
if genpassed == 0:
resetGen(false)
$gentimelimit.wait_time = Genetic.max_life
$gentimelimit.start()
if genpassed == 0:
Genetic.generations = []
Genetic.generations.append(Genetic.randomgen())
for i in range(Genetic.gensize):
var newthing = preload("res://thing.tscn").instance()
newthing.cardata = Genetic.generations[-1][i]
if viewbest:
newthing.modulate = Color(1,1,1,0.1)
newthing.call_deferred("set_global_position",Vector2(184,320))
$cars.call_deferred("add_child",newthing)
func _on_ResetButton_pressed():
resetGen()
func resetGen(removeRoad = true):
genpassed = 0
Genetic.generations = []
Genetic.generations.append(Genetic.randomgen())
Genetic.genfitness = []
for i in $cars.get_children():
i.queue_free()
if removeRoad:
for i in $RoadGeneration.get_children():
i.queue_free()
$gentimelimit.stop()
$HUD/GenLabel.text = "Generation: " + str(genpassed)
func _on_HScrollBar_value_changed(value):
$cam.zoom = Vector2(value,value)
$HUD/ZoomLabel.text = "Zoom: "+ str($cam.zoom).split(',')[0].substr(1, 5)
# terminate simulation
func end():
var all = []
for i in $cars.get_children():
var distance = i.global_position.x - $start.rect_global_position.x
var wheelsum = i.wheelsum
var bodyvolume = i.bodyvolume
var weight = i.totalweight
all.append(Genetic.fitness(distance,wheelsum,bodyvolume,weight))
#print("\n\n\n",all,"\n\n\n")
Genetic.genfitness.append(all)
Genetic.nextgen(Genetic.generations[-1])
# wipeout (exterminate) all cars
for i in $cars.get_children():
i.queue_free()
genpassed += 1
$HUD/GenLabel.text = "Generation: " + str(genpassed)
_on_StartButton_pressed()
func _on_GenSizeSlider_value_changed(value):
$HUD/GenSizeLabel.text = "Generations size = " + str(value)
Genetic.gensize = int(value)
# reset generations but keep the road
genpassed = 0
Genetic.generations = []
Genetic.generations.append(Genetic.randomgen())
Genetic.genfitness = []
for i in $cars.get_children():
i.queue_free()
$HUD/GenLabel.text = "Generation: " + str(genpassed)
func _on_FitnessButton_item_selected(index):
Genetic.fitness_id = index
func _on_gentimelimit_timeout():
end()
func _on_lifetime_value_changed(value):
Genetic.max_life = value
$HUD/Label.text = "MAX LIFE: "+ str(Genetic.max_life)
resetGen(false)
func _on_HScrollBar2_value_changed(value):
$HUD/Label2.text = "Time scale: " + str(value)
Engine.time_scale = value
func _on_viewbest_toggled(button_pressed):
viewbest = button_pressed
func _on_selection_item_selected(index):
Genetic.selection_id = index
pass # Replace with function body.