-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain.gd
117 lines (89 loc) · 3.18 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
extends Node2D
var Enemy = preload("res://Enemy.tscn")
onready var enemy_container = $EnemyContainer
onready var spawn_container = $SpawnContainer
onready var spawn_timer = $SpawnTimer
onready var difficulty_timer = $DifficultyTimer
onready var difficulty_value = $CanvasLayer/VBoxContainer/BottomRow/HBoxContainer/DifficultyValue
onready var killed_value = $CanvasLayer/VBoxContainer/TopRow2/TopRow/EnemiesKilledValue
onready var game_over_screen = $CanvasLayer/GameOverScreen
var active_enemy = null
var current_letter_index: int = -1
var difficulty: int = 0
var enemies_killed: int = 0
func _ready() -> void:
start_game()
func find_new_active_enemy(typed_character: String):
for enemy in enemy_container.get_children():
var prompt = enemy.get_prompt()
var next_character = prompt.substr(0, 1)
if next_character == typed_character:
print("found new enemy that starts with %s" % next_character)
active_enemy = enemy
current_letter_index = 1
active_enemy.set_next_character(current_letter_index)
return
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventKey and event.is_pressed() and not event.is_echo():
var typed_event = event as InputEventKey
var key_typed = PoolByteArray([typed_event.unicode]).get_string_from_utf8()
if active_enemy == null:
find_new_active_enemy(key_typed)
else:
var prompt = active_enemy.get_prompt()
var next_character = prompt.substr(current_letter_index, 1)
if key_typed == next_character:
print("successfully typed %s" % key_typed)
current_letter_index += 1
active_enemy.set_next_character(current_letter_index)
if current_letter_index == prompt.length():
print("done")
current_letter_index = -1
active_enemy.queue_free()
active_enemy = null
enemies_killed += 1
killed_value.text = str(enemies_killed)
else:
print("incorrectly typed %s instead of %s" % [key_typed, next_character])
func _on_SpawnTimer_timeout() -> void:
spawn_enemy()
func spawn_enemy():
var enemy_instance = Enemy.instance()
var spawns = spawn_container.get_children()
var index = randi() % spawns.size()
enemy_instance.global_position = spawns[index].global_position
enemy_container.add_child(enemy_instance)
enemy_instance.set_difficulty(difficulty)
func _on_DifficultyTimer_timeout() -> void:
if difficulty >= 20:
difficulty_timer.stop()
difficulty = 20
return
difficulty += 1
GlobalSignals.emit_signal("difficulty_increased", difficulty)
print("Difficulty increased to %d" % difficulty)
var new_wait_time = spawn_timer.wait_time - 0.2
spawn_timer.wait_time = clamp(new_wait_time, 1, spawn_timer.wait_time)
difficulty_value.text = str(difficulty)
func _on_LoseArea_body_entered(body: Node) -> void:
game_over()
func game_over():
game_over_screen.show()
spawn_timer.stop()
difficulty_timer.stop()
active_enemy = null
current_letter_index = -1
for enemy in enemy_container.get_children():
enemy.queue_free()
func start_game():
game_over_screen.hide()
difficulty = 0
enemies_killed = 0
difficulty_value.text = str(0)
killed_value.text = str(0)
randomize()
spawn_timer.start()
difficulty_timer.start()
spawn_enemy()
func _on_RestartButton_pressed() -> void:
start_game()