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

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