mirror of
				https://github.com/MaddyThorson/StrawberryBF.git
				synced 2025-11-04 01:41:33 +08:00 
			
		
		
		
	Fixed crash on some Entity removals
This commit is contained in:
		@ -1,5 +1,6 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections;
 | 
			
		||||
using System.Diagnostics;
 | 
			
		||||
 | 
			
		||||
namespace Strawberry
 | 
			
		||||
{
 | 
			
		||||
@ -56,10 +57,8 @@ namespace Strawberry
 | 
			
		||||
 | 
			
		||||
		private Result<bool> Set(TIndex to)
 | 
			
		||||
		{
 | 
			
		||||
			if (!states.ContainsKey(to))
 | 
			
		||||
				Runtime.FatalError("State does not exist in this State Machine. Call Add() first!");
 | 
			
		||||
			if (inStateCall)
 | 
			
		||||
				Runtime.FatalError("Cannot set State directly from inside a State Enter/Exit/Update call. Return the desired State change instead.");
 | 
			
		||||
			Debug.Assert(states.ContainsKey(to), "State does not exist in this State Machine. Call Add() first!");
 | 
			
		||||
			Runtime.Assert(!inStateCall, "Cannot set State directly from inside a State Enter/Exit/Update call. Return the desired State change instead.");
 | 
			
		||||
 | 
			
		||||
			if (to != state)
 | 
			
		||||
			{
 | 
			
		||||
 | 
			
		||||
@ -29,12 +29,12 @@ namespace Strawberry
 | 
			
		||||
				delete c;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public void Added(Scene scene)
 | 
			
		||||
		private void Added(Scene scene)
 | 
			
		||||
		{
 | 
			
		||||
			Scene = scene;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public void Removed()
 | 
			
		||||
		private void Removed()
 | 
			
		||||
		{
 | 
			
		||||
			Scene = null;
 | 
			
		||||
		}
 | 
			
		||||
@ -81,7 +81,7 @@ namespace Strawberry
 | 
			
		||||
			return component;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public void UpdateLists()
 | 
			
		||||
		private void UpdateLists()
 | 
			
		||||
		{
 | 
			
		||||
			if (toRemove.Count > 0)
 | 
			
		||||
			{
 | 
			
		||||
@ -425,10 +425,5 @@ namespace Strawberry
 | 
			
		||||
		{
 | 
			
		||||
			Draw.Rect(SceneHitbox, color);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		static public int operator<=>(Entity a, Entity b)
 | 
			
		||||
		{
 | 
			
		||||
			return a.Depth <=> b.Depth;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -65,14 +65,14 @@ namespace Strawberry
 | 
			
		||||
		public virtual void Update()
 | 
			
		||||
		{
 | 
			
		||||
			UpdateLists();
 | 
			
		||||
			for (var e in entities)
 | 
			
		||||
			for (let e in entities)
 | 
			
		||||
				if (e.Active)
 | 
			
		||||
					e.Update();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public virtual void Draw()
 | 
			
		||||
		{
 | 
			
		||||
			for (var e in entities)
 | 
			
		||||
			for (let e in entities)
 | 
			
		||||
				if (e.Visible)
 | 
			
		||||
					e.Draw();
 | 
			
		||||
		}
 | 
			
		||||
@ -95,11 +95,13 @@ namespace Strawberry
 | 
			
		||||
		{
 | 
			
		||||
			if (toRemove.Count > 0)
 | 
			
		||||
			{
 | 
			
		||||
				for (var e in toRemove)
 | 
			
		||||
				for (let e in toRemove)
 | 
			
		||||
				{
 | 
			
		||||
					Calc.Log(scope => e.GetType().GetName);
 | 
			
		||||
 | 
			
		||||
					entities.Remove(e);
 | 
			
		||||
					UntrackEntity(e);
 | 
			
		||||
					e.Removed();
 | 
			
		||||
					e.[Friend]Removed();
 | 
			
		||||
					if (e.DeleteOnRemove)
 | 
			
		||||
						delete e;
 | 
			
		||||
				}
 | 
			
		||||
@ -109,22 +111,21 @@ namespace Strawberry
 | 
			
		||||
 | 
			
		||||
			if (toAdd.Count > 0)
 | 
			
		||||
			{
 | 
			
		||||
				for (var e in toAdd)
 | 
			
		||||
				for (let e in toAdd)
 | 
			
		||||
				{
 | 
			
		||||
					entities.Add(e);
 | 
			
		||||
					TrackEntity(e);
 | 
			
		||||
					e.Added(this);
 | 
			
		||||
					e.[Friend]Added(this);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			for (var e in entities)
 | 
			
		||||
				e.UpdateLists();
 | 
			
		||||
			for (let e in entities)
 | 
			
		||||
				e.[Friend]UpdateLists();
 | 
			
		||||
 | 
			
		||||
			if (toAdd.Count > 0)
 | 
			
		||||
			{
 | 
			
		||||
				for (var e in toAdd)
 | 
			
		||||
				for (let e in toAdd)
 | 
			
		||||
					e.Started();
 | 
			
		||||
 | 
			
		||||
				toAdd.Clear();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user