Play zones
This commit is contained in:
parent
4826bc4035
commit
7cce797ded
7 changed files with 150 additions and 58 deletions
|
@ -1,16 +1,48 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue