diff --git a/prefabs/Interactable.tscn b/prefabs/Interactable.tscn new file mode 100644 index 0000000..57ebe60 --- /dev/null +++ b/prefabs/Interactable.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=4 format=3 uid="uid://x6pqolxtgwvy"] + +[ext_resource type="Script" path="res://scripts/Interactable.cs" id="1_4jn0u"] +[ext_resource type="SpriteFrames" uid="uid://bvnfvjn7am8tc" path="res://resources/sprites/key.tres" id="2_meac6"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_w4p6k"] + +[node name="Interactable" type="Node2D"] +script = ExtResource("1_4jn0u") + +[node name="Area2D" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +shape = SubResource("CircleShape2D_w4p6k") + +[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] +visible = false +sprite_frames = ExtResource("2_meac6") +autoplay = "default" +frame = 1 +frame_progress = 0.336703 + +[connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"] +[connection signal="body_exited" from="Area2D" to="." method="_on_area_2d_body_exited"] diff --git a/scripts/Interactable.cs b/scripts/Interactable.cs new file mode 100644 index 0000000..d9fa807 --- /dev/null +++ b/scripts/Interactable.cs @@ -0,0 +1,42 @@ +using Godot; +using System; + +public partial class Interactable : Node2D +{ + [Export] public Vector2 SpriteOffset; + [Export] public float AreaRadius; + + private AnimatedSprite2D _sprite; + + private CollisionShape2D _areaMesh; + + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + _sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D"); + _areaMesh = (CollisionShape2D)FindChild("CollisionShape2D"); + _sprite.Position += SpriteOffset; + ((CircleShape2D)_areaMesh.Shape).Radius = AreaRadius; + } + + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(double delta) + { + } + + private void _on_area_2d_body_entered(Node2D body) + { + if (body is Player player) + { + _sprite.Visible = true; + } + } + + private void _on_area_2d_body_exited(Node2D body) + { + if (body is Player player) + { + _sprite.Visible = false; + } + } +}