Fixed crash on some Entity removals

This commit is contained in:
Matt Thorson 2020-05-30 16:19:46 -07:00
parent 4daafca0b7
commit 9bb6ac6a73
3 changed files with 17 additions and 22 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Diagnostics;
namespace Strawberry namespace Strawberry
{ {
@ -56,10 +57,8 @@ namespace Strawberry
private Result<bool> Set(TIndex to) private Result<bool> Set(TIndex to)
{ {
if (!states.ContainsKey(to)) Debug.Assert(states.ContainsKey(to), "State does not exist in this State Machine. Call Add() first!");
Runtime.FatalError("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 (inStateCall)
Runtime.FatalError("Cannot set State directly from inside a State Enter/Exit/Update call. Return the desired State change instead.");
if (to != state) if (to != state)
{ {

View File

@ -29,12 +29,12 @@ namespace Strawberry
delete c; delete c;
} }
public void Added(Scene scene) private void Added(Scene scene)
{ {
Scene = scene; Scene = scene;
} }
public void Removed() private void Removed()
{ {
Scene = null; Scene = null;
} }
@ -81,7 +81,7 @@ namespace Strawberry
return component; return component;
} }
public void UpdateLists() private void UpdateLists()
{ {
if (toRemove.Count > 0) if (toRemove.Count > 0)
{ {
@ -425,10 +425,5 @@ namespace Strawberry
{ {
Draw.Rect(SceneHitbox, color); Draw.Rect(SceneHitbox, color);
} }
static public int operator<=>(Entity a, Entity b)
{
return a.Depth <=> b.Depth;
}
} }
} }

View File

@ -65,14 +65,14 @@ namespace Strawberry
public virtual void Update() public virtual void Update()
{ {
UpdateLists(); UpdateLists();
for (var e in entities) for (let e in entities)
if (e.Active) if (e.Active)
e.Update(); e.Update();
} }
public virtual void Draw() public virtual void Draw()
{ {
for (var e in entities) for (let e in entities)
if (e.Visible) if (e.Visible)
e.Draw(); e.Draw();
} }
@ -95,11 +95,13 @@ namespace Strawberry
{ {
if (toRemove.Count > 0) if (toRemove.Count > 0)
{ {
for (var e in toRemove) for (let e in toRemove)
{ {
Calc.Log(scope => e.GetType().GetName);
entities.Remove(e); entities.Remove(e);
UntrackEntity(e); UntrackEntity(e);
e.Removed(); e.[Friend]Removed();
if (e.DeleteOnRemove) if (e.DeleteOnRemove)
delete e; delete e;
} }
@ -109,22 +111,21 @@ namespace Strawberry
if (toAdd.Count > 0) if (toAdd.Count > 0)
{ {
for (var e in toAdd) for (let e in toAdd)
{ {
entities.Add(e); entities.Add(e);
TrackEntity(e); TrackEntity(e);
e.Added(this); e.[Friend]Added(this);
} }
} }
for (var e in entities) for (let e in entities)
e.UpdateLists(); e.[Friend]UpdateLists();
if (toAdd.Count > 0) if (toAdd.Count > 0)
{ {
for (var e in toAdd) for (let e in toAdd)
e.Started(); e.Started();
toAdd.Clear(); toAdd.Clear();
} }
} }