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

43 lines
1.3 KiB
C#
Raw Normal View History

2023-11-23 22:07:49 +03:00
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)
2023-11-23 22:07:49 +03:00
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;
}
2023-11-24 17:20:43 +03:00
public DrawableSpriteBehaviour() => OnUnassigned += OnUnassign;
private void OnUnassign(IAssignable assignable) => _sprite = null!;
2023-11-23 22:07:49 +03:00
}