feat: More Control Over Displayables

This commit is contained in:
2023-11-27 10:37:39 +03:00
parent cd75c14d83
commit 12bbfdceaf
12 changed files with 676 additions and 86 deletions

View File

@@ -0,0 +1,8 @@
using Microsoft.Xna.Framework.Graphics;
namespace Syntriax.Engine.Core.Abstract;
public interface IDisplayable
{
public void Draw(SpriteBatch spriteBatch);
}

View File

@@ -8,10 +8,6 @@ namespace Syntriax.Engine.Core.Abstract;
public interface ISprite
{
Action<ISprite>? OnTextureChanged { get; set; }
Action<ISprite>? OnColorChanged { get; set; }
Action<ISprite>? OnDepthChanged { get; set; }
Texture2D Texture2D { get; set; }
Color Color { get; set; }
float Depth { get; set; }
}

View File

@@ -1,42 +0,0 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core.Abstract;
using ISprite = Syntriax.Engine.Core.Abstract.ISprite;
namespace Syntriax.Engine.Core.Behaviours;
public class DrawableSpriteBehaviour : Behaviour, IDrawBehaviour, IAssignableSprite
{
public Action<IAssignableSprite>? OnSpriteAssigned { get; set; } = null;
private ISprite _sprite = null!;
public ISprite Sprite => _sprite;
public void Draw(SpriteBatch spriteBatch)
{
if (!BehaviourController.GameObject.StateEnable.Enabled || !StateEnable.Enabled)
return;
Vector2 position = BehaviourController.GameObject.Transform.Position;
Vector2 scale = BehaviourController.GameObject.Transform.Scale;
Rectangle rectangle = new Rectangle((int)position.X, (int)position.Y, (int)(Sprite.Texture2D.Width * scale.X), (int)(Sprite.Texture2D.Height * scale.Y));
spriteBatch.Draw(Sprite.Texture2D, rectangle, Sprite.Color);
}
public bool Assign(ISprite sprite)
{
_sprite = sprite;
OnSpriteAssigned?.Invoke(this);
return true;
}
public DrawableSpriteBehaviour() => OnUnassigned += OnUnassign;
private void OnUnassign(IAssignable assignable) => _sprite = null!;
}

View File

@@ -1,9 +0,0 @@
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core.Behaviours;
public interface IDrawBehaviour : IBehaviour
{
public void Draw(SpriteBatch spriteBatch);
}

View File

@@ -126,7 +126,7 @@ public class GameManager : IEntity
spriteBatch.Begin();
foreach (var gameObject in GameObjects)
if (gameObject.BehaviourController.TryGetBehaviour<Behaviours.IDrawBehaviour>(out var drawable))
if (gameObject.BehaviourController.TryGetBehaviour<IDisplayable>(out var drawable))
drawable.Draw(spriteBatch);
spriteBatch.End();

View File

@@ -11,12 +11,8 @@ namespace Syntriax.Engine.Core;
public class Sprite : ISprite
{
public Action<ISprite>? OnTextureChanged { get; set; }
public Action<ISprite>? OnColorChanged { get; set; }
public Action<ISprite>? OnDepthChanged { get; set; }
private Texture2D _texture = null!;
private Color _color = Color.White;
private float _depth = 0f;
public Texture2D Texture2D
{
@@ -30,30 +26,4 @@ public class Sprite : ISprite
OnTextureChanged?.Invoke(this);
}
}
public Color Color
{
get => _color;
set
{
if (_color == value)
return;
_color = value;
OnColorChanged?.Invoke(this);
}
}
public float Depth
{
get => _depth;
set
{
if (_depth == value)
return;
_depth = value;
OnDepthChanged?.Invoke(this);
}
}
}