Compare commits
6 Commits
d60537f940
...
d75bae802a
Author | SHA1 | Date | |
---|---|---|---|
d75bae802a | |||
251bd948ab | |||
5a01b01215 | |||
6f68e29fb8 | |||
8a8c09f043 | |||
44bee2df08 |
@@ -1,6 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core.Abstract;
|
namespace Syntriax.Engine.Core.Abstract;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates the class implementing it has Assignable fields that are necessary for the engine to work properly.
|
/// Indicates the class implementing it has Assignable fields that are necessary for the engine to work properly.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IAssignable { }
|
public interface IAssignable
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Callback triggered when the <see cref="IAssignable"/>'s fields are unassigned and completely ready to recycle.
|
||||||
|
/// </summary>
|
||||||
|
Action<IAssignable>? OnUnassigned { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unassign <see cref="IAssignable"/>'s all fields and make it ready to recycle.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <see cref="true"/>, if the fields are unsigned successfully, <see cref="false"/> if not.
|
||||||
|
/// </returns>
|
||||||
|
bool Unassign();
|
||||||
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
namespace Syntriax.Engine.Core.Abstract;
|
namespace Syntriax.Engine.Core.Abstract;
|
||||||
|
|
||||||
public interface IEntity : IAssignableStateEnable
|
public interface IEntity : IInitialize, IAssignableStateEnable
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@ using System;
|
|||||||
|
|
||||||
namespace Syntriax.Engine.Core.Abstract;
|
namespace Syntriax.Engine.Core.Abstract;
|
||||||
|
|
||||||
public interface IInitialize : IEntity
|
public interface IInitialize
|
||||||
{
|
{
|
||||||
Action<IInitialize>? OnInitialized { get; set; }
|
Action<IInitialize>? OnInitialized { get; set; }
|
||||||
Action<IInitialize>? OnFinalized { get; set; }
|
Action<IInitialize>? OnFinalized { get; set; }
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
using Syntriax.Engine.Core.Abstract;
|
using Syntriax.Engine.Core.Abstract;
|
||||||
using Syntriax.Engine.Core.Exceptions;
|
using Syntriax.Engine.Core.Exceptions;
|
||||||
|
|
||||||
@@ -6,6 +7,7 @@ namespace Syntriax.Engine.Core;
|
|||||||
|
|
||||||
public abstract class Behaviour : IBehaviour
|
public abstract class Behaviour : IBehaviour
|
||||||
{
|
{
|
||||||
|
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
||||||
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
|
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
|
||||||
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
|
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
|
||||||
|
|
||||||
@@ -54,7 +56,7 @@ public abstract class Behaviour : IBehaviour
|
|||||||
|
|
||||||
public bool Assign(IStateEnable stateEnable)
|
public bool Assign(IStateEnable stateEnable)
|
||||||
{
|
{
|
||||||
if (_initialized)
|
if (Initialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_stateEnable = stateEnable;
|
_stateEnable = stateEnable;
|
||||||
@@ -65,7 +67,7 @@ public abstract class Behaviour : IBehaviour
|
|||||||
|
|
||||||
public bool Assign(IBehaviourController behaviourController)
|
public bool Assign(IBehaviourController behaviourController)
|
||||||
{
|
{
|
||||||
if (_behaviourController is not null)
|
if (Initialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_behaviourController = behaviourController;
|
_behaviourController = behaviourController;
|
||||||
@@ -73,6 +75,18 @@ public abstract class Behaviour : IBehaviour
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Unassign()
|
||||||
|
{
|
||||||
|
if (Initialized)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_stateEnable = null!;
|
||||||
|
_behaviourController = null!;
|
||||||
|
|
||||||
|
OnUnassigned?.Invoke(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public bool Initialize()
|
public bool Initialize()
|
||||||
{
|
{
|
||||||
if (Initialized)
|
if (Initialized)
|
||||||
@@ -90,9 +104,6 @@ public abstract class Behaviour : IBehaviour
|
|||||||
if (!Initialized)
|
if (!Initialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_behaviourController = null!;
|
|
||||||
_stateEnable = null!;
|
|
||||||
|
|
||||||
Initialized = false;
|
Initialized = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@@ -15,6 +15,7 @@ public class BehaviourController : IBehaviourController
|
|||||||
|
|
||||||
public Action<IBehaviourController, IBehaviour>? OnBehaviourAdded { get; set; } = null;
|
public Action<IBehaviourController, IBehaviour>? OnBehaviourAdded { get; set; } = null;
|
||||||
public Action<IBehaviourController, IBehaviour>? OnBehaviourRemoved { get; set; } = null;
|
public Action<IBehaviourController, IBehaviour>? OnBehaviourRemoved { get; set; } = null;
|
||||||
|
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
||||||
public Action<IAssignableGameObject>? OnGameObjectAssigned { get; set; } = null;
|
public Action<IAssignableGameObject>? OnGameObjectAssigned { get; set; } = null;
|
||||||
|
|
||||||
|
|
||||||
@@ -87,7 +88,7 @@ public class BehaviourController : IBehaviourController
|
|||||||
|
|
||||||
public bool Assign(IGameObject gameObject)
|
public bool Assign(IGameObject gameObject)
|
||||||
{
|
{
|
||||||
if (_gameObject is not null)
|
if (GameObject is not null && GameObject.Initialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_gameObject = gameObject;
|
_gameObject = gameObject;
|
||||||
@@ -95,6 +96,16 @@ public class BehaviourController : IBehaviourController
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Unassign()
|
||||||
|
{
|
||||||
|
if (GameObject is not null && GameObject.Initialized)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_gameObject = null!;
|
||||||
|
OnUnassigned?.Invoke(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public void Update(GameTime gameTime)
|
public void Update(GameTime gameTime)
|
||||||
{
|
{
|
||||||
if (!GameObject.StateEnable.Enabled)
|
if (!GameObject.StateEnable.Enabled)
|
||||||
|
@@ -14,8 +14,12 @@ public abstract class BehaviourOverride : Behaviour
|
|||||||
{
|
{
|
||||||
OnInitialized += OnInitialize;
|
OnInitialized += OnInitialize;
|
||||||
OnFinalized += OnFinalize;
|
OnFinalized += OnFinalize;
|
||||||
|
OnUnassigned += OnUnassign;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual void OnUnassign() { }
|
||||||
|
private void OnUnassign(IAssignable assignable) => OnUnassign();
|
||||||
|
|
||||||
protected virtual void OnInitialize() { }
|
protected virtual void OnInitialize() { }
|
||||||
private void OnInitialize(IInitialize _)
|
private void OnInitialize(IInitialize _)
|
||||||
{
|
{
|
||||||
|
@@ -32,11 +32,11 @@ public class DrawableSpriteBehaviour : Behaviour, IDrawBehaviour, IAssignableSpr
|
|||||||
|
|
||||||
public bool Assign(ISprite sprite)
|
public bool Assign(ISprite sprite)
|
||||||
{
|
{
|
||||||
if (_sprite is not null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
_sprite = sprite;
|
_sprite = sprite;
|
||||||
OnSpriteAssigned?.Invoke(this);
|
OnSpriteAssigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DrawableSpriteBehaviour() => OnUnassigned += OnUnassign;
|
||||||
|
private void OnUnassign(IAssignable assignable) => _sprite = null!;
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>false</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
@@ -1,15 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
|
||||||
using Syntriax.Engine.Core.Abstract;
|
using Syntriax.Engine.Core.Abstract;
|
||||||
|
using Syntriax.Engine.Core.Exceptions;
|
||||||
using Syntriax.Engine.Core.Factory;
|
using Syntriax.Engine.Core.Factory;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core;
|
namespace Syntriax.Engine.Core;
|
||||||
|
|
||||||
public class GameManager : IInitialize
|
public class GameManager : IEntity
|
||||||
{
|
{
|
||||||
public Action<IInitialize>? OnInitialized { get; set; } = null;
|
public Action<IInitialize>? OnInitialized { get; set; } = null;
|
||||||
public Action<IInitialize>? OnFinalized { get; set; } = null;
|
public Action<IInitialize>? OnFinalized { get; set; } = null;
|
||||||
|
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
||||||
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
|
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
|
||||||
|
|
||||||
|
|
||||||
@@ -63,6 +68,8 @@ public class GameManager : IInitialize
|
|||||||
if (Initialized)
|
if (Initialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
NotAssignedException.Check(this, StateEnable);
|
||||||
|
|
||||||
foreach (var gameObject in GameObjects)
|
foreach (var gameObject in GameObjects)
|
||||||
gameObject.Initialize();
|
gameObject.Initialize();
|
||||||
|
|
||||||
@@ -84,7 +91,7 @@ public class GameManager : IInitialize
|
|||||||
|
|
||||||
public bool Assign(IStateEnable stateEnable)
|
public bool Assign(IStateEnable stateEnable)
|
||||||
{
|
{
|
||||||
if (_stateEnable is not null)
|
if (Initialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_stateEnable = stateEnable;
|
_stateEnable = stateEnable;
|
||||||
@@ -92,6 +99,16 @@ public class GameManager : IInitialize
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Unassign()
|
||||||
|
{
|
||||||
|
if (Initialized)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_stateEnable = null!;
|
||||||
|
OnUnassigned?.Invoke(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public void Update(GameTime time)
|
public void Update(GameTime time)
|
||||||
{
|
{
|
||||||
foreach (var gameObject in GameObjects)
|
foreach (var gameObject in GameObjects)
|
||||||
|
@@ -11,6 +11,7 @@ public class GameObject : IGameObject
|
|||||||
{
|
{
|
||||||
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
|
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
|
||||||
public Action<IAssignableTransform>? OnTransformAssigned { get; set; } = null;
|
public Action<IAssignableTransform>? OnTransformAssigned { get; set; } = null;
|
||||||
|
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
||||||
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
|
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
|
||||||
|
|
||||||
public Action<IEntity>? OnNameChanged { get; set; } = null;
|
public Action<IEntity>? OnNameChanged { get; set; } = null;
|
||||||
@@ -92,17 +93,13 @@ public class GameObject : IGameObject
|
|||||||
behaviour => behaviour.Finalize()
|
behaviour => behaviour.Finalize()
|
||||||
);
|
);
|
||||||
|
|
||||||
_transform = null!;
|
|
||||||
_behaviourController = null!;
|
|
||||||
_stateEnable = null!;
|
|
||||||
|
|
||||||
Initialized = false;
|
Initialized = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Assign(IStateEnable stateEnable)
|
public bool Assign(IStateEnable stateEnable)
|
||||||
{
|
{
|
||||||
if (_stateEnable is not null)
|
if (Initialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_stateEnable = stateEnable;
|
_stateEnable = stateEnable;
|
||||||
@@ -112,7 +109,7 @@ public class GameObject : IGameObject
|
|||||||
|
|
||||||
public bool Assign(ITransform transform)
|
public bool Assign(ITransform transform)
|
||||||
{
|
{
|
||||||
if (_transform is not null)
|
if (Initialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_transform = transform;
|
_transform = transform;
|
||||||
@@ -122,7 +119,7 @@ public class GameObject : IGameObject
|
|||||||
|
|
||||||
public bool Assign(IBehaviourController behaviourController)
|
public bool Assign(IBehaviourController behaviourController)
|
||||||
{
|
{
|
||||||
if (_behaviourController is not null)
|
if (Initialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_behaviourController = behaviourController;
|
_behaviourController = behaviourController;
|
||||||
@@ -130,6 +127,19 @@ public class GameObject : IGameObject
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Unassign()
|
||||||
|
{
|
||||||
|
if (Initialized)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_stateEnable = null!;
|
||||||
|
_transform = null!;
|
||||||
|
_behaviourController = null!;
|
||||||
|
|
||||||
|
OnUnassigned?.Invoke(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public GameObject() { OnBehaviourControllerAssigned += ConnectBehaviourController; }
|
public GameObject() { OnBehaviourControllerAssigned += ConnectBehaviourController; }
|
||||||
private void ConnectBehaviourController(IAssignableBehaviourController controller)
|
private void ConnectBehaviourController(IAssignableBehaviourController controller)
|
||||||
{
|
{
|
||||||
|
@@ -6,6 +6,7 @@ namespace Syntriax.Engine.Core;
|
|||||||
|
|
||||||
public class StateEnable : IStateEnable
|
public class StateEnable : IStateEnable
|
||||||
{
|
{
|
||||||
|
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
||||||
public Action<IAssignableEntity>? OnEntityAssigned { get; set; } = null;
|
public Action<IAssignableEntity>? OnEntityAssigned { get; set; } = null;
|
||||||
public Action<IStateEnable>? OnEnabledChanged { get; set; } = null;
|
public Action<IStateEnable>? OnEnabledChanged { get; set; } = null;
|
||||||
|
|
||||||
@@ -29,11 +30,21 @@ public class StateEnable : IStateEnable
|
|||||||
|
|
||||||
public bool Assign(IEntity entity)
|
public bool Assign(IEntity entity)
|
||||||
{
|
{
|
||||||
if (_entity is not null)
|
if (_entity is not null && _entity.Initialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_entity = entity;
|
_entity = entity;
|
||||||
OnEntityAssigned?.Invoke(this);
|
OnEntityAssigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Unassign()
|
||||||
|
{
|
||||||
|
if (_entity is null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_entity = null!;
|
||||||
|
OnUnassigned?.Invoke(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -59,16 +59,26 @@ public class KeyboardInputsBehaviour : BehaviourOverride, IKeyboardInputs
|
|||||||
for (int i = 0; i < cachePressedCurrentlyCount; i++)
|
for (int i = 0; i < cachePressedCurrentlyCount; i++)
|
||||||
{
|
{
|
||||||
Keys currentlyPressedKey = cachePressedCurrently[i];
|
Keys currentlyPressedKey = cachePressedCurrently[i];
|
||||||
if (!WasPressed(currentlyPressedKey))
|
|
||||||
if (OnPressed.TryGetValue(currentlyPressedKey, out var action))
|
if (!OnPressed.TryGetValue(currentlyPressedKey, out var action))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (WasPressed(currentlyPressedKey))
|
||||||
|
continue;
|
||||||
|
|
||||||
action.Invoke(this, currentlyPressedKey);
|
action.Invoke(this, currentlyPressedKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < cachePressedPreviouslyCount; i++)
|
for (int i = 0; i < cachePressedPreviouslyCount; i++)
|
||||||
{
|
{
|
||||||
Keys previouslyPressedKey = cachePressedPreviously[i];
|
Keys previouslyPressedKey = cachePressedPreviously[i];
|
||||||
if (!IsPressed(previouslyPressedKey))
|
|
||||||
if (OnReleased.TryGetValue(previouslyPressedKey, out var action))
|
if (!OnReleased.TryGetValue(previouslyPressedKey, out var action))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (IsPressed(previouslyPressedKey))
|
||||||
|
continue;
|
||||||
|
|
||||||
action.Invoke(this, previouslyPressedKey);
|
action.Invoke(this, previouslyPressedKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user