From de683399fac5aeaa6f7f1839717c20fb66525659 Mon Sep 17 00:00:00 2001 From: Evgenij Titarenko Date: Sun, 25 Aug 2024 13:41:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=D0=B8=D0=B2=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BE=D0=B1=D1=8A?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scenes/Day1.tscn | 8 ++- scripts/Day.cs | 12 +++- scripts/Interactable.cs | 139 +++++++++++++++++++++------------------- scripts/Player.cs | 5 ++ 4 files changed, 94 insertions(+), 70 deletions(-) diff --git a/scenes/Day1.tscn b/scenes/Day1.tscn index de3e452..7a609f7 100644 --- a/scenes/Day1.tscn +++ b/scenes/Day1.tscn @@ -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="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="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="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="PackedScene" uid="uid://x6pqolxtgwvy" path="res://prefabs/Interactable.tscn" id="12_p32yk"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_h0540"] size = Vector2(640, 24) @@ -262,4 +262,6 @@ SpriteOffset = Vector2(0, -30) AreaRadius = 20.0 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"] diff --git a/scripts/Day.cs b/scripts/Day.cs index 6f6f4a7..02c717e 100644 --- a/scripts/Day.cs +++ b/scripts/Day.cs @@ -32,7 +32,15 @@ public partial class Day : Node2D _music.VolumeDb = -40; _player.CurrentState = Player.State.Wait; } - + + public void AssignEndDay() + { + _player.InteractableObjects.Add(_endDay); + } + public void RemoveEndDay() + { + _player.InteractableObjects.Remove(_endDay); + } public void ChangeDay() { @@ -41,7 +49,7 @@ public partial class Day : Node2D public void EnableEndDay() { - + _endDay.IsEnabled = true; } // Called every frame. 'delta' is the elapsed time since the previous frame. diff --git a/scripts/Interactable.cs b/scripts/Interactable.cs index 17665ac..39c5d54 100644 --- a/scripts/Interactable.cs +++ b/scripts/Interactable.cs @@ -4,74 +4,83 @@ using System; [Tool] public partial class Interactable : Node2D { - private const double Tolerance = 1e-6; - - [Signal] - public delegate void PlayerNearByEventHandler(Player player); - [Signal] - public delegate void PlayerLeftEventHandler(Player player); - - [Export] public Vector2 SpriteOffset; - [Export] public float AreaRadius; - [Export] public bool IsEnabled; - private AnimatedSprite2D _sprite; + private const double Tolerance = 1e-6; - private CollisionShape2D _areaMesh; + [Signal] + public delegate void PlayerNearByEventHandler(Player player); - private CircleShape2D _collisionCircle; - - // 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; - _collisionCircle =(CircleShape2D)_areaMesh.Shape; - _collisionCircle.Radius = AreaRadius; - if (Engine.IsEditorHint()) - { - _sprite.Visible = true; - } - else - { - _sprite.Visible = false; - } - } + [Signal] + public delegate void PlayerLeftEventHandler(Player player); - // Called every frame. 'delta' is the elapsed time since the previous frame. - public override void _Process(double delta) - { - if (Engine.IsEditorHint()) - { - if (_sprite.Position != SpriteOffset) _sprite.Position = SpriteOffset; - if (Math.Abs(_collisionCircle.Radius - AreaRadius) > Tolerance) _collisionCircle.Radius = AreaRadius; - } + [Signal] + public delegate void OnInteractEventHandler(); - if (IsEnabled) - { - _sprite.Visible = true; - } - else - { - _sprite.Visible = false; - } - } + [Export] public Vector2 SpriteOffset; + [Export] public float AreaRadius; + [Export] public bool IsEnabled; + private AnimatedSprite2D _sprite; - private void _on_area_2d_body_entered(Node2D body) - { - if (IsEnabled && body is Player player) - { - _sprite.Visible = true; - EmitSignal(SignalName.PlayerNearBy, player); - } - } + private CollisionShape2D _areaMesh; - private void _on_area_2d_body_exited(Node2D body) - { - if (IsEnabled && body is Player player) - { - _sprite.Visible = false; - EmitSignal(SignalName.PlayerLeft, player); - } - } -} + private CircleShape2D _collisionCircle; + + // 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; + _collisionCircle = (CircleShape2D)_areaMesh.Shape; + _collisionCircle.Radius = AreaRadius; + if (Engine.IsEditorHint()) + { + _sprite.Visible = true; + } + else + { + _sprite.Visible = false; + } + } + + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(double delta) + { + if (Engine.IsEditorHint()) + { + if (_sprite.Position != SpriteOffset) _sprite.Position = SpriteOffset; + if (Math.Abs(_collisionCircle.Radius - AreaRadius) > Tolerance) _collisionCircle.Radius = AreaRadius; + } + + if (IsEnabled) + { + _sprite.Visible = true; + } + else + { + _sprite.Visible = false; + } + } + + public void Interact() + { + EmitSignal(SignalName.OnInteract); + } + + private void _on_area_2d_body_entered(Node2D body) + { + if (IsEnabled && body is Player player) + { + _sprite.Visible = true; + EmitSignal(SignalName.PlayerNearBy, player); + } + } + + private void _on_area_2d_body_exited(Node2D body) + { + if (IsEnabled && body is Player player) + { + _sprite.Visible = false; + EmitSignal(SignalName.PlayerLeft, player); + } + } +} \ No newline at end of file diff --git a/scripts/Player.cs b/scripts/Player.cs index 8feaa43..6440ba2 100644 --- a/scripts/Player.cs +++ b/scripts/Player.cs @@ -257,6 +257,11 @@ public partial class Player : CharacterBody2D _nextPosition = door.Exit.GlobalPosition; CurrentState = State.Wait; } + + if (InteractableObject is Interactable obj) + { + obj.Interact(); + } } if (@event.IsActionPressed("show_chat"))