-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.gd
182 lines (147 loc) · 4.34 KB
/
main.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
extends Node2D
const MEOW = ["meow!", "nya!", ":3", ";3", "purrr~"]
var numberQueue := []
var backN := 2
var turnTime := 3.0
var turnState := 0
var maxState := 10
var currentTurn := 0
var acceptChoice := false
var countdownNum := 3
var chosen := true
var rightTimes := 0
var wrongTimes := 0
var missTimes := 0
var gameState := "mainmenu"
var RNG = RandomNumberGenerator.new()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
reset_game()
get_window().title = "NyaN-Back by Loveaabb"
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
pass
func count() -> void:
countdownNum -= 1
$UI.change_number(countdownNum)
func is_size_reached_n() -> bool:
return numberQueue.size() == backN + 1
func is_numbers_match() -> bool:
return (numberQueue.back() == numberQueue.front())
func randi_skip_num(minn: int, maxn: int, skip: int) -> int:
var rawrn = RNG.randi_range(minn, maxn - 1)
var res = 0
if rawrn >= skip:
res = rawrn + 1
else:
res = rawrn
return res
func pregame() -> void:
$UI.disp_message("The game starts in", 0)
$UI.disp_message("seconds", 1)
$UI.change_number(countdownNum)
$UI/ButtonGoBack.show()
backN = $UI.get_backn()
$PregameCountDown.start()
func game_start() -> void:
turnState = 0
gameState = "inprogress"
$gameCounter.wait_time = turnTime / maxState
$gameCounter.start()
func next_turn() -> void:
var num = 0
if numberQueue.size() == backN + 1:
numberQueue.pop_front()
if RNG.randf() > 0.5:
num = numberQueue.front()
else:
num = randi_skip_num(0, 9, numberQueue.front())
else:
num = RNG.randi_range(0, 9)
numberQueue.push_back(num)
acceptChoice = false
$UI.set_choice_buttons_availability(acceptChoice)
currentTurn += 1
func reset_game() -> void:
$PregameCountDown.stop()
$gameCounter.stop()
numberQueue = []
turnState = 0
maxState = 10
currentTurn = 0
acceptChoice = false
countdownNum = 3
chosen = true
rightTimes = 0
wrongTimes = 0
missTimes = 0
gameState = "mainmenu"
$UI.set_choice_buttons_visibility(false)
$UI.set_choice_buttons_availability(false)
$UI.disp_message("Welcome", 0)
$UI.change_number(2)
$UI.disp_message("NyaN-Back! :3", 1)
$UI.disp_message("", 2)
$UI/TextStats.text = ""
$UI/StartBtn.show()
$UI/ButtonGoBack.hide()
$UI/SetN/TextWhereN.show()
$UI/SetN/TextEdit.show()
func update_stats():
var total = wrongTimes + rightTimes + missTimes
$UI/TextStats.text = "C/W/M: %d/%d/%d Accuracy: %.1f%%" \
% [rightTimes, wrongTimes, missTimes, 1.0 * rightTimes / total * 100.0]
func _on_pregame_count_down_timeout() -> void:
if countdownNum > 1:
count()
if countdownNum == 1:
$UI.disp_message("second", 1)
else:
$PregameCountDown.stop()
game_start()
func _on_game_counter_timeout() -> void:
if turnState == 0:
next_turn()
$UI.disp_message("Turn #" + str(currentTurn), 0)
$UI.disp_message("N = " + str(backN), 1)
if currentTurn == 1:
$UI.set_choice_buttons_visibility(true)
if not chosen:
$UI/PopTextGenerator.emit_text("Miss!", $UI/NumDisplay.position, Color.RED)
missTimes += 1
update_stats()
if is_size_reached_n():
chosen = false
$UI.disp_message("Get ready...", 2)
else:
$UI.disp_message("", 2)
if turnState <= 2:
$UI.change_number(-turnState - 1)
elif turnState == 3:
$UI.change_number(numberQueue.back())
if is_size_reached_n():
acceptChoice = true
$UI.disp_message("Make your choice", 2)
$UI.set_choice_buttons_availability(acceptChoice)
turnState = (turnState + 1) % maxState
func _on_ui_choice(yes: bool) -> void:
if acceptChoice:
if is_numbers_match() == yes:
$UI/PopTextGenerator.emit_text("Correct!", $UI/NumDisplay.position, Color.GREEN)
rightTimes += 1
update_stats()
else:
$UI/PopTextGenerator.emit_text("Wrong!", $UI/NumDisplay.position, Color.RED)
wrongTimes += 1
update_stats()
acceptChoice = false
chosen = true
$UI.set_choice_buttons_availability(acceptChoice)
func _on_color_rect_gui_input(event: InputEvent) -> void:
if (event is InputEventMouseButton) \
and event.is_pressed() and event.button_index == 1 \
or (event is InputEventScreenTouch) \
and event.is_pressed():
var strtomeow: String = MEOW.pick_random()
var randcolor = Color(RNG.randf(), RNG.randf(), RNG.randf(), 1)
$UI/PopTextGenerator.emit_text(strtomeow, event.position, randcolor)