-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestionsNAnswers.gd
134 lines (105 loc) · 3.48 KB
/
QuestionsNAnswers.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
extends VBoxContainer
@export var button_a: Button
@export var button_b: Button
@export var button_c: Button
@export var button_d: Button
@onready var question_text = get_node("TitleHbox/PanelContainer/CenterContainer/RichTextLabel")
@onready var buttons = {
"A": button_a, "B": button_b,
"C": button_c, "D": button_d
}
var current_question: Dictionary
var locked_option: String = ""
var question_no: int
var correct_option: String
var game_state: int
signal answered(is_correct)
signal locked
func _ready():
for button in buttons.values():
button.toggled.connect(func(_pressed): lock_answer(button))
func _input(event: InputEvent):
if event.is_action_pressed("lock_option_a"):
lock_answer(buttons["A"])
elif event.is_action_pressed("lock_option_c"):
lock_answer(buttons["B"])
elif event.is_action_pressed("lock_option_b"):
lock_answer(buttons["C"])
elif event.is_action_pressed("lock_option_d"):
lock_answer(buttons["D"])
func on_game_state_changed(state: int):
game_state = state
match state:
Game.GAME_STATE.empty:
reset_ui()
show()
Game.GAME_STATE.question:
setup_next_question()
question_text.visible = true
Game.GAME_STATE.A:
button_a.show_text()
Game.GAME_STATE.B:
button_b.show_text()
Game.GAME_STATE.C:
button_c.show_text()
Game.GAME_STATE.D:
button_d.show_text()
Game.GAME_STATE.answered:
check()
func on_fty_fty():
if game_state != Game.GAME_STATE.D: return
var hidden_count = 0
var first_rand = -INF
while hidden_count < 2:
print ("randi() mod %d + %d" % [Game.GAME_STATE.D - Game.GAME_STATE.A + 1, Game.GAME_STATE.A])
var rand = randi() % (Game.GAME_STATE.D - Game.GAME_STATE.A + 1) + Game.GAME_STATE.A
# Do not double select the same button we want 50/50
if first_rand == rand:
continue
# We do not want to hide a correct option
if Game.GAME_STATE.keys()[rand] != correct_option:
hidden_count += 1
print(Game.GAME_STATE.keys()[rand])
buttons[Game.GAME_STATE.keys()[rand]].get_node("Margin").visible = false
first_rand = rand
func reset_ui():
question_text.hide()
for button in buttons.values():
# If advanced into next game mode, the animation player
# might still be animating
button.stop_animations()
# The button might have an override for indicating
# correct (green) or wrong (red)
button.remove_theme_stylebox_override("normal")
button.hide_text()
button.toggle_mode = true
func setup_next_question():
var next_question = $QuestionManager.get_next_question()
if next_question == null: return
question_text.visible = false
current_question = next_question
question_text.text = "[center]%s[/center]" % current_question["question"]
button_a.button_text = current_question["A"]
button_b.button_text = current_question["B"]
button_c.button_text = current_question["C"]
button_d.button_text = current_question["D"]
correct_option = current_question["correct"]
func check():
if locked_option == "": return
var locked_button = buttons[locked_option]
locked_button.button_pressed = false
locked_button.focus_mode = Control.FOCUS_NONE
if locked_option == correct_option:
answered.emit(true)
locked_button.start_animation("Correct")
else:
answered.emit(false)
locked_button.start_animation("Wrong")
# Indicate which would have been the right option
buttons[correct_option].start_animation("Correct")
func lock_answer(locked_button: Button):
for b in buttons.values():
if b != locked_button:
b.button_pressed = false
locked_option = locked_button.name
locked.emit()