godot-demo-projects/2d/dodge_the_creeps/main.gd

57 lines
1.3 KiB
GDScript3
Raw Normal View History

2018-03-12 16:49:09 +01:00
extends Node
@export var mob_scene: PackedScene
2018-03-12 16:49:09 +01:00
var score
2019-03-17 01:26:56 +01:00
func game_over():
$ScoreTimer.stop()
$MobTimer.stop()
$HUD.show_game_over()
$Music.stop()
$DeathSound.play()
2018-03-12 16:49:09 +01:00
func new_game():
2023-03-01 07:46:50 +01:00
get_tree().call_group(&"mobs", &"queue_free")
2018-03-12 16:49:09 +01:00
score = 0
$Player.start($StartPosition.position)
$StartTimer.start()
2019-03-17 01:26:56 +01:00
$HUD.update_score(score)
2018-03-12 16:49:09 +01:00
$HUD.show_message("Get Ready")
$Music.play()
2018-03-12 16:49:09 +01:00
func _on_MobTimer_timeout():
# Create a new instance of the Mob scene.
var mob = mob_scene.instantiate()
2020-10-06 11:28:27 +02:00
# Choose a random location on Path2D.
var mob_spawn_location = get_node(^"MobPath/MobSpawnLocation")
mob_spawn_location.progress = randi()
2020-10-06 11:28:27 +02:00
# Set the mob's direction perpendicular to the path direction.
var direction = mob_spawn_location.rotation + PI / 2
2020-10-06 11:28:27 +02:00
# Set the mob's position to a random location.
mob.position = mob_spawn_location.position
2020-10-06 11:28:27 +02:00
# Add some randomness to the direction.
direction += randf_range(-PI / 4, PI / 4)
mob.rotation = direction
2020-10-06 11:28:27 +02:00
# Choose the velocity for the mob.
var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)
2018-03-12 16:49:09 +01:00
# Spawn the mob by adding it to the Main scene.
add_child(mob)
2018-03-12 16:49:09 +01:00
func _on_ScoreTimer_timeout():
score += 1
$HUD.update_score(score)
2019-03-17 01:26:56 +01:00
2019-03-17 01:26:56 +01:00
func _on_StartTimer_timeout():
$MobTimer.start()
$ScoreTimer.start()