Dodge - cleanup. Added Survivor. /JL
This commit is contained in:
39
dodge/scripts/hud.gd
Normal file
39
dodge/scripts/hud.gd
Normal file
@@ -0,0 +1,39 @@
|
||||
extends CanvasLayer
|
||||
|
||||
# Notifies `Main` node that the button has been pressed
|
||||
signal start_game
|
||||
|
||||
func show_message(text):
|
||||
$Message.text = text
|
||||
$Message.show()
|
||||
$MessageTimer.start()
|
||||
|
||||
func show_game_over():
|
||||
show_message("Game Over")
|
||||
# Wait until the MessageTimer has counted down.
|
||||
await $MessageTimer.timeout
|
||||
|
||||
$Message.text = "Dodge the Creeps!"
|
||||
$Message.show()
|
||||
# Make a one-shot timer and wait for it to finish.
|
||||
await get_tree().create_timer(1.0).timeout
|
||||
$StartButton.show()
|
||||
|
||||
func update_score(score):
|
||||
$ScoreLabel.text = str(score)
|
||||
|
||||
func _on_start_button_pressed():
|
||||
$StartButton.hide()
|
||||
start_game.emit()
|
||||
|
||||
func _on_message_timer_timeout():
|
||||
$Message.hide()
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(_delta: float) -> void:
|
||||
pass
|
||||
1
dodge/scripts/hud.gd.uid
Normal file
1
dodge/scripts/hud.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bm3hot0cb2ls8
|
||||
62
dodge/scripts/main.gd
Normal file
62
dodge/scripts/main.gd
Normal file
@@ -0,0 +1,62 @@
|
||||
extends Node
|
||||
|
||||
@export var mob_scene: PackedScene
|
||||
var score = 0
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
#new_game()
|
||||
pass
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(_delta: float) -> void:
|
||||
pass
|
||||
|
||||
func game_over():
|
||||
$ScoreTimer.stop()
|
||||
$MobTimer.stop()
|
||||
$HUD.show_game_over()
|
||||
|
||||
func new_game():
|
||||
get_tree().call_group("mobs", "queue_free")
|
||||
$HUD.update_score(score)
|
||||
$HUD.show_message("Get Ready")
|
||||
score = 0
|
||||
$Player.start($StartPosition.position)
|
||||
$StartTimer.start()
|
||||
|
||||
|
||||
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 position to the random location.
|
||||
mob.position = mob_spawn_location.position
|
||||
|
||||
# Set the mob's direction perpendicular to the path direction.
|
||||
var direction = mob_spawn_location.rotation + PI / 2
|
||||
|
||||
# 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)
|
||||
|
||||
|
||||
func _on_score_timer_timeout():
|
||||
score += 1
|
||||
$HUD.update_score(score)
|
||||
|
||||
func _on_start_timer_timeout():
|
||||
$MobTimer.start()
|
||||
$ScoreTimer.start()
|
||||
1
dodge/scripts/main.gd.uid
Normal file
1
dodge/scripts/main.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://c6ywdg0jd2u57
|
||||
16
dodge/scripts/mob.gd
Normal file
16
dodge/scripts/mob.gd
Normal file
@@ -0,0 +1,16 @@
|
||||
extends RigidBody2D
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
var mob_types = Array($AnimatedSprite2D.sprite_frames.get_animation_names())
|
||||
$AnimatedSprite2D.animation = mob_types.pick_random()
|
||||
$AnimatedSprite2D.play()
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(_delta: float) -> void:
|
||||
pass
|
||||
|
||||
func _on_visible_on_screen_notifier_2d_screen_exited():
|
||||
queue_free()
|
||||
1
dodge/scripts/mob.gd.uid
Normal file
1
dodge/scripts/mob.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://byts86xvp7fx5
|
||||
52
dodge/scripts/player.gd
Normal file
52
dodge/scripts/player.gd
Normal file
@@ -0,0 +1,52 @@
|
||||
extends Area2D
|
||||
|
||||
signal hit
|
||||
@export var speed = 400 # How fast the player will move (pixels/sec).
|
||||
var screen_size # Size of the game window.
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
screen_size = get_viewport_rect().size
|
||||
hide()
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
var velocity = Vector2.ZERO # The player's movement vector.
|
||||
if Input.is_action_pressed("move_right"):
|
||||
velocity.x += 1
|
||||
if Input.is_action_pressed("move_left"):
|
||||
velocity.x -= 1
|
||||
if Input.is_action_pressed("move_down"):
|
||||
velocity.y += 1
|
||||
if Input.is_action_pressed("move_up"):
|
||||
velocity.y -= 1
|
||||
|
||||
if velocity.length() > 0:
|
||||
velocity = velocity.normalized() * speed
|
||||
$AnimatedSprite2D.play()
|
||||
else:
|
||||
$AnimatedSprite2D.stop()
|
||||
|
||||
position += velocity * delta
|
||||
position = position.clamp(Vector2.ZERO, screen_size)
|
||||
|
||||
if velocity.x != 0:
|
||||
$AnimatedSprite2D.animation = "walk"
|
||||
$AnimatedSprite2D.flip_v = false
|
||||
# See the note below about the following boolean assignment.
|
||||
$AnimatedSprite2D.flip_h = velocity.x < 0
|
||||
elif velocity.y != 0:
|
||||
$AnimatedSprite2D.animation = "up"
|
||||
$AnimatedSprite2D.flip_v = velocity.y > 0
|
||||
|
||||
|
||||
func _on_body_entered(_body):
|
||||
hide() # Player disappears after being hit.
|
||||
hit.emit()
|
||||
# Must be deferred as we can't change physics properties on a physics callback.
|
||||
$CollisionShape2D.set_deferred("disabled", true)
|
||||
|
||||
func start(pos):
|
||||
position = pos
|
||||
show()
|
||||
$CollisionShape2D.disabled = false
|
||||
1
dodge/scripts/player.gd.uid
Normal file
1
dodge/scripts/player.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://uto2x8nu0r1u
|
||||
Reference in New Issue
Block a user