2016-05-17 20:08:07 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
2016-05-21 13:13:05 +00:00
|
|
|
|
using System.Runtime.Remoting.Messaging;
|
2016-05-17 20:08:07 +00:00
|
|
|
|
|
|
|
|
|
namespace SpritePacker
|
|
|
|
|
{
|
|
|
|
|
public class Sprite
|
|
|
|
|
{
|
|
|
|
|
private Rectangle area;
|
|
|
|
|
private string filename;
|
2016-05-21 13:13:05 +00:00
|
|
|
|
private Image image;
|
2016-05-17 20:08:07 +00:00
|
|
|
|
|
2018-08-03 19:43:05 +00:00
|
|
|
|
public bool Placed { get; set; } = false;
|
|
|
|
|
|
2016-05-17 20:08:07 +00:00
|
|
|
|
public Rectangle Area
|
|
|
|
|
{
|
|
|
|
|
get { return area; }
|
|
|
|
|
set { area = value; }
|
|
|
|
|
}
|
|
|
|
|
public string Filename
|
|
|
|
|
{
|
|
|
|
|
get { return filename; }
|
|
|
|
|
set { filename = value; }
|
|
|
|
|
}
|
2016-05-21 13:13:05 +00:00
|
|
|
|
public Image Image { get { return image; } }
|
2016-05-17 20:08:07 +00:00
|
|
|
|
|
2016-05-21 13:13:05 +00:00
|
|
|
|
public Point Location { get { return new Point(X, Y); } }
|
2016-05-18 20:02:15 +00:00
|
|
|
|
public int X
|
|
|
|
|
{
|
|
|
|
|
get { return area.X; }
|
|
|
|
|
set { area.X = value; }
|
|
|
|
|
}
|
|
|
|
|
public int Y
|
|
|
|
|
{
|
|
|
|
|
get { return area.Y; }
|
|
|
|
|
set { area.Y = value; }
|
|
|
|
|
}
|
|
|
|
|
public int Width
|
|
|
|
|
{
|
|
|
|
|
get { return area.Width; }
|
|
|
|
|
set { area.Width = value; }
|
|
|
|
|
}
|
|
|
|
|
public int Height
|
|
|
|
|
{
|
|
|
|
|
get { return area.Height; }
|
|
|
|
|
set { area.Height = value; }
|
|
|
|
|
}
|
|
|
|
|
public int Top { get { return area.Top; } }
|
|
|
|
|
public int Bottom { get { return area.Bottom; } }
|
|
|
|
|
public int Left { get { return area.Left; } }
|
|
|
|
|
public int Right { get { return area.Right; } }
|
|
|
|
|
|
2016-05-21 13:13:05 +00:00
|
|
|
|
public int AreaSize { get { return area.Width * area.Height; } }
|
2016-05-21 14:38:22 +00:00
|
|
|
|
public int MaxSideLength { get { return Math.Max(area.Width, area.Height); } }
|
2016-05-21 13:13:05 +00:00
|
|
|
|
|
2016-05-17 20:08:07 +00:00
|
|
|
|
public Sprite(string inFilename)
|
|
|
|
|
{
|
|
|
|
|
Filename = inFilename;
|
2016-05-21 13:13:05 +00:00
|
|
|
|
|
|
|
|
|
image = Image.FromFile(filename);
|
|
|
|
|
Width = image.Width;
|
|
|
|
|
Height = image.Height;
|
2016-05-17 20:08:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int GetLargestSize(List<Sprite> sprList)
|
|
|
|
|
{
|
|
|
|
|
int largestSoFar = 0;
|
|
|
|
|
foreach(Sprite spr in sprList)
|
|
|
|
|
{
|
|
|
|
|
if (spr.Area.Width > largestSoFar)
|
|
|
|
|
largestSoFar = spr.Area.Width;
|
|
|
|
|
}
|
2016-05-18 20:02:15 +00:00
|
|
|
|
return largestSoFar;
|
2016-05-17 20:08:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Sprite> GetIntersectors(List<Sprite> spriteList)
|
|
|
|
|
{
|
|
|
|
|
List<Sprite> result = new List<Sprite>();
|
|
|
|
|
foreach(Sprite spr in spriteList)
|
|
|
|
|
{
|
2016-05-21 13:13:05 +00:00
|
|
|
|
if (spr == this) // Don't attempt to check for collisions with ourselves
|
|
|
|
|
continue;
|
2016-05-17 20:08:07 +00:00
|
|
|
|
if (spr.IntersectsWith(this))
|
|
|
|
|
result.Add(spr);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
public List<Sprite> GetIntersectorsX(List<Sprite> spriteList)
|
|
|
|
|
{
|
|
|
|
|
List<Sprite> result = new List<Sprite>();
|
|
|
|
|
foreach(Sprite spr in spriteList)
|
|
|
|
|
{
|
|
|
|
|
if (spr.IntersectsWithX(this))
|
|
|
|
|
result.Add(spr);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
public List<Sprite> GetIntersectorsY(List<Sprite> spriteList)
|
|
|
|
|
{
|
|
|
|
|
List<Sprite> result = new List<Sprite>();
|
|
|
|
|
foreach(Sprite spr in spriteList)
|
|
|
|
|
{
|
|
|
|
|
if (spr.IntersectsWithY(this))
|
|
|
|
|
result.Add(spr);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IntersectsWith(List<Sprite> otherSprites)
|
|
|
|
|
{
|
|
|
|
|
foreach (Sprite spr in otherSprites)
|
|
|
|
|
{
|
|
|
|
|
if (IntersectsWith(spr))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public bool IntersectsWith(Sprite otherSprite)
|
|
|
|
|
{
|
|
|
|
|
return otherSprite.Area.IntersectsWith(Area);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IntersectsWithX(List<Sprite> otherSprites)
|
|
|
|
|
{
|
|
|
|
|
foreach (Sprite spr in otherSprites)
|
|
|
|
|
{
|
|
|
|
|
if (IntersectsWithX(spr))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public bool IntersectsWithY(List<Sprite> otherSprites)
|
|
|
|
|
{
|
|
|
|
|
foreach (Sprite spr in otherSprites)
|
|
|
|
|
{
|
|
|
|
|
if (IntersectsWithY(spr))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IntersectsWithX(Sprite otherSprite)
|
|
|
|
|
{
|
|
|
|
|
if(Area.Right > otherSprite.Area.X &&
|
|
|
|
|
Area.X < otherSprite.Area.Right)
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public bool IntersectsWithY(Sprite otherSprite)
|
|
|
|
|
{
|
|
|
|
|
if(Area.Bottom > otherSprite.Area.Y &&
|
|
|
|
|
Area.Y < otherSprite.Area.Bottom)
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return string.Format("Sprite {0} at {1}", Filename, Area);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|