Added data members to Sprite, re-coded SB Extension

This commit is contained in:
Benjamin Ward
2015-08-02 16:09:22 -04:00
parent 7afb5e4663
commit 783b1fce6a
2 changed files with 58 additions and 11 deletions
@@ -12,9 +12,10 @@ namespace MonoGame.Extended.Graphics
spriteBatch.Draw(textureRegion.Texture, position, sourceRectangle, color);
}
public static void Draw(this SpriteBatch spriteBatch, Sprite sprite, Color color)
public static void Draw(this SpriteBatch spriteBatch, Sprite sprite)
{
spriteBatch.Draw(sprite.Texture, sprite.Position, color);
if (sprite.IsVisible)
spriteBatch.Draw(sprite.TextureRegion.Texture, sprite.Position, sprite.GetBoundingRectangle(), sprite.TextureRegion.Bounds, sprite.Origin, sprite.Rotation, sprite.Scale, sprite.Color);
}
public static void Draw(this SpriteBatch spriteBatch, TextureRegion2D textureRegion, Rectangle destinationRectangle, Color color)
+55 -9
View File
@@ -1,5 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -9,24 +10,69 @@ namespace MonoGame.Extended.Sprites
{
public class Sprite
{
public Texture2D Texture;
public Vector2 Position = Vector2.Zero;
public TextureRegion2D TextureRegion;
public Color Color { get; set; }
public bool IsVisible { get; set; }
public Vector2 Scale = Vector2.One;
public Rectangle Bounds
{
get { return new Rectangle((int)Position.X, (int)Position.Y, Texture.Bounds.Width, Texture.Bounds.Height); }
}
public Vector2 Position { get; set; }
public Vector2 Scale { get; set; }
public float Rotation { get; set; }
public Vector2 Origin { get; set; }
public Sprite(Texture2D texture)
{
Texture = texture;
TextureRegion = new TextureRegion2D(texture);
Scale = Vector2.One;
}
public Sprite(Texture2D texture, Vector2 position) : this(texture)
{
Position = position;
}
public Sprite(Texture2D texture, Vector2 position, Color color) : this(texture, position)
{
Color = color;
}
public Sprite(Texture2D texture, Vector2 position, Color? color, float? rotation, Vector2? scale) : this(texture, position)
{
// If any parameters are null, provide them with suitable defaults.
Color = color ?? Color.White;
Rotation = rotation ?? 0;
Scale = scale ?? Vector2.One;
}
public Sprite(TextureRegion2D texture)
{
TextureRegion = texture;
Scale = Vector2.One;
}
public Sprite(TextureRegion2D texture, Vector2 position)
: this(texture)
{
Position = position;
}
public Sprite(TextureRegion2D texture, Vector2 position, Color color)
: this(texture, position)
{
Color = color;
}
public Sprite(TextureRegion2D texture, Vector2 position, Color? color, float? rotation, Vector2? scale)
: this(texture, position)
{
// If any parameters are null, provide them with suitable defaults.
Color = color ?? Color.White;
Rotation = rotation ?? 0;
Scale = scale ?? Vector2.One;
}
public Rectangle GetBoundingRectangle()
{
return new Rectangle((int)Position.X, (int)Position.Y, TextureRegion.Bounds.Width, TextureRegion.Bounds.Height);
}
}
}