chore: Added Back Graphics
This commit is contained in:
26
Game/Graphics/Abstract/Assignable/IAssignableSprite.cs
Normal file
26
Game/Graphics/Abstract/Assignable/IAssignableSprite.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace Syntriax.Engine.Core.Abstract;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates the object is an <see cref="IAssignable"/> with an assignable <see cref="ISprite"/> field.
|
||||
/// </summary>
|
||||
public interface IAssignableSprite : IAssignable
|
||||
{
|
||||
/// <summary>
|
||||
/// Callback triggered when the <see cref="ISprite"/> value has has been assigned a new value.
|
||||
/// </summary>
|
||||
Action<IAssignableSprite>? OnSpriteAssigned { get; set; }
|
||||
|
||||
/// <inheritdoc cref="ISprite" />
|
||||
ISprite Sprite { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Assign a value to the <see cref="ISprite"/> field of this object
|
||||
/// </summary>
|
||||
/// <param name="sprite">New <see cref="ISprite"/> to assign.</param>
|
||||
/// <returns>
|
||||
/// <see cref="true"/>, if the value given assigned successfully assigned, <see cref="false"/> if not.
|
||||
/// </returns>
|
||||
bool Assign(ISprite sprite);
|
||||
}
|
||||
13
Game/Graphics/Abstract/ISprite.cs
Normal file
13
Game/Graphics/Abstract/ISprite.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace Syntriax.Engine.Core.Abstract;
|
||||
|
||||
// TODO Probably gonna have to rethink this
|
||||
public interface ISprite
|
||||
{
|
||||
Action<ISprite>? OnTextureChanged { get; set; }
|
||||
|
||||
Texture2D Texture2D { get; set; }
|
||||
}
|
||||
27
Game/Graphics/Sprite.cs
Normal file
27
Game/Graphics/Sprite.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Core;
|
||||
|
||||
public class Sprite : ISprite
|
||||
{
|
||||
public Action<ISprite>? OnTextureChanged { get; set; }
|
||||
|
||||
private Texture2D _texture = null!;
|
||||
|
||||
public Texture2D Texture2D
|
||||
{
|
||||
get => _texture;
|
||||
set
|
||||
{
|
||||
if (_texture == value)
|
||||
return;
|
||||
|
||||
_texture = value;
|
||||
OnTextureChanged?.Invoke(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Game/Graphics/TwoDimensional/Abstract/IDisplayable.cs
Normal file
8
Game/Graphics/TwoDimensional/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);
|
||||
}
|
||||
20
Game/Graphics/TwoDimensional/Abstract/IDisplayableSprite.cs
Normal file
20
Game/Graphics/TwoDimensional/Abstract/IDisplayableSprite.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Graphics.TwoDimensional.Abstract;
|
||||
public interface IDisplayableSprite : IDisplayable, IAssignableSprite
|
||||
{
|
||||
Action<IDisplayableSprite>? OnSpriteEffectsChanged { get; set; }
|
||||
Action<IDisplayableSprite>? OnOriginChanged { get; set; }
|
||||
Action<IDisplayableSprite>? OnColorChanged { get; set; }
|
||||
Action<IDisplayableSprite>? OnDepthChanged { get; set; }
|
||||
|
||||
SpriteEffects SpriteEffects { get; set; }
|
||||
Vector2 Origin { get; set; }
|
||||
Color Color { get; set; }
|
||||
float Depth { get; set; }
|
||||
}
|
||||
117
Game/Graphics/TwoDimensional/DisplayableSpriteBehaviour.cs
Normal file
117
Game/Graphics/TwoDimensional/DisplayableSpriteBehaviour.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
using Syntriax.Engine.Graphics.TwoDimensional.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Graphics.TwoDimensional;
|
||||
|
||||
public class DisplayableSpriteBehaviour : Behaviour, IDisplayableSprite, IAssignableSprite
|
||||
{
|
||||
public Action<IAssignableSprite>? OnSpriteAssigned { get; set; } = null;
|
||||
public Action<IDisplayableSprite>? OnSpriteEffectsChanged { get; set; } = null;
|
||||
public Action<IDisplayableSprite>? OnOriginChanged { get; set; } = null;
|
||||
public Action<IDisplayableSprite>? OnColorChanged { get; set; } = null;
|
||||
public Action<IDisplayableSprite>? OnDepthChanged { get; set; } = null;
|
||||
|
||||
private ISprite _sprite = null!;
|
||||
private Color _color = Color.White;
|
||||
private float _depth = 0f;
|
||||
private SpriteEffects _spriteEffects = SpriteEffects.None;
|
||||
private Vector2 _origin = Vector2.One * .5f;
|
||||
|
||||
|
||||
public ISprite Sprite => _sprite;
|
||||
|
||||
public SpriteEffects SpriteEffects
|
||||
{
|
||||
get => _spriteEffects;
|
||||
set
|
||||
{
|
||||
if (_spriteEffects == value)
|
||||
return;
|
||||
|
||||
_spriteEffects = value;
|
||||
OnSpriteEffectsChanged?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 Origin
|
||||
{
|
||||
get => _origin;
|
||||
set
|
||||
{
|
||||
if (_origin == value)
|
||||
return;
|
||||
|
||||
_origin = value;
|
||||
OnOriginChanged?.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);
|
||||
}
|
||||
}
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!BehaviourController.GameObject.StateEnable.Enabled || !StateEnable.Enabled)
|
||||
return;
|
||||
|
||||
ITransform transform = BehaviourController.GameObject.Transform;
|
||||
Vector2D position = transform.Position;
|
||||
Vector2D scale = transform.Scale;
|
||||
|
||||
Rectangle rectangle = new((int)position.X, -(int)position.Y, (int)(Sprite.Texture2D.Width * scale.X), (int)(Sprite.Texture2D.Height * scale.Y));
|
||||
|
||||
spriteBatch.Draw(Sprite.Texture2D, rectangle, null, Color, transform.Rotation, new Vector2(Sprite.Texture2D.Width, Sprite.Texture2D.Height) * Origin, SpriteEffects, Depth);
|
||||
}
|
||||
|
||||
public bool Assign(ISprite sprite)
|
||||
{
|
||||
_sprite = sprite;
|
||||
OnSpriteAssigned?.Invoke(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
public DisplayableSpriteBehaviour() => OnUnassigned += OnUnassign;
|
||||
public DisplayableSpriteBehaviour(
|
||||
Color? color = null,
|
||||
float? depth = null,
|
||||
SpriteEffects? spriteEffects = null,
|
||||
Vector2? origin = null)
|
||||
{
|
||||
OnUnassigned += OnUnassign;
|
||||
|
||||
_color = color ?? _color;
|
||||
_depth = depth ?? _depth;
|
||||
_spriteEffects = spriteEffects ?? _spriteEffects;
|
||||
_origin = origin ?? _origin;
|
||||
}
|
||||
private void OnUnassign(IAssignable assignable) => _sprite = null!;
|
||||
}
|
||||
Reference in New Issue
Block a user