Реализация интерактивного объекта

This commit is contained in:
Евгений Титаренко 2024-08-25 13:41:52 +03:00
parent 872d650b3d
commit de683399fa
4 changed files with 94 additions and 70 deletions

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=20 format=3 uid="uid://c1abgp6quvo3o"] [gd_scene load_steps=25 format=3 uid="uid://c1abgp6quvo3o"]
[ext_resource type="Texture2D" uid="uid://b5baxx5o21qy1" path="res://resources/sprites/Sub/Sub.png" id="1_a6v5r"] [ext_resource type="Texture2D" uid="uid://b5baxx5o21qy1" path="res://resources/sprites/Sub/Sub.png" id="1_a6v5r"]
[ext_resource type="Script" path="res://scripts/Day.cs" id="1_cko08"] [ext_resource type="Script" path="res://scripts/Day.cs" id="1_cko08"]
@ -10,8 +10,8 @@
[ext_resource type="SpriteFrames" uid="uid://532buo56y4q2" path="res://resources/sprites/npcs/crewmate.tres" id="7_j5wfn"] [ext_resource type="SpriteFrames" uid="uid://532buo56y4q2" path="res://resources/sprites/npcs/crewmate.tres" id="7_j5wfn"]
[ext_resource type="PackedScene" uid="uid://dfdsnwub212o6" path="res://prefabs/Player.tscn" id="8_csxln"] [ext_resource type="PackedScene" uid="uid://dfdsnwub212o6" path="res://prefabs/Player.tscn" id="8_csxln"]
[ext_resource type="AudioStream" uid="uid://be35iuapayv0u" path="res://resources/music/Day1.wav" id="9_43nb0"] [ext_resource type="AudioStream" uid="uid://be35iuapayv0u" path="res://resources/music/Day1.wav" id="9_43nb0"]
[ext_resource type="PackedScene" uid="uid://x6pqolxtgwvy" path="res://prefabs/Interactable.tscn" id="12_p32yk"]
[ext_resource type="SpriteFrames" uid="uid://wu7pmuvjw5qm" path="res://resources/sprites/npcs/Empty.tres" id="10_medl6"] [ext_resource type="SpriteFrames" uid="uid://wu7pmuvjw5qm" path="res://resources/sprites/npcs/Empty.tres" id="10_medl6"]
[ext_resource type="PackedScene" uid="uid://x6pqolxtgwvy" path="res://prefabs/Interactable.tscn" id="12_p32yk"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_h0540"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_h0540"]
size = Vector2(640, 24) size = Vector2(640, 24)
@ -262,4 +262,6 @@ SpriteOffset = Vector2(0, -30)
AreaRadius = 20.0 AreaRadius = 20.0
IsEnabled = false IsEnabled = false
[connection signal="DialogEnded" from="NPCs/Ivan" to="." method="ChangeDay"] [connection signal="DialogEnded" from="NPCs/Ivan" to="." method="EnableChangeDay"]
[connection signal="PlayerLeft" from="EndDay" to="." method="RemoveEndDay"]
[connection signal="PlayerNearBy" from="EndDay" to="." method="AssignEndDay"]

View file

@ -33,6 +33,14 @@ public partial class Day : Node2D
_player.CurrentState = Player.State.Wait; _player.CurrentState = Player.State.Wait;
} }
public void AssignEndDay()
{
_player.InteractableObjects.Add(_endDay);
}
public void RemoveEndDay()
{
_player.InteractableObjects.Remove(_endDay);
}
public void ChangeDay() public void ChangeDay()
{ {
@ -41,7 +49,7 @@ public partial class Day : Node2D
public void EnableEndDay() public void EnableEndDay()
{ {
_endDay.IsEnabled = true;
} }
// Called every frame. 'delta' is the elapsed time since the previous frame. // Called every frame. 'delta' is the elapsed time since the previous frame.

View file

@ -8,9 +8,13 @@ public partial class Interactable : Node2D
[Signal] [Signal]
public delegate void PlayerNearByEventHandler(Player player); public delegate void PlayerNearByEventHandler(Player player);
[Signal] [Signal]
public delegate void PlayerLeftEventHandler(Player player); public delegate void PlayerLeftEventHandler(Player player);
[Signal]
public delegate void OnInteractEventHandler();
[Export] public Vector2 SpriteOffset; [Export] public Vector2 SpriteOffset;
[Export] public float AreaRadius; [Export] public float AreaRadius;
[Export] public bool IsEnabled; [Export] public bool IsEnabled;
@ -57,6 +61,11 @@ public partial class Interactable : Node2D
} }
} }
public void Interact()
{
EmitSignal(SignalName.OnInteract);
}
private void _on_area_2d_body_entered(Node2D body) private void _on_area_2d_body_entered(Node2D body)
{ {
if (IsEnabled && body is Player player) if (IsEnabled && body is Player player)

View file

@ -257,6 +257,11 @@ public partial class Player : CharacterBody2D
_nextPosition = door.Exit.GlobalPosition; _nextPosition = door.Exit.GlobalPosition;
CurrentState = State.Wait; CurrentState = State.Wait;
} }
if (InteractableObject is Interactable obj)
{
obj.Interact();
}
} }
if (@event.IsActionPressed("show_chat")) if (@event.IsActionPressed("show_chat"))