-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.gd
90 lines (62 loc) · 2 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
extends Node2D
@export var mob_scene: PackedScene
var score: int
# Called when the node enters the scene tree for the first time.
func _ready():
var screen_size = get_viewport_rect().size
# Start from center
$StartPosition.position = screen_size / 2
# Clear all Vector2 points
$MobPath.curve.clear_points()
# Set 1st vector
$MobPath.curve.add_point(Vector2(0,0))
# Set 2nd vector
$MobPath.curve.add_point(Vector2(screen_size.x, 0))
# Set 3rd vector
$MobPath.curve.add_point(Vector2(screen_size.x, screen_size.y))
# Set 4th vector
$MobPath.curve.add_point(Vector2(0, screen_size.y))
# Set 5th vector
$MobPath.curve.add_point(Vector2(0, 0))
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
func game_over():
$ScoreTimer.stop()
$MobTimer.stop()
$HUD.show_game_over()
$Music.stop()
$DeathSound.play()
func new_game():
score = 0
$Player.start($StartPosition.position)
$StartTimer.start()
$HUD.update_score(score)
$HUD.show_message("Get Ready")
# Remove old mobs
get_tree().call_group("mobs", "queue_free")
$Music.play()
func _on_start_timer_timeout():
$MobTimer.start()
$ScoreTimer.start()
func _on_score_timer_timeout():
score += 1
$HUD.update_score(score)
func _on_mob_timer_timeout():
# Create a new instance of the Mob scene.
var mob = mob_scene.instantiate()
# Choose a random location on Path2D.
var mob_spawn_location = $MobPath/MobSpawnLocation
mob_spawn_location.progress_ratio = randf()
# Set the mob's direction perpendicular to the path direction.
var direction = mob_spawn_location.rotation + PI / 2
# Set the mob's position to a random location.
mob.position = mob_spawn_location.position
# Add some randomness to the direction.
direction += randf_range(-PI / 4, PI / 4)
mob.rotation = direction
# Choose the velocity for the mob.
var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)
# Spawn the mob by adding it to the Main scene.
add_child(mob)