1bit-game-jam/scripts/PlayZone.cs
2023-08-19 14:25:16 +03:00

48 lines
1.2 KiB
C#

using Godot;
public partial class PlayZone : Node2D
{
[Export] public Node2D TopLeftCorner;
[Export] public Node2D BottomRightCorner;
[Export] public Node2D PlayerSpawnPoint;
[Signal]
public delegate void ZoneEnteredEventHandler(PlayZone sender);
[Signal]
public delegate void ZoneLeftEventHandler(PlayZone sender);
public Rect2I Bounds = new Rect2I(0, 0, 0, 0);
private Area2D _area2D;
private CollisionShape2D _collisionShape2D;
public override void _Ready()
{
_area2D = (Area2D)FindChild("Area2D");
_collisionShape2D = (CollisionShape2D)FindChild("CollisionShape2D");
var size = BottomRightCorner.Position - TopLeftCorner.Position;
Bounds = new Rect2I((int)TopLeftCorner.Position.X, (int)TopLeftCorner.Position.Y, (int)size.X, (int)size.Y);
var middle = (BottomRightCorner.Position + TopLeftCorner.Position) / 2;
_area2D.Position = middle;
_collisionShape2D.Shape = new RectangleShape2D { Size = size };
}
private void BodyEntered(Node2D body)
{
if (body is not Player player)
return;
EmitSignal(SignalName.ZoneEntered, this);
}
private void BodyExited(Node2D body)
{
if (body is not Player player)
return;
EmitSignal(SignalName.ZoneLeft, this);
}
}