Animation finally working! Probably need better sprites.

This commit is contained in:
Tim Young 2018-02-15 16:57:46 +00:00
parent de5ec6cfd3
commit 4da26055a6
3 changed files with 60 additions and 18 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Drawing; using System.Drawing;
using System.Windows.Forms;
namespace EduNetworkBuilder namespace EduNetworkBuilder
{ {
@ -15,9 +16,9 @@ namespace EduNetworkBuilder
Size HowBig; //how big is each animation. Size HowBig; //how big is each animation.
public DateTime NextAnimation; public DateTime NextAnimation;
int AnimationCount = 0; int AnimationCount = 0;
int AnimationSpeed = 300; //ms between animations int AnimationSpeed = 150; //ms between animations
Image BackgroundSnip;
int maxAnim = 6; int maxAnim = 6;
bool erased = true;
public bool AnimationDone { get { if (AnimationCount > 0) return false; return true; } } public bool AnimationDone { get { if (AnimationCount > 0) return false; return true; } }
public bool FinalErase = false; public bool FinalErase = false;
@ -43,27 +44,31 @@ namespace EduNetworkBuilder
AnimationCount = 6; AnimationCount = 6;
break; break;
} }
NextAnimation = DateTime.UtcNow.AddMilliseconds(AnimationSpeed);
} }
public void GrabBackground(Image TheNetworkImage) public void DrawAnimation(Image TheNetworkImage, PictureBox myPB)
{ {
BackgroundSnip = new Bitmap(Where.Width, Where.Height); if (AnimationCount < 0) return;
Graphics.FromImage(BackgroundSnip).DrawImage(TheNetworkImage, new Rectangle(0, 0, Where.Width, Where.Height), Where, GraphicsUnit.Pixel); if (!erased) return;
}
public void DrawAnimation(Image TheNetworkImage)
{
Bitmap AnimationFrame = new Bitmap(Where.Width, Where.Height);
Rectangle AniminSnip = new Rectangle((maxAnim - AnimationCount) * HowBig.Width, ImageStartPoint.Y, HowBig.Width, HowBig.Height); Rectangle AniminSnip = new Rectangle((maxAnim - AnimationCount) * HowBig.Width, ImageStartPoint.Y, HowBig.Width, HowBig.Height);
Graphics.FromImage(TheNetworkImage).DrawImage(Properties.Resources.Animations, Where, AniminSnip, GraphicsUnit.Pixel);
Graphics.FromImage(AnimationFrame).DrawImage(Properties.Resources.Animations, new Rectangle(0, 0, Where.Width, Where.Height), AniminSnip, GraphicsUnit.Pixel); myPB.Invalidate(Where);
Graphics.FromImage(TheNetworkImage).DrawImage(AnimationFrame, new Rectangle(0, 0, Where.Width, Where.Height), Where, GraphicsUnit.Pixel);
} }
public void EraseAnimation(Image TheNetworkImage) public void EraseAnimation(Image TheNetworkImage, PictureBox myPB, Image TheNetworkImageBackground)
{ {
Graphics.FromImage(TheNetworkImage).DrawImage(BackgroundSnip, Where, new Rectangle(0,0,Where.Width, Where.Height), GraphicsUnit.Pixel); if (DateTime.UtcNow >= NextAnimation)
{
//Console.WriteLine("\nThis happened at: " + DateTime.UtcNow.ToString("mm:ss.fff tt"));
if (TheNetworkImageBackground == null) return;//do nothing if no animation set
Graphics.FromImage(TheNetworkImage).DrawImage(TheNetworkImageBackground, Where, Where, GraphicsUnit.Pixel);
myPB.Invalidate(Where);
erased = true;
NextAnimation = DateTime.UtcNow.AddMilliseconds(AnimationSpeed);
//Console.WriteLine("next: "+ NextAnimation.ToString("mm:ss.fff tt"));
AnimationCount--;
}
} }
} }
} }

View File

@ -70,6 +70,8 @@ namespace EduNetworkBuilder
private List<string> BrokenItems = new List<string>(); private List<string> BrokenItems = new List<string>();
public bool IsRandomNetwork = false; public bool IsRandomNetwork = false;
private List<AnimationClass> Animations = new List<AnimationClass>();
/// <summary> /// <summary>
/// WhatFrom: If we are launched from a homework, we remember it here. /// WhatFrom: If we are launched from a homework, we remember it here.
/// </summary> /// </summary>
@ -1359,7 +1361,15 @@ namespace EduNetworkBuilder
public void Tick(bool SkipVisuals = false) public void Tick(bool SkipVisuals = false)
{ {
if (!SkipVisuals) if (!SkipVisuals)
{
foreach (AnimationClass one in Animations)
one.EraseAnimation(TheNetImageBackground, myPBox, TheNetImage);
EraseOldPackets(); EraseOldPackets();
for(int i= Animations.Count-1; i>=0; i--)
{
if (Animations[i].AnimationDone) Animations.RemoveAt(i);
}
}
//if (myPackets.Count > 50) //if (myPackets.Count > 50)
//Console.WriteLine("Packets: " + myPackets.Count.ToString()); //Console.WriteLine("Packets: " + myPackets.Count.ToString());
if (myPackets.Count > 0) if (myPackets.Count > 0)
@ -1376,7 +1386,9 @@ namespace EduNetworkBuilder
//It has all been taken care of //It has all been taken care of
} }
if (!SkipVisuals) if (!SkipVisuals)
{
DrawPackets(); DrawPackets();
}
//myPBox.Refresh(); //myPBox.Refresh();
previously_had_packets = true; previously_had_packets = true;
} }
@ -1416,6 +1428,19 @@ namespace EduNetworkBuilder
} }
previously_had_packets = false; previously_had_packets = false;
} }
if (!SkipVisuals)
{
foreach (AnimationClass one in Animations)
{
one.DrawAnimation(TheNetImageBackground, myPBox);
}
myPBox.Refresh();
}
}
public void AddAnimation(AnimationName What, Rectangle Where)
{
Animations.Add(new AnimationClass(What, Where));
} }
public void AddMessage(PacketMessage toAdd) public void AddMessage(PacketMessage toAdd)

View File

@ -941,6 +941,18 @@ namespace EduNetworkBuilder
{ {
if(!myNetwork.ItemTestIsComplete(ItemClickedOn.hostname, NetTestType.DeviceBlowsUpWithPower)) if(!myNetwork.ItemTestIsComplete(ItemClickedOn.hostname, NetTestType.DeviceBlowsUpWithPower))
{ {
Rectangle Where = new Rectangle(ItemClickedOn.myLocation().X,
ItemClickedOn.myLocation().Y - ((ItemClickedOn.Size * 2) / 3), ItemClickedOn.Size, ItemClickedOn.Size);
if(ItemClickedOn.IsBurned)
{
int which = GameRandomGen.Next(2);
if(which ==0)
myNetwork.AddAnimation(AnimationName.Smoke1, Where);
else
myNetwork.AddAnimation(AnimationName.Spark1, Where);
}
else
myNetwork.AddAnimation(AnimationName.Fire1, Where);
ItemClickedOn.IsBurned = true; ItemClickedOn.IsBurned = true;
} }
} }