Enhanced snail entity with new features

Added a sprite to the Snail class and implemented animations for the snail's movements. The snail now has a body and shell texture, along with an eating shadow. Also, added logic to flip the sprite based on movement direction in the 'snail_eating' state. Furthermore, made CollisionShape2D invisible for better aesthetics.
This commit is contained in:
Dan 2024-05-15 17:21:47 +01:00
parent 7d47bdbf5a
commit 5777a7f2f1
9 changed files with 213 additions and 4 deletions

View file

@ -1,9 +1,82 @@
[gd_scene load_steps=6 format=3 uid="uid://bnwvtlsvxjmel"]
[gd_scene load_steps=11 format=3 uid="uid://bnwvtlsvxjmel"]
[ext_resource type="Script" path="res://entities/scripts/snail.gd" id="1_lkvd1"]
[ext_resource type="Script" path="res://entities/scripts/finite_state_machine.gd" id="1_tejvt"]
[ext_resource type="Texture2D" uid="uid://dh8fo7865wgs" path="res://resources/textures/snail.png" id="2_yor00"]
[ext_resource type="Script" path="res://entities/snail/states/snail_sleeping.gd" id="3_wnrnl"]
[ext_resource type="Script" path="res://entities/snail/states/snail_eating.gd" id="4_1abwi"]
[ext_resource type="Texture2D" uid="uid://d30yqtob6phcj" path="res://resources/textures/snail_body.png" id="7_8spp3"]
[ext_resource type="Texture2D" uid="uid://5vt8eaihmut3" path="res://resources/textures/snail_shell.png" id="8_0v3d4"]
[sub_resource type="Animation" id="Animation_kpiuy"]
resource_name = "GoingToSleep"
length = 1.5
loop_mode = 1
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite/SnailBody:scale")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.3, 1.5),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Vector2(1, 1), Vector2(0, 0), Vector2(0, 0)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Sprite/SnailShell:rotation")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0.1, 1, 1.1, 1.2, 1.3, 1.4, 1.5),
"transitions": PackedFloat32Array(1, -2, 1, 1, 1, 1, 1),
"update": 0,
"values": [0.0, -6.28319, -6.10865, -6.28319, -6.10865, -6.28319, -6.28319]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Sprite/SnailShell:position")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0.1, 0.6, 1, 1.5),
"transitions": PackedFloat32Array(1, 1, 1, 1),
"update": 0,
"values": [Vector2(0, 0), Vector2(0, -300), Vector2(0, 0), Vector2(0, 0)]
}
tracks/3/type = "value"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("Sprite/SnailBody:position")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0, 0.4, 0.6, 1.5),
"transitions": PackedFloat32Array(1, 1, 1, 1),
"update": 0,
"values": [Vector2(60, 30), Vector2(60, -300), Vector2(60, 30), Vector2(60, 30)]
}
tracks/4/type = "value"
tracks/4/imported = false
tracks/4/enabled = true
tracks/4/path = NodePath("Sprite/ShadowEating:scale")
tracks/4/interp = 1
tracks/4/loop_wrap = true
tracks/4/keys = {
"times": PackedFloat32Array(0, 0.3, 1.5),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Vector2(1.09451, 0.620219), Vector2(0, 0), Vector2(0, 0)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_6ntaf"]
_data = {
"GoingToSleep": SubResource("Animation_kpiuy")
}
[sub_resource type="CircleShape2D" id="CircleShape2D_2whjo"]
radius = 42.0476
@ -15,9 +88,32 @@ input_pickable = true
script = ExtResource("1_lkvd1")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_6ntaf")
}
autoplay = "GoingToSleep"
[node name="Polygon2D" type="Polygon2D" parent="."]
polygon = PackedVector2Array(-8, -8, -5, -1, 10, 5, -7, 9, -25, 8, -28, -5, -20, -11)
[node name="Sprite" type="Sprite2D" parent="."]
position = Vector2(-6, 0)
scale = Vector2(0.1, 0.1)
[node name="SnailBody" type="Sprite2D" parent="Sprite"]
position = Vector2(60, 30)
scale = Vector2(1e-05, 1e-05)
texture = ExtResource("7_8spp3")
[node name="SnailShell" type="Sprite2D" parent="Sprite"]
position = Vector2(0, -168.582)
rotation = -4.71395
scale = Vector2(1, 1)
texture = ExtResource("8_0v3d4")
[node name="ShadowEating" type="Sprite2D" parent="Sprite"]
self_modulate = Color(0, 0, 0, 0.0784314)
show_behind_parent = true
position = Vector2(121, 68.925)
scale = Vector2(1e-05, 1e-05)
texture = ExtResource("2_yor00")
[node name="StateMachine" type="Node" parent="." node_paths=PackedStringArray("initial_state")]
script = ExtResource("1_tejvt")
@ -30,4 +126,5 @@ script = ExtResource("3_wnrnl")
script = ExtResource("4_1abwi")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
visible = false
shape = SubResource("CircleShape2D_2whjo")

View file

@ -3,6 +3,7 @@ class_name Snail
@onready var fsm : FiniteStateMachine = $StateMachine as FiniteStateMachine
@onready var flowers : Flowers = get_parent()
@onready var sprite : Sprite2D = $Sprite
var enabled : bool = false
var eating : bool = false

View file

@ -8,12 +8,15 @@ var eat_timer : float = 0.0
var move_to : Vector2 = Vector2.ZERO
var original_snail_scale : float = 0.1
func enter(_msg : Dictionary = {}) -> void:
Log.pr("I am a snail and I will eat!")
snail.eating = true
func exit() -> void:
snail.eating = false
snail.sprite.flip_h = false
func update(delta : float) -> void:
eat_timer += delta
@ -28,9 +31,15 @@ func physics_update(_delta : float) -> void:
move_to = snail.get_random_target()
if snail.global_position.distance_to(move_to) > 3:
snail.velocity = snail.global_position.direction_to(move_to) * snail.speed
if move_to.x > snail.global_position.x:
snail.sprite.scale.x = original_snail_scale
else:
snail.sprite.scale.x = -original_snail_scale
snail.move_and_slide()
snail.look_at(move_to)
else:
move_to = Vector2.ZERO