Syntriax.Engine/Engine.Core/Behaviours/DrawableBehaviour.cs

43 lines
1.1 KiB
C#

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 (!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)
{
if (_sprite is not null)
return false;
_sprite = sprite;
OnSpriteAssigned?.Invoke(this);
return true;
}
}