From 3dfc4dd04d1e099e54df1c4b41ffd37172612400 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Mon, 21 Nov 2022 01:43:45 -0300 Subject: [PATCH] only respawn the enemy if it is off screen and the camera is not looking at its spawn point --- scenes/enemies/Enemy.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/scenes/enemies/Enemy.cs b/scenes/enemies/Enemy.cs index 5a3c614..ba9be87 100644 --- a/scenes/enemies/Enemy.cs +++ b/scenes/enemies/Enemy.cs @@ -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("/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; + } } }