SupaLidlGame/BoundingBoxes/ConnectorBox.cs

72 lines
1.5 KiB
C#
Raw Normal View History

2023-03-22 21:22:51 -07:00
using Godot;
using System;
2023-03-25 10:21:24 -07:00
using SupaLidlGame.Characters;
2023-03-22 21:22:51 -07:00
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.BoundingBoxes;
public partial class ConnectorBox : Area2D
2023-03-22 21:22:51 -07:00
{
2023-06-03 18:21:46 -07:00
[Signal]
public delegate void RequestedEnterEventHandler(
ConnectorBox box,
Player player);
2023-03-25 10:21:24 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public string ToArea { get; set; }
2023-03-22 21:22:51 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public string ToConnector { get; set; }
2023-03-25 10:21:24 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public string Identifier { get; set; }
2023-03-26 10:53:45 -07:00
2023-06-03 18:21:46 -07:00
/// <summary>
/// Determines if the connector requires the user to interact to enter
/// the connector
/// </summary>
[Export]
public bool RequiresInteraction { get; set; } = false;
2023-03-25 10:21:24 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public CollisionShape2D Collision { get; set; }
2023-03-25 10:21:24 -07:00
2023-06-03 18:21:46 -07:00
private Player _player = null;
2023-03-25 10:21:24 -07:00
2023-06-03 18:21:46 -07:00
public override void _Ready()
{
if (Collision is null)
2023-03-25 10:21:24 -07:00
{
2023-06-03 18:21:46 -07:00
throw new NullReferenceException("Collision not specified");
}
2023-03-25 10:21:24 -07:00
2023-06-03 18:21:46 -07:00
BodyEntered += (Node2D body) =>
{
if (body is Player player)
2023-03-25 10:21:24 -07:00
{
2023-06-03 18:21:46 -07:00
_player = player;
}
};
2023-03-25 10:21:24 -07:00
2023-06-03 18:21:46 -07:00
BodyExited += (Node2D body) =>
{
if (body is Player)
2023-03-25 10:21:24 -07:00
{
2023-06-03 18:21:46 -07:00
_player = null;
}
};
}
2023-03-25 10:21:24 -07:00
2023-06-03 18:21:46 -07:00
public override void _Process(double delta)
{
if (Input.IsActionJustReleased("interact"))
2023-03-25 10:21:24 -07:00
{
2023-06-03 18:21:46 -07:00
if (_player is not null)
2023-03-25 10:21:24 -07:00
{
2023-06-03 18:21:46 -07:00
EmitSignal(SignalName.RequestedEnter, this, _player);
2023-03-25 10:21:24 -07:00
}
}
2023-06-03 18:21:46 -07:00
base._Process(delta);
2023-03-22 21:22:51 -07:00
}
}