Added texture cache and sprite origins.
This commit is contained in:
@@ -8,6 +8,7 @@ public class Sprite(float x, float y)
|
||||
public float XPosition { get; protected set; } = x;
|
||||
public float YPosition { get; protected set; } = y;
|
||||
public Vector2 Position => new(XPosition, YPosition);
|
||||
public Vector2 Origin { get; protected set; }
|
||||
public Rectangle BoundBox { get; protected set; }
|
||||
|
||||
protected Rectangle CollisionBox;
|
||||
@@ -18,7 +19,8 @@ public class Sprite(float x, float y)
|
||||
|
||||
public virtual void Update(SpriteUpdateContext context)
|
||||
{
|
||||
CollisionBox = new Rectangle((int)XPosition + BoundBox.X, (int)YPosition + BoundBox.Y, BoundBox.Width, BoundBox.Height);
|
||||
//CollisionBox = new Rectangle((int)XPosition + BoundBox.X, (int)YPosition + BoundBox.Y, BoundBox.Width, BoundBox.Height);
|
||||
CollisionBox = new Rectangle((int)(Position.X - Origin.X) + BoundBox.X, (int)(Position.Y - Origin.Y) + BoundBox.Y, BoundBox.Width, BoundBox.Height);
|
||||
}
|
||||
|
||||
public bool Intersects(Sprite sprite)
|
||||
@@ -29,6 +31,28 @@ public class Sprite(float x, float y)
|
||||
public virtual void Draw(SpriteDrawArgs args)
|
||||
{
|
||||
//spriteBatch.Draw(Texture, Position, DrawColor);
|
||||
DrawCollisionBox(args);
|
||||
}
|
||||
|
||||
private void DrawCollisionBox(SpriteDrawArgs args)
|
||||
{
|
||||
//var pixel = DebugPixel; // static cached
|
||||
|
||||
Texture2D pixel = new Texture2D(args.SpriteBatch.GraphicsDevice, 1, 1);
|
||||
pixel.SetData(new[] { Color.White });
|
||||
|
||||
//Rectangle r = GetWorldCollisionBox();
|
||||
|
||||
Color c = Color.LimeGreen; // debug color
|
||||
|
||||
// Top
|
||||
args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.X, CollisionBox.Y, CollisionBox.Width, 1), c);
|
||||
// Bottom
|
||||
args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.X, CollisionBox.Bottom - 1, CollisionBox.Width, 1), c);
|
||||
// Left
|
||||
args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.X, CollisionBox.Y, 1, CollisionBox.Height), c);
|
||||
// Right
|
||||
args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.Right - 1, CollisionBox.Y, 1, CollisionBox.Height), c);
|
||||
}
|
||||
|
||||
public virtual void OnCollision(SpriteCollisionContext context)
|
||||
|
||||
Reference in New Issue
Block a user