feat: add follow logic to the slime;

This commit is contained in:
2026-09-24 21:52:56 +02:00
parent 7b30283765
commit 2aad0915e3
5 changed files with 197 additions and 14 deletions
+32
View File
@@ -0,0 +1,32 @@
extends CharacterBody2D
@export var speed: int = 300
var target: Node2D
func _physics_process(delta):
if target:
animate_slime()
move_slime()
move_and_slide()
func move_slime():
var distance_to_player: Vector2
distance_to_player = target.global_position - global_position
var direction_normal: Vector2 = distance_to_player.normalized()
velocity = direction_normal * speed
func animate_slime():
var normal_velocity: Vector2 = velocity.normalized()
if normal_velocity.x > 0.707:
$AnimatedSprite2D.play("move_right")
elif normal_velocity.x < -0.707:
$AnimatedSprite2D.play("move_left")
if normal_velocity.y > 0.707:
$AnimatedSprite2D.play("move_down")
elif normal_velocity.y < -0.707:
$AnimatedSprite2D.play("move_up")
func _on_player_detect_area_body_entered(body):
if body is Player:
target = body
+1
View File
@@ -0,0 +1 @@
uid://b38dc40ei5ape
+151
View File
@@ -1,5 +1,6 @@
[gd_scene format=3 uid="uid://c1ehbmaja4lpg"]
[ext_resource type="Script" uid="uid://b38dc40ei5ape" path="res://Scenes/Enemies/slime_enemy.gd" id="1_5uyk1"]
[ext_resource type="Texture2D" uid="uid://cwejyda54idtw" path="res://Ninja Adventure - Asset Pack/Actor/Monsters/Slime/Slime.png" id="1_18say"]
[sub_resource type="CircleShape2D" id="CircleShape2D_3rdvi"]
@@ -9,6 +10,70 @@ radius = 6.0827627
atlas = ExtResource("1_18say")
region = Rect2(0, 0, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_5uyk1"]
atlas = ExtResource("1_18say")
region = Rect2(0, 0, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_sv1en"]
atlas = ExtResource("1_18say")
region = Rect2(0, 16, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_1u7tg"]
atlas = ExtResource("1_18say")
region = Rect2(0, 32, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_jwhlm"]
atlas = ExtResource("1_18say")
region = Rect2(0, 48, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_rh62s"]
atlas = ExtResource("1_18say")
region = Rect2(32, 0, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_n4i4n"]
atlas = ExtResource("1_18say")
region = Rect2(32, 16, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_ld0qu"]
atlas = ExtResource("1_18say")
region = Rect2(32, 32, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_cmhxm"]
atlas = ExtResource("1_18say")
region = Rect2(32, 48, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_2tcdh"]
atlas = ExtResource("1_18say")
region = Rect2(48, 0, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_nqitv"]
atlas = ExtResource("1_18say")
region = Rect2(48, 16, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_gf5l7"]
atlas = ExtResource("1_18say")
region = Rect2(48, 32, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_1xypq"]
atlas = ExtResource("1_18say")
region = Rect2(48, 48, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_n2bhi"]
atlas = ExtResource("1_18say")
region = Rect2(16, 0, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_33pvu"]
atlas = ExtResource("1_18say")
region = Rect2(16, 16, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_mek6i"]
atlas = ExtResource("1_18say")
region = Rect2(16, 32, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_hiyn6"]
atlas = ExtResource("1_18say")
region = Rect2(16, 48, 16, 16)
[sub_resource type="SpriteFrames" id="SpriteFrames_chgu2"]
animations = [{
"frames": [{
@@ -18,10 +83,84 @@ animations = [{
"loop": 1,
"name": &"default",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_5uyk1")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_sv1en")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_1u7tg")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_jwhlm")
}],
"loop": 1,
"name": &"move_down",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_rh62s")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_n4i4n")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_ld0qu")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_cmhxm")
}],
"loop": 1,
"name": &"move_left",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_2tcdh")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_nqitv")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_gf5l7")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_1xypq")
}],
"loop": 1,
"name": &"move_right",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_n2bhi")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_33pvu")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_mek6i")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_hiyn6")
}],
"loop": 1,
"name": &"move_up",
"speed": 5.0
}]
[sub_resource type="CircleShape2D" id="CircleShape2D_chgu2"]
radius = 60.0
[node name="SlimeEnemy" type="CharacterBody2D" unique_id=384578683]
collision_layer = 16
motion_mode = 1
script = ExtResource("1_5uyk1")
speed = 50
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=627034710]
position = Vector2(0, 2)
@@ -29,3 +168,15 @@ shape = SubResource("CircleShape2D_3rdvi")
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="." unique_id=1489783052]
sprite_frames = SubResource("SpriteFrames_chgu2")
animation = &"move_up"
autoplay = "default"
[node name="PlayerDetectArea" type="Area2D" parent="." unique_id=2126126428]
collision_layer = 16
collision_mask = 3
[node name="CollisionShape2D" type="CollisionShape2D" parent="PlayerDetectArea" unique_id=1018550471]
shape = SubResource("CircleShape2D_chgu2")
debug_color = Color(0.19248506, 0.64210063, 0, 0.41960785)
[connection signal="body_entered" from="PlayerDetectArea" to="." method="_on_player_detect_area_body_entered"]