From 19f0f888078541ce865945c4f4ac045e630822ea Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 2 Apr 2022 22:05:02 +0200 Subject: [PATCH] :sparkles: Create basic enemy attracted to the fire --- src/entities/Enemy.tscn | 18 ++++++++++++++++++ src/entities/behaviours/AttractedTo.gd | 26 ++++++++++++++++++++++++++ src/levels/Game.tscn | 6 +++++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/entities/Enemy.tscn create mode 100644 src/entities/behaviours/AttractedTo.gd diff --git a/src/entities/Enemy.tscn b/src/entities/Enemy.tscn new file mode 100644 index 0000000..5ee34a5 --- /dev/null +++ b/src/entities/Enemy.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://src/mechanics/Black.png" type="Texture" id=1] +[ext_resource path="res://src/entities/behaviours/AttractedTo.gd" type="Script" id=2] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 16, 16 ) + +[node name="Enemy" type="KinematicBody2D"] + +[node name="AttractedTo" type="Node" parent="."] +script = ExtResource( 2 ) + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 1 ) + +[node name="Shape" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) diff --git a/src/entities/behaviours/AttractedTo.gd b/src/entities/behaviours/AttractedTo.gd new file mode 100644 index 0000000..e2e1345 --- /dev/null +++ b/src/entities/behaviours/AttractedTo.gd @@ -0,0 +1,26 @@ +extends Node + + +export var movement_per_second: float = 32.0 + + +onready var target: KinematicBody2D = get_parent() +# TODO: Use a better algorithm +onready var goal: StaticBody2D = get_parent().get_parent().get_node("Fire") + + +signal touching_goal +var goal_reached_triggered: bool = false +signal goal_reached + + +func _physics_process(_delta): + 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 \ No newline at end of file diff --git a/src/levels/Game.tscn b/src/levels/Game.tscn index 4a79567..ad3c363 100644 --- a/src/levels/Game.tscn +++ b/src/levels/Game.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://src/mechanics/Background.tscn" type="PackedScene" id=1] [ext_resource path="res://src/mechanics/Darkness.tscn" type="PackedScene" id=2] [ext_resource path="res://src/entities/Fire.tscn" type="PackedScene" id=3] [ext_resource path="res://src/entities/Player.tscn" type="PackedScene" id=4] +[ext_resource path="res://src/entities/Enemy.tscn" type="PackedScene" id=5] [node name="Game" type="Node2D"] @@ -19,3 +20,6 @@ intensity = 1.0 [node name="Player" parent="." instance=ExtResource( 4 )] position = Vector2( 500, 438 ) + +[node name="Enemy" parent="." instance=ExtResource( 5 )] +position = Vector2( 755, 508 )