From 69b08c09e77d88dce7684cccc9d488335fde9a3f Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 16 Apr 2024 04:29:34 +0200 Subject: [PATCH] Create `Move` and `MoveStraight` and refactor `MoveTowards` --- behaviours/move.gd | 6 ++++++ behaviours/{move_towards.svg => move.svg} | 0 ...{move_towards.svg.import => move.svg.import} | 6 +++--- behaviours/move_straight.gd | 17 +++++++++++++++++ behaviours/move_straight.tscn | 6 ++++++ behaviours/move_towards.gd | 13 ++++++------- 6 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 behaviours/move.gd rename behaviours/{move_towards.svg => move.svg} (100%) rename behaviours/{move_towards.svg.import => move.svg.import} (74%) create mode 100644 behaviours/move_straight.gd create mode 100644 behaviours/move_straight.tscn diff --git a/behaviours/move.gd b/behaviours/move.gd new file mode 100644 index 0000000..ae3e420 --- /dev/null +++ b/behaviours/move.gd @@ -0,0 +1,6 @@ +@icon("res://behaviours/move.svg") +extends Node2D +class_name Move + + +signal move(norm: Vector2) diff --git a/behaviours/move_towards.svg b/behaviours/move.svg similarity index 100% rename from behaviours/move_towards.svg rename to behaviours/move.svg diff --git a/behaviours/move_towards.svg.import b/behaviours/move.svg.import similarity index 74% rename from behaviours/move_towards.svg.import rename to behaviours/move.svg.import index 87cd989..4c4d4a8 100644 --- a/behaviours/move_towards.svg.import +++ b/behaviours/move.svg.import @@ -3,15 +3,15 @@ importer="texture" type="CompressedTexture2D" uid="uid://d10wobjiuh1mm" -path="res://.godot/imported/move_towards.svg-f5d5c8fa66b43d0301cc029ecee0a291.ctex" +path="res://.godot/imported/move.svg-6291495a0c7bf9c3c43cbea535114f1a.ctex" metadata={ "vram_texture": false } [deps] -source_file="res://behaviours/move_towards.svg" -dest_files=["res://.godot/imported/move_towards.svg-f5d5c8fa66b43d0301cc029ecee0a291.ctex"] +source_file="res://behaviours/move.svg" +dest_files=["res://.godot/imported/move.svg-6291495a0c7bf9c3c43cbea535114f1a.ctex"] [params] diff --git a/behaviours/move_straight.gd b/behaviours/move_straight.gd new file mode 100644 index 0000000..9c6da0f --- /dev/null +++ b/behaviours/move_straight.gd @@ -0,0 +1,17 @@ +@icon("res://behaviours/move.svg") +extends Move +class_name MoveStraight + + +## A [Move] that moves in a fixed direction. + + +@export var direction: Vector2 + + +func randomize_direction() -> void: + direction = Vector2.from_angle(Random.rng.randf_range(0, 2*PI)) + + +func _physics_process(_delta: float) -> void: + move.emit(direction) diff --git a/behaviours/move_straight.tscn b/behaviours/move_straight.tscn new file mode 100644 index 0000000..e176db3 --- /dev/null +++ b/behaviours/move_straight.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://dk1ipq7dhkhf3"] + +[ext_resource type="Script" path="res://behaviours/move_straight.gd" id="1_8yf73"] + +[node name="MoveStraight" type="Node2D"] +script = ExtResource("1_8yf73") diff --git a/behaviours/move_towards.gd b/behaviours/move_towards.gd index 2c24b82..e46cd27 100644 --- a/behaviours/move_towards.gd +++ b/behaviours/move_towards.gd @@ -1,12 +1,9 @@ -@icon("res://behaviours/move_towards.svg") -extends Node2D +@icon("res://behaviours/move.svg") +extends Move class_name MoveTowards -## A node emitting the [signal move] signal each physics timestep to move towards the position of [field target]. - - -signal move(norm: Vector2) +## A [Move] that moves towards the [field position] of a [field target]. @export var target: Node2D = null @@ -27,4 +24,6 @@ func _physics_process(_delta: float) -> void: if target: var gap = target.global_position - global_position var norm = gap.normalized() - move.emit(norm) + move.emit(norm) + else: + move.emit(Vector2.ZERO)