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? 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!; }