35 lines
887 B
GDScript
35 lines
887 B
GDScript
extends Node2D
|
|
|
|
var moving = false
|
|
@onready var player = get_tree().get_root().get_node("main/Player")
|
|
var speed = 500
|
|
|
|
# The target is set on initialization and never updated.
|
|
@onready var target = player.position
|
|
|
|
var reached = false
|
|
signal target_reached
|
|
|
|
func _ready() -> void:
|
|
|
|
# Wait one second before firing
|
|
await get_tree().create_timer(1).timeout
|
|
moving = true
|
|
|
|
func _process(delta: float) -> void:
|
|
|
|
# Fire towards the center of the cross
|
|
if moving:
|
|
position += (target - global_position).normalized().rotated(-get_parent().rotation) * speed * delta
|
|
|
|
# When reaching the center, inform parent node
|
|
if((global_position - target).length() < 40):
|
|
moving = false
|
|
if not reached:
|
|
target_reached.emit()
|
|
reached = true
|
|
|
|
# Damage the player on contact
|
|
if has_node("Area2D") and $Area2D.overlaps_body(player):
|
|
player.hurt(1, global_position-player.position)
|
|
|