Сцена для интерактивных объектов

This commit is contained in:
Евгений Титаренко 2024-08-13 17:25:50 +03:00
parent 42db203fca
commit cf7f3bee5b
2 changed files with 66 additions and 0 deletions

24
prefabs/Interactable.tscn Normal file
View file

@ -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"]

42
scripts/Interactable.cs Normal file
View file

@ -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;
}
}
}