Compare commits
2 Commits
12bbfdceaf
...
9b45bfa4fd
Author | SHA1 | Date | |
---|---|---|---|
9b45bfa4fd | |||
6dec7dd720 |
@@ -19,7 +19,7 @@ public class GameManager : IEntity
|
||||
|
||||
|
||||
private IList<IGameObject> _gameObjects = new List<IGameObject>(Constants.GAME_OBJECTS_SIZE_INITIAL);
|
||||
private IList<IDrawable> _drawables = new List<IDrawable>(Constants.DRAWABLE_OBJECTS_SIZE_INITIAL);
|
||||
private IList<IDisplayable> _drawables = new List<IDisplayable>(Constants.DRAWABLE_OBJECTS_SIZE_INITIAL);
|
||||
|
||||
private IStateEnable _stateEnable = null!;
|
||||
private GameObjectFactory _gameObjectFactory = null!;
|
||||
@@ -37,12 +37,25 @@ public class GameManager : IEntity
|
||||
|
||||
public bool Initialized => _initialized;
|
||||
public IList<IGameObject> GameObjects => _gameObjects;
|
||||
public IStateEnable StateEnable { get { if (_stateEnable is null) Assign(new StateEnableFactory().Instantiate(this)); return _stateEnable; } }
|
||||
public IStateEnable StateEnable
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_stateEnable is null)
|
||||
{
|
||||
Assign(new StateEnableFactory().Instantiate(this));
|
||||
if (_stateEnable is null)
|
||||
throw NotAssignedException.From(this, _stateEnable);
|
||||
}
|
||||
|
||||
return _stateEnable;
|
||||
}
|
||||
}
|
||||
|
||||
public void RegisterGameObject(IGameObject gameObject)
|
||||
{
|
||||
if (_gameObjects.Contains(gameObject))
|
||||
throw new Exception($"{nameof(IGameComponent)} named {gameObject.Name} is already registered to the {nameof(GameManager)}.");
|
||||
throw new Exception($"{nameof(IGameObject)} named {gameObject.Name} is already registered to the {nameof(GameManager)}.");
|
||||
|
||||
Register(gameObject);
|
||||
}
|
||||
@@ -57,7 +70,7 @@ public class GameManager : IEntity
|
||||
public IGameObject RemoveGameObject(IGameObject gameObject)
|
||||
{
|
||||
if (!_gameObjects.Contains(gameObject))
|
||||
throw new Exception($"{nameof(IGameComponent)} named {gameObject.Name} is not registered to the {nameof(GameManager)}.");
|
||||
throw new Exception($"{nameof(IGameObject)} named {gameObject.Name} is not registered to the {nameof(GameManager)}.");
|
||||
|
||||
Unregister(gameObject);
|
||||
return gameObject;
|
||||
@@ -125,8 +138,7 @@ public class GameManager : IEntity
|
||||
{
|
||||
spriteBatch.Begin();
|
||||
|
||||
foreach (var gameObject in GameObjects)
|
||||
if (gameObject.BehaviourController.TryGetBehaviour<IDisplayable>(out var drawable))
|
||||
foreach (var drawable in _drawables)
|
||||
drawable.Draw(spriteBatch);
|
||||
|
||||
spriteBatch.End();
|
||||
@@ -140,7 +152,7 @@ public class GameManager : IEntity
|
||||
gameObject.BehaviourController.OnBehaviourRemoved -= OnBehaviourRemove;
|
||||
gameObject.OnFinalized -= OnGameObjectFinalize;
|
||||
|
||||
if (gameObject.BehaviourController.TryGetBehaviour<IDrawable>(out var drawable))
|
||||
if (gameObject.BehaviourController.TryGetBehaviour<IDisplayable>(out var drawable))
|
||||
_drawables.Remove(drawable);
|
||||
|
||||
_gameObjects.Remove(gameObject);
|
||||
@@ -152,7 +164,7 @@ public class GameManager : IEntity
|
||||
gameObject.BehaviourController.OnBehaviourRemoved += OnBehaviourRemove;
|
||||
gameObject.OnFinalized += OnGameObjectFinalize;
|
||||
|
||||
if (gameObject.BehaviourController.TryGetBehaviour<IDrawable>(out var drawable))
|
||||
if (gameObject.BehaviourController.TryGetBehaviour<IDisplayable>(out var drawable))
|
||||
_drawables.Add(drawable);
|
||||
|
||||
_gameObjects.Add(gameObject);
|
||||
@@ -166,13 +178,13 @@ public class GameManager : IEntity
|
||||
|
||||
private void OnBehaviourAdd(IBehaviourController controller, IBehaviour behaviour)
|
||||
{
|
||||
if (behaviour is IDrawable drawable)
|
||||
if (behaviour is IDisplayable drawable)
|
||||
_drawables.Add(drawable);
|
||||
}
|
||||
|
||||
private void OnBehaviourRemove(IBehaviourController controller, IBehaviour behaviour)
|
||||
{
|
||||
if (behaviour is IDrawable drawable)
|
||||
if (behaviour is IDisplayable drawable)
|
||||
_drawables.Remove(drawable);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user