initial sprite sharing functions

This commit is contained in:
Tim Young 2017-09-10 16:57:31 -05:00
parent 69d9bddc1e
commit 1dc3b5e9f1

View File

@ -116,6 +116,7 @@ namespace SpriteLibrary
Image MyOriginalImage; //The untainted background Image MyOriginalImage; //The untainted background
PictureBox DrawingArea; //The PictureBox we draw ourselves on PictureBox DrawingArea; //The PictureBox we draw ourselves on
List<Sprite> Sprites = new List<Sprite>(); List<Sprite> Sprites = new List<Sprite>();
List<SpriteController> LinkedControllers = new List<SpriteController>(); //Other sprite controllers that we share sprites with
/// <summary> /// <summary>
/// Since everything needs a random number generator, we make one that should be accessible throughout your program. /// Since everything needs a random number generator, we make one that should be accessible throughout your program.
@ -638,6 +639,21 @@ namespace SpriteLibrary
return newList; return newList;
} }
/// <summary>
/// Get a list of all your named sprites. These should just be your template sprites.
/// </summary>
/// <returns>A list containing all the named sprites</returns>
public List<Sprite> AllNamedSprites()
{
List<Sprite> tList = new List<Sprite>();
foreach(Sprite one in Sprites)
{
if (one.SpriteName != "")
tList.Add(one);
}
return tList;
}
/// <summary> /// <summary>
/// Return an adjustment ratio. This is the image-size to picture-box ratio. /// Return an adjustment ratio. This is the image-size to picture-box ratio.
/// It is used for calculating precise pixels or picture-box locations. /// It is used for calculating precise pixels or picture-box locations.
@ -959,9 +975,35 @@ namespace SpriteLibrary
public void AddSprite(Sprite SpriteToAdd) public void AddSprite(Sprite SpriteToAdd)
{ {
Sprites.Add(SpriteToAdd); Sprites.Add(SpriteToAdd);
AddSpriteToLinkedControllers(SpriteToAdd);
SortSprites(); SortSprites();
} }
/// <summary>
/// This internal function is for adding named sprites from other controllers to keep them in sync
/// </summary>
/// <param name="SpriteToAdd">The sprite to add if it does not exist yet on this controller</param>
internal void AddSpriteIfNotExists(Sprite SpriteToAdd)
{
if (SpriteToAdd.SpriteName == "") return; //We only add named sprites
Sprite found = SpriteFromName(SpriteToAdd.SpriteName);
if(found == null)
Sprites.Add(SpriteToAdd);
}
/// <summary>
/// If we are linked to other controllers, add this sprite template to the other controllers also
/// </summary>
/// <param name="SpriteToAdd">The sprite we are trying to add</param>
internal void AddSpriteToLinkedControllers(Sprite SpriteToAdd)
{
if (SpriteToAdd.SpriteName == "") return; //We only add named sprites
foreach (SpriteController one in LinkedControllers)
{
one.AddSpriteIfNotExists(SpriteToAdd);
}
}
/// <summary> /// <summary>
/// Tell a sprite to destroy itself. The sprite will have Destroying property set to true from /// Tell a sprite to destroy itself. The sprite will have Destroying property set to true from
/// the time you destroy it until it vanishes. Whe you destroy a sprite, it will erase itself /// the time you destroy it until it vanishes. Whe you destroy a sprite, it will erase itself
@ -1000,6 +1042,64 @@ namespace SpriteLibrary
What.SetName(Name); What.SetName(Name);
} }
/// <summary>
/// Link up a sprite controller so that it shares sprites with this other sprite controller. If one sprite controller
/// does not have the named sprite, it will query any linked controllers for that named sprite and copy it to the
/// controller that did not have it. This means you only need to create a sprite once, and you can use it on multiple
/// sprite controllers. In many games, you will want to have a sprite appear on different PictureBoxes, and this is
/// a way to do that. For example, you may want to have a bad-guy running around on the screen, but also have his sprite
/// appear in a bad-guy summary, along with his stats, on the side. Loading sprites can be slow, so this makes things a bit
/// faster by only needing to load them once.
/// </summary>
/// <param name="ControllerToLinkToThis">The sprite-controller to link. You only need to link it one direction,
/// the sprite controller will automatically create a bi-directional link</param>
public void LinkControllersForSpriteTemplateSharing(SpriteController ControllerToLinkToThis)
{
if (ControllerToLinkToThis == null) return;
if(!LinkedControllers.Contains(ControllerToLinkToThis))
{
LinkedControllers.Add(ControllerToLinkToThis);
}
ControllerToLinkToThis.LinkControllersForSpriteTemplateSharing(this); //link the other direction also
}
/// <summary>
/// Unlink a previously linked controller. If you have linked a controller from a different window and are trying to
/// kill off the controller in a window you are closing, you want to unlink them as the window closes. We take a brief
/// moment to copy over any templates that have not yet been copied over.
/// </summary>
/// <param name="ControllerToUnlink">The </param>
public void UnlinkControllersForSpriteTemplateSharing(SpriteController ControllerToUnlink)
{
if (ControllerToUnlink == null) return; //nothing to do.
if (LinkedControllers.Contains(ControllerToUnlink))
{
LinkedControllers.Remove(ControllerToUnlink);
}
ControllerToUnlink.UnlinkControllersForSpriteTemplateSharingInternal(this);
List<Sprite> MySpriteTemplates = AllNamedSprites();
List<Sprite> TheirSpriteTemplates = ControllerToUnlink.AllNamedSprites();
foreach (Sprite one in MySpriteTemplates)
ControllerToUnlink.AddSpriteIfNotExists(one);
foreach (Sprite one in TheirSpriteTemplates)
AddSpriteIfNotExists(one);
}
/// <summary>
/// This unlinks the second half. This is an internal function so people using SpriteController cannot accidentally
/// unlink half a controller.
/// </summary>
/// <param name="ControllerToUnlink"></param>
internal void UnlinkControllersForSpriteTemplateSharingInternal(SpriteController ControllerToUnlink)
{
if (ControllerToUnlink == null) return; //nothing to do.
if (LinkedControllers.Contains(ControllerToUnlink))
{
LinkedControllers.Remove(ControllerToUnlink);
}
}
/// <summary> /// <summary>
/// This takes a point, as given by the mouse-click args, and returns the sprites at that point. Different /// This takes a point, as given by the mouse-click args, and returns the sprites at that point. Different
/// functions use different coordinates, whether based off the background image, or based off the picturebox. /// functions use different coordinates, whether based off the background image, or based off the picturebox.