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.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
namespace EduNetworkBuilder
{
@ -15,9 +16,9 @@ namespace EduNetworkBuilder
Size HowBig; //how big is each animation.
public DateTime NextAnimation;
int AnimationCount = 0;
int AnimationSpeed = 300; //ms between animations
Image BackgroundSnip;
int AnimationSpeed = 150; //ms between animations
int maxAnim = 6;
bool erased = true;
public bool AnimationDone { get { if (AnimationCount > 0) return false; return true; } }
public bool FinalErase = false;
@ -43,27 +44,31 @@ namespace EduNetworkBuilder
AnimationCount = 6;
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);
Graphics.FromImage(BackgroundSnip).DrawImage(TheNetworkImage, new Rectangle(0, 0, Where.Width, Where.Height), Where, GraphicsUnit.Pixel);
}
public void DrawAnimation(Image TheNetworkImage)
{
Bitmap AnimationFrame = new Bitmap(Where.Width, Where.Height);
if (AnimationCount < 0) return;
if (!erased) return;
Rectangle AniminSnip = new Rectangle((maxAnim - AnimationCount) * HowBig.Width, ImageStartPoint.Y, HowBig.Width, HowBig.Height);
Graphics.FromImage(AnimationFrame).DrawImage(Properties.Resources.Animations, new Rectangle(0, 0, Where.Width, Where.Height), AniminSnip, GraphicsUnit.Pixel);
Graphics.FromImage(TheNetworkImage).DrawImage(AnimationFrame, new Rectangle(0, 0, Where.Width, Where.Height), Where, GraphicsUnit.Pixel);
Graphics.FromImage(TheNetworkImage).DrawImage(Properties.Resources.Animations, Where, AniminSnip, GraphicsUnit.Pixel);
myPB.Invalidate(Where);
}
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>();
public bool IsRandomNetwork = false;
private List<AnimationClass> Animations = new List<AnimationClass>();
/// <summary>
/// WhatFrom: If we are launched from a homework, we remember it here.
/// </summary>
@ -1358,8 +1360,16 @@ namespace EduNetworkBuilder
public void Tick(bool SkipVisuals = false)
{
if(!SkipVisuals)
if (!SkipVisuals)
{
foreach (AnimationClass one in Animations)
one.EraseAnimation(TheNetImageBackground, myPBox, TheNetImage);
EraseOldPackets();
for(int i= Animations.Count-1; i>=0; i--)
{
if (Animations[i].AnimationDone) Animations.RemoveAt(i);
}
}
//if (myPackets.Count > 50)
//Console.WriteLine("Packets: " + myPackets.Count.ToString());
if (myPackets.Count > 0)
@ -1375,8 +1385,10 @@ namespace EduNetworkBuilder
{
//It has all been taken care of
}
if(!SkipVisuals)
if (!SkipVisuals)
{
DrawPackets();
}
//myPBox.Refresh();
previously_had_packets = true;
}
@ -1416,6 +1428,19 @@ namespace EduNetworkBuilder
}
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)

View File

@ -941,6 +941,18 @@ namespace EduNetworkBuilder
{
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;
}
}