-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Main.gd
100 lines (86 loc) · 2.51 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
extends Node
export (PackedScene) var Asteroid
export (PackedScene) var Explosion
export (PackedScene) var Enemy
export (PackedScene) var Drop
func _ready():
randomize()
global.screensize = get_viewport().size
$Music.play()
start_next_level()
func start_next_level():
global.level += 1
$EnemyTimer.stop()
$EnemyTimer.wait_time = rand_range(4, 7)
$EnemyTimer.start()
$HUD.show_message("Wave %s" % global.level)
for i in range(global.level):
$"AsteroidPath/PathLocation".set_offset(randi())
spawn_asteroid('big', $"AsteroidPath/PathLocation".position, Vector2())
func _process(delta):
if $Asteroids.get_child_count() == 0:
start_next_level()
func add_bullet(bullet_type, pos, dir):
var b = bullet_type.instance()
b.start_at(pos, dir)
$Bullets.add_child(b)
func spawn_asteroid(size, pos, vel):
var a = Asteroid.instance()
a.connect("explode", self, "explode_asteroid")
a.init(size, pos, vel)
$Asteroids.add_child(a)
func explode_asteroid(size, extents, pos, vel, hit_vel):
global.score += global.rock_points[size]
$HUD.update_score(global.score)
var newsize = global.break_pattern[size]
if newsize:
for offset in [-1, 1]:
var newpos = pos + hit_vel.tangent() * max(extents.x/2, extents.y/2) * offset
var newvel = hit_vel.tangent() * vel.length() * 1.2 * offset
newvel = newvel.rotated(rand_range(-0.5, 0.5))
spawn_asteroid(newsize, newpos, newvel)
var expl = Explosion.instance()
add_child(expl)
expl.position = pos
$"Sounds/AsteroidExplosion".play()
expl.play()
if randf() < global.drop_chance:
var d = Drop.instance()
d.position = pos
add_child(d)
func explode_player():
var expl = Explosion.instance()
add_child(expl)
expl.position = $Player.position
expl.scale = Vector2(1.5, 1.5)
expl.animation = "sonic"
expl.play()
$HUD.show_message("Game Over")
$RestartTimer.start()
func explode_enemy(pos):
global.score += global.enemy_points
$HUD.update_score(global.score)
var expl = Explosion.instance()
add_child(expl)
expl.position = pos
expl.scale = Vector2(0.8, 0.8)
expl.animation = "sonic"
$"Sounds/EnemyExplosion".play()
expl.play()
var d = Drop.instance()
d.position = pos
add_child(d)
func _on_EnemyTimer_timeout():
var e = Enemy.instance()
add_child(e)
e.target = $Player
e.connect("shoot", self, "add_bullet")
e.connect("explode", self, "explode_enemy")
$EnemyTimer.wait_time = rand_range(25, 40)
$EnemyTimer.start()
func _on_RestartTimer_timeout():
global.new_game()
func _on_Player_pickup(body):
global.cash += 1
$HUD.update_cash(global.cash)
body.queue_free()