feat: More Control Over Displayables
This commit is contained in:
8
Engine.Core/Abstract/IDisplayable.cs
Normal file
8
Engine.Core/Abstract/IDisplayable.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace Syntriax.Engine.Core.Abstract;
|
||||
|
||||
public interface IDisplayable
|
||||
{
|
||||
public void Draw(SpriteBatch spriteBatch);
|
||||
}
|
@@ -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; }
|
||||
}
|
||||
|
@@ -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!;
|
||||
}
|
@@ -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);
|
||||
}
|
@@ -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();
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user