-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.gd
80 lines (63 loc) · 1.91 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
extends Node
signal game_over
export (PackedScene) var Spinach
export var percentage_chance_to_spawn = 25
var seconds_left = 30
var score
var screen_size # Size of the game window.
var min_height # Minimum height
var sweaks;
func _ready():
# Ewwwwww!
min_height = $Player.min_height
screen_size = $Player.screen_size
sweaks = [$Sweaks/one, $Sweaks/two, $Sweaks/three]
# Needs to be called in main so all random functions in the other nodes
randomize()
# Let's us reset the game
func new_game():
score = 0
seconds_left = 30
$Player.start($StartPosition.position)
$Music.play()
$SpinachTimer.start()
$GameOverTimer.start()
$SecondsTimer.start()
$Background.visible = true
# Called on game over timeout
func game_over():
emit_signal("game_over")
$Player.stop()
$SpinachTimer.stop()
$GameOverTimer.stop()
$SecondsTimer.stop()
$HUD.show_game_over()
$Music.stop()
$EndSound.play()
$Background.visible = false
# Generate a new spinach leaf based on the chance to spawn
func _on_SpinachTimer_timeout():
var chance_range = int(100 / percentage_chance_to_spawn)
if ((randi() % chance_range) == 0):
var spinach = Spinach.instance()
add_child_below_node($Background, spinach)
var rand_x = randi() % int(screen_size.x)
var rand_y = randi() % int(screen_size.y) + min_height
spinach.position = Vector2(rand_x, rand_y)
var direction = rand_range(-PI / 4, PI / 4)
spinach.rotation = direction
# Let Main know when this particular tasty morsel is eaten
spinach.connect("eaten", self, "_on_Spinach_eaten")
# Let Spinach know when game is over to remove itself from the canvas
self.connect("game_over", spinach, "game_over")
# Fired when leaf is collided with
func _on_Spinach_eaten():
score += 1
$HUD.change_score(score)
play_sweak()
func _on_SecondsTimer_timeout():
seconds_left -= 1
$HUD.change_seconds(seconds_left)
func play_sweak():
var sweak = sweaks[randi() % sweaks.size()]
sweak.play()