fix: IGameObjects Not Being Initialized Properly

This commit is contained in:
Syntriax 2024-10-26 22:45:24 +03:00
parent 6bc9043a86
commit 24b50eba12
3 changed files with 23 additions and 6 deletions

View File

@ -1,5 +1,7 @@
using System;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core.Abstract;
public abstract class BaseEntity : IEntity
@ -72,6 +74,8 @@ public abstract class BaseEntity : IEntity
UnassignInternal();
_stateEnable = null!;
_stateEnable.Unassign();
OnUnassigned?.Invoke(this);
return true;
}
@ -82,6 +86,8 @@ public abstract class BaseEntity : IEntity
if (IsInitialized)
return false;
NotAssignedException.Check(this, _stateEnable);
InitializeInternal();
IsInitialized = true;

View File

@ -63,6 +63,10 @@ public class GameManager : BaseEntity, IGameManager
{
_hierarchyObjects.Add(hierarchyObject);
hierarchyObject.EnterHierarchy(this);
if (!hierarchyObject.Initialize())
throw new Exception($"{nameof(hierarchyObject)} can't be finalized");
OnHierarchyObjectRegistered?.Invoke(this, hierarchyObject);
}
}
@ -85,6 +89,10 @@ public class GameManager : BaseEntity, IGameManager
{
_hierarchyObjects.Remove(hierarchyObject);
hierarchyObject.ExitHierarchy();
if (!hierarchyObject.Finalize())
throw new Exception($"{nameof(hierarchyObject)} can't be finalized");
OnHierarchyObjectUnRegistered?.Invoke(this, hierarchyObject);
}
}
@ -138,6 +146,9 @@ public class GameManager : BaseEntity, IGameManager
if (!gameObject.EnterHierarchy(this))
throw new Exception($"{nameof(gameObject)} can't enter the hierarchy");
if (!gameObject.Initialize())
throw new Exception($"{nameof(gameObject)} can't be initialized");
OnHierarchyObjectRegistered?.Invoke(this, gameObject);
OnGameObjectRegistered?.Invoke(this, gameObject);
}
@ -156,6 +167,9 @@ public class GameManager : BaseEntity, IGameManager
if (!gameObject.ExitHierarchy())
throw new Exception($"{nameof(gameObject)} can't exit the hierarchy");
if (!gameObject.Finalize())
throw new Exception($"{nameof(gameObject)} can't be finalized");
OnHierarchyObjectUnRegistered?.Invoke(this, gameObject);
OnGameObjectUnRegistered?.Invoke(this, gameObject);
}

View File

@ -18,7 +18,6 @@ public class GameObject : BaseEntity, IGameObject
private ITransform _transform = null!;
private IBehaviourController _behaviourController = null!;
private IStateEnable _stateEnable = null!;
private IGameManager? _gameManager = null;
private string _name = nameof(GameObject);
@ -47,13 +46,12 @@ public class GameObject : BaseEntity, IGameObject
NotAssignedException.Check(this, _transform);
NotAssignedException.Check(this, _behaviourController);
NotAssignedException.Check(this, _stateEnable);
NotAssignedException.Check(this, _gameManager);
}
public void Update()
{
if (!_stateEnable.Enabled)
if (!StateEnable.Enabled)
return;
OnUpdated?.Invoke(this);
@ -91,7 +89,6 @@ public class GameObject : BaseEntity, IGameObject
{
base.UnassignInternal();
_stateEnable = null!;
_transform = null!;
_behaviourController = null!;
_gameManager = null!;
@ -104,8 +101,8 @@ public class GameObject : BaseEntity, IGameObject
controller.BehaviourController.OnBehaviourAdded += OnBehaviourAdded;
controller.BehaviourController.OnBehaviourRemoved += OnBehaviourRemoved;
}
private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Initialize(); }
private void OnBehaviourAdded(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Finalize(); }
private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Finalize(); }
private void OnBehaviourAdded(IBehaviourController _, IBehaviour behaviour) { if (!IsInitialized) behaviour.Initialize(); }
bool IHierarchyObject.EnterHierarchy(IGameManager gameManager)
{