-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_scene.gd
More file actions
246 lines (202 loc) · 6.22 KB
/
main_scene.gd
File metadata and controls
246 lines (202 loc) · 6.22 KB
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
extends Node2D
var time_left:Timer
var started = false
var ended = false
var running = false
var showing_correct = false
var current_question = -1
var max_question = Data.question_count
@onready var time_label = $Time_label
@onready var time_to_start = $start_timer
var current_action = ""
var last_action = ""
var start_seconds = 0
var incorrect = false
var action_texts = Data.load_actions()
var data = []
var actions = InputMap.get_actions()
var processed_actions = []
var last_actions = []
# I don't like this :(
func get_action_from_input():
var pressed = []
for action in actions:
if Input.is_action_pressed(action):
pressed.append(action)
return pressed
# From https://forum.godotengine.org/t/how-to-round-to-a-specific-decimal-place/27552
func round_place(num,places):
return (round(num*pow(10,places))/pow(10,places))
func check_actions(action_list):
for action in action_list:
if action not in processed_actions:
return false
return true
var action_real = []
func handle_input(action):
#if action == "" or not action in action_texts.keys():
#return
if ($time_out_timer.wait_time - $time_out_timer.time_left) < 0.2:
return
action_real = []
for act in action:
if act not in action_real:
action_real.append(act.to_lower())
last_actions = []
if action_real == [] or not check_actions(action_real):
return
print(action_real)
if running:
last_action = action_real
var time = $time_out_timer.time_left
var hatred_action = action_real
hatred_action.sort()
#action.sort()
#if len(hatred_action) > 1:
#temp_action = ",".join(hatred_action)
#else:
#temp_action = hatred_action[0]
var curr_action = []
if "," in current_action:
curr_action = Array(current_action.split(","))
else:
curr_action = [current_action]
curr_action.sort()
if hatred_action == curr_action:
$time_out_timer.stop()
data.append({"number":current_question, "action":current_action, "time":$time_out_timer.wait_time - time, "correct":true})
correct()
else:
$time_out_timer.stop()
data.append({"number":current_question, "action":current_action, "time":$time_out_timer.wait_time - time, "correct":false})
incorrect = true
incorrect_or_timeout()
func _input(event: InputEvent) -> void:
#print(event.as_text())
if event is InputEventJoypadButton or event is InputEventJoypadMotion:
#print(event.as_text())
var action = get_action_from_input()
last_actions += action
ticker = 10
#handle_input(action)
func set_new_action():
var keys = action_texts.keys()
current_action = keys.pick_random()
print("Current Action: "+current_action)
$main_label.text = action_texts.get(current_action)
func after_showing_correct():
$error_label.visible = false
showing_correct = false
run()
pass
func reset_text():
$main_label.text = "Wait"
func _get_binding_names(actions):
var temp = []
for action in actions:
temp.append(BindingNames.get_binding_from_action(action))
return "+".join(temp)
func _figure_out_binding_names(actions):
if "," in actions:
return _get_binding_names(actions.split(","))
return BindingNames.get_binding_from_action(actions)
func incorrect_or_timeout():
reset_text()
running = false
#print("Incorrect action, inputed "+last_action+" instead of "+current_action)
print("Incorrect action")
print(last_action)
print(current_action)
showing_correct = true
$error_label.visible = true
if incorrect:
$error_label.text = "You pressed %s instead of %s!" % [_get_binding_names(last_action), _figure_out_binding_names(current_action)]
else:
$error_label.text = "Ran out of time!\nYou need to press %s for action \"%s\"!" %[_figure_out_binding_names(current_action), action_texts.get(current_action)]
incorrect = false
$temp_false_timer.start()
func correct():
reset_text()
running = false
$time_out_timer.wait_time = 5
print("Correct action")
if Data.vibration:
Input.start_joy_vibration(0, 0, 0.5, 0.5)
$temp_correct_timer.start()
func start_first_time():
start_seconds += 1
$main_label.text = "Starting in " + str(3-start_seconds)
if start_seconds == 3:
time_to_start.stop()
_start()
func _start():
started = true
time_label.visible = true
$time_out_timer.connect("timeout", incorrect_or_timeout)
run()
func handle_end():
print("Ending Scene, starting ending")
print(data)
var temp = {"data":data, "total_actions":max_question}
var avg_temp = 0
var correct = 0
for d in data:
avg_temp += d.get("time")
if d.get("correct"):
correct += 1
temp.get_or_add("average_time", avg_temp/len(data))
temp.get_or_add("name", Data.player_name)
temp.get_or_add("correct_count", correct)
Data.player_data = temp
get_tree().change_scene_to_file("res://ending.tscn")
#var file = FileAccess.open("user://last.json", FileAccess.WRITE)
#var json_string = JSON.stringify(temp)
#file.store_string(json_string)
#file.close()
func run():
current_question += 1
$question_bar.value = current_question
if current_question >= max_question:
handle_end()
running = true
set_new_action()
$time_out_timer.start()
func process_actions():
var temp = action_texts.keys()
for temp2 in temp:
if "," not in temp2:
processed_actions.append(temp2)
else:
var part2 = temp2.split(",")
processed_actions = processed_actions + Array(part2)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
if Data.vibration:
Input.start_joy_vibration(0, 1, 1, 0.5)
process_actions()
#time_to_start = SceneTree.create_timer(3.0)
time_to_start.start()
time_to_start.connect("timeout", start_first_time)
$temp_correct_timer.connect("timeout", run)
$temp_false_timer.connect("timeout", after_showing_correct)
$question_bar.max_value = max_question
pass # Replace with function body.
var ticker = 0
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if running:
ticker += 1
#var gaming = get_action_from_input()
if ticker >= 20:
ticker = 0
handle_input(last_actions)
last_actions = []
#print("process")
if running and started:
time_label.text = str(round_place(($time_out_timer.wait_time - $time_out_timer.time_left), 3)) + "s"
#var action = get_action_from_input()
#handle_input(action)
pass
func _on_time_out_timer_timeout() -> void:
running = false
showing_correct = true