decoupling sprite from transform

This commit is contained in:
Dylan Wilson
2017-11-21 20:57:32 +10:00
parent 46f3f99b39
commit 90a1802cb3
3 changed files with 22 additions and 28 deletions
@@ -21,7 +21,7 @@ namespace MonoGame.Extended.Animations
public SpriteSheetAnimation Play(string name, Action onCompleted = null)
{
if ((_currentAnimation == null) || _currentAnimation.IsComplete || (_currentAnimation.Name != name))
if (_currentAnimation == null || _currentAnimation.IsComplete || _currentAnimation.Name != name)
{
_currentAnimation = _animationFactory.Create(name);
_currentAnimation.OnCompleted = onCompleted;
@@ -32,7 +32,7 @@ namespace MonoGame.Extended.Animations
public void Update(float deltaTime)
{
if ((_currentAnimation != null) && !_currentAnimation.IsComplete)
if (_currentAnimation != null && !_currentAnimation.IsComplete)
{
_currentAnimation.Update(deltaTime);
TextureRegion = _currentAnimation.CurrentFrame;
+14 -18
View File
@@ -6,7 +6,7 @@ using MonoGame.Extended.TextureAtlases;
namespace MonoGame.Extended.Sprites
{
public class Sprite : Transform2D, IColorable, IRectangularF, ISpriteBatchDrawable
public class Sprite : IColorable//, IRectangularF//, ISpriteBatchDrawable
{
private TextureRegion2D _textureRegion;
@@ -19,7 +19,6 @@ namespace MonoGame.Extended.Sprites
Alpha = 1.0f;
Color = Color.White;
IsVisible = true;
Scale = Vector2.One;
Effect = SpriteEffects.None;
OriginNormalized = new Vector2(0.5f, 0.5f);
Depth = 0.0f;
@@ -42,15 +41,12 @@ namespace MonoGame.Extended.Sprites
public Color Color { get; set; }
public RectangleF BoundingRectangle
public RectangleF GetBoundingRectangle(Vector2 position, float rotation, Vector2 scale)
{
get
{
var corners = GetCorners();
var min = new Vector2(corners.Min(i => i.X), corners.Min(i => i.Y));
var max = new Vector2(corners.Max(i => i.X), corners.Max(i => i.Y));
return new RectangleF(min.X, min.Y, max.X - min.X, max.Y - min.Y);
}
var corners = GetCorners(position, rotation, scale);
var min = new Vector2(corners.Min(i => i.X), corners.Min(i => i.Y));
var max = new Vector2(corners.Max(i => i.X), corners.Max(i => i.Y));
return new RectangleF(min.X, min.Y, max.X - min.X, max.Y - min.Y);
}
public bool IsVisible { get; set; }
@@ -72,16 +68,16 @@ namespace MonoGame.Extended.Sprites
}
}
public Vector2[] GetCorners()
public Vector2[] GetCorners(Vector2 position, float rotation, Vector2 scale)
{
var min = -Origin;
var max = min + new Vector2(TextureRegion.Width, TextureRegion.Height);
var offset = Position;
var offset = position;
if (Scale != Vector2.One)
if (scale != Vector2.One)
{
min = min*Scale;
max = max*Scale;
min = min * scale;
max = max * scale;
}
var corners = new Vector2[4];
@@ -91,9 +87,9 @@ namespace MonoGame.Extended.Sprites
corners[3] = new Vector2(min.X, max.Y);
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (Rotation != 0)
if (rotation != 0)
{
var matrix = Matrix.CreateRotationZ(Rotation);
var matrix = Matrix.CreateRotationZ(rotation);
for (var i = 0; i < 4; i++)
corners[i] = Vector2.Transform(corners[i], matrix);
@@ -105,4 +101,4 @@ namespace MonoGame.Extended.Sprites
return corners;
}
}
}
}
@@ -1,16 +1,17 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MonoGame.Extended.Sprites
{
public static class SpriteExtensions
{
public static void Draw(this Sprite sprite, SpriteBatch spriteBatch)
public static void Draw(this Sprite sprite, SpriteBatch spriteBatch, Vector2 position, float rotation, Vector2 scale)
{
Draw(spriteBatch, sprite);
Draw(spriteBatch, sprite, position, rotation, scale);
}
public static void Draw(this SpriteBatch spriteBatch, Sprite sprite)
public static void Draw(this SpriteBatch spriteBatch, Sprite sprite, Vector2 position, float rotation, Vector2 scale)
{
if (sprite == null) throw new ArgumentNullException(nameof(sprite));
@@ -18,11 +19,8 @@ namespace MonoGame.Extended.Sprites
{
var texture = sprite.TextureRegion.Texture;
var sourceRectangle = sprite.TextureRegion.Bounds;
spriteBatch.Draw(texture, sprite.WorldPosition, sourceRectangle, sprite.Color*sprite.Alpha, sprite.WorldRotation,
sprite.Origin,
sprite.WorldScale, sprite.Effect, sprite.Depth);
spriteBatch.Draw(texture, position, sourceRectangle, sprite.Color*sprite.Alpha, rotation, sprite.Origin, scale, sprite.Effect, sprite.Depth);
}
}
}
}
}