1
Fork 0
mirror of https://github.com/RYGhub/the-cold-night.git synced 2024-11-25 06:04:19 +00:00

🧹 Clean up AttractedTo

This commit is contained in:
Steffo 2022-04-03 18:15:15 +02:00
parent e08870b788
commit cd83f39b3d
Signed by: steffo
GPG key ID: 6965406171929D01
4 changed files with 43 additions and 33 deletions

View file

@ -1,27 +0,0 @@
extends Node
export var movement_per_second: float = 32.0
export var goal_path: String = "../../PhaseOneContainer/Fire"
onready var target: KinematicBody2D = get_parent()
# TODO: Use a better algorithm
onready var goal: StaticBody2D = get_node(goal_path)
signal touching_goal
var goal_reached_triggered: bool = false
signal goal_reached
func _physics_process(_delta):
if target.global_position != null:
var direction: Vector2 = (goal.global_position - target.global_position).normalized()
var _motion: Vector2 = target.move_and_slide(direction * movement_per_second, Vector2.ZERO)
for slide_no in target.get_slide_count():
var slide = target.get_slide_collision(slide_no)
if slide.collider == goal:
emit_signal("touching_goal")
if not goal_reached_triggered:
emit_signal("goal_reached")
goal_reached_triggered = true

View file

@ -1,6 +0,0 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://src/entities/behaviours/AttractedTo.gd" type="Script" id=1]
[node name="AttractedTo" type="Node"]
script = ExtResource( 1 )

View file

@ -0,0 +1,34 @@
extends Node
class_name AttractedToMovement
signal touching_goal
signal goal_reached
export var movement_per_second: float
export var goal_path: NodePath
onready var parent: KinematicBody2D = get_parent()
onready var goal: StaticBody2D = get_node(goal_path) if goal_path else null
var _goal_reached_triggered: bool = false
func move():
var direction: Vector2 = (goal.global_position - parent.global_position).normalized()
var _motion: Vector2 = parent.move_and_slide(direction * movement_per_second, Vector2.ZERO)
for slide_no in parent.get_slide_count():
var slide = parent.get_slide_collision(slide_no)
if slide.collider == goal:
emit_signal("touching_goal")
if not _goal_reached_triggered:
emit_signal("goal_reached")
_goal_reached_triggered = true
func _physics_process(_delta):
if goal:
move()

View file

@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://src/entities/behaviours/AttractedToMovement.gd" type="Script" id=1]
[node name="AttractedToMovement" type="Node"]
script = ExtResource( 1 )
__meta__ = {
"_editor_description_": "Moves the parent KinematicBody2D towards the specified goal."
}