-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.gd
131 lines (110 loc) · 4.12 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
extends Node2D
const MaxSpawned = 5
const Spaceship = preload("res://Spaceship.tscn")
const Boulder1 = preload("res://Boulder 1.tscn")
const Boulder2 = preload("res://Boulder 2.tscn")
const Boulder3 = preload("res://Boulder 3.tscn")
const Boulder4 = preload("res://Boulder 4.tscn")
const Stars = preload("res://Stars.tscn")
var score = 0
var current_spawned = 0
func _ready():
# Create the spawn curve for the boulders.
var boulder_curve = Curve2D.new()
var screen_size = get_viewport_rect().size
get_viewport().connect("size_changed", self, "_on_viewport_size_changed")
boulder_curve.add_point(Vector2(0, 0))
boulder_curve.add_point(Vector2(screen_size.x, 0))
boulder_curve.add_point(Vector2(screen_size.x, screen_size.y))
boulder_curve.add_point(Vector2(0, screen_size.y))
boulder_curve.add_point(Vector2(0, 0))
$BoulderPath.curve = boulder_curve
generate_background()
func _on_viewport_size_changed():
for s in get_tree().get_nodes_in_group("stars"):
s.queue_free()
generate_background()
func generate_background():
var screen_size = get_viewport_rect().size
var stars = Stars.instance()
var texture_size = stars.get_texture().get_size()
var star_position = Vector2(0, 0)
# Fill the background with stars.
while true:
stars.position = star_position
add_child(stars)
star_position.x += texture_size.x
if star_position.x >= screen_size.x:
star_position.x = 0
star_position.y += texture_size.y
if star_position.y >= screen_size.y:
break
stars = Stars.instance()
func new_game():
score = 0
current_spawned = 0
$HUD.update_score(score)
$HUD.show_message("Get Ready")
var spaceship = Spaceship.instance()
spaceship.connect("spaceship_crashed", self, "_on_spaceship_crashed")
add_child(spaceship)
spaceship.show()
$BoulderTimer.start()
func game_over():
$BoulderTimer.stop()
for boulder in get_tree().get_nodes_in_group("boulders"):
boulder.queue_free()
$HUD.show_game_over()
func _on_BoulderTimer_timeout():
if current_spawned < MaxSpawned:
# Choose a random location on Path2D.
$BoulderPath/BoulderSpawnLocation.set_offset(randi())
# Create a Boulder instance and add it to the scene.
var boulder = Boulder1.instance()
boulder.connect("boulder_removed", self, "_on_Boulder_boulder_removed")
boulder.connect("boulder_hit", self, "_on_Boulder_boulder_hit")
add_child(boulder)
# Set the boulder's position to a random location.
boulder.position = $BoulderPath/BoulderSpawnLocation.position
# Set the boulders's direction perpendicular to the path direction.
var direction = $BoulderPath/BoulderSpawnLocation.rotation + PI / 2
# Add some randomness to the direction.
direction += rand_range(-PI / 4, PI / 4)
boulder.rotation = direction
boulder.velocity = Vector2(boulder.speed, 0).rotated(direction)
current_spawned += 1
func _on_spaceship_crashed():
game_over()
func _on_Boulder_boulder_removed():
current_spawned -= 1
func _on_Boulder_boulder_hit(boulder, type):
var velocity = boulder.velocity
var new_boulder
for x in [-1, 0, 1]:
match type:
1:
new_boulder = Boulder2.instance()
_add_boulder(new_boulder, boulder, x)
2:
new_boulder = Boulder3.instance()
_add_boulder(new_boulder, boulder, x)
3:
new_boulder = Boulder4.instance()
_add_boulder(new_boulder, boulder, x)
4:
pass
match type:
1:
score += 100
2:
score += 150
3:
score += 200
4:
score += 250
$HUD.update_score(score)
func _add_boulder(new_boulder, boulder, x):
new_boulder.position = boulder.position
new_boulder.velocity = boulder.velocity.rotated(x * PI/4)
new_boulder.connect("boulder_hit", self, "_on_Boulder_boulder_hit")
add_child(new_boulder)