From cf7f3bee5b45e4963ea5a0e10543b838e3dcb07f Mon Sep 17 00:00:00 2001 From: Evgenij Titarenko Date: Tue, 13 Aug 2024 17:25:50 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D1=86=D0=B5=D0=BD=D0=B0=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=B0=D0=BA=D1=82=D0=B8?= =?UTF-8?q?=D0=B2=D0=BD=D1=8B=D1=85=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prefabs/Interactable.tscn | 24 ++++++++++++++++++++++ scripts/Interactable.cs | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 prefabs/Interactable.tscn create mode 100644 scripts/Interactable.cs 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; + } + } +}