only respawn the enemy if it is off screen and the camera is not looking at its spawn point

This commit is contained in:
Chris Cromer 2022-11-21 01:43:45 -03:00
parent 160f86f35a
commit 3dfc4dd04d

View File

@ -6,14 +6,29 @@ public class Enemy : KinematicBody2D
[Export]
private Vector2 _startPosition;
public Vector2 StartPosition { get { return _startPosition; } }
private Event _event;
public override void _Ready()
{
_event = GetNode<Event>("/root/Event");
_startPosition = Position;
}
public virtual void Reset()
{
Position = _startPosition;
// Shove him off screen somewhere until we can respawn him
Position = new Vector2(-100, - 100);
_event.Connect("CameraView", this, "OnCameraView");
}
public void OnCameraView(Rect2 rect)
{
rect.Size += new Vector2(32, 32);
if (!rect.HasPoint(StartPosition))
{
// We can respawn him because the player is not in the spawn point
_event.Disconnect("CameraView", this, "OnCameraView");
Position = StartPosition;
}
}
}