private async void timerTick() { if (!imageLoaded) { return; } Source.Cancel(); int alive = currentExistence.Phases.First().GetNumberAlive(); LastReport = currentExistence.MakeAnUniversalTimeUntiPass(); int dead = statingPopulationNumber - alive; lblAlvie.Text = alive.ToString(); lblDead.Text = dead.ToString(); universalTimeUnit++; lblUniversalTime.Text = universalTimeUnit.ToString(); lblUTUSecond.Text = String.Format("{0:0.0}", (universalTimeUnit / (DateTime.Now - StartingTime).TotalSeconds)); if (panel1.BackgroundImage == null) { panel1.BackgroundImage = new Bitmap(500, 500); } if (currentExistence != null || currentExistence.Phases != null || currentExistence.Phases.FirstOrDefault() != null) { Source = new CancellationTokenSource(); panel1.BackgroundImage = await drawer.Draw(Source.Token, new List <UniversalObject>(currentExistence.Phases.First().UniversalObjectsInside), cbShowAttacks.Checked, LastReport, (Bitmap)panel1.BackgroundImage); } //panel1.Refresh(); imageLoaded = true; }
public UniversalTimeUnitPassReport MankeAnUniversalTimeUntiPass() { UniversalTimeUnitPassReport universalTimeUnitPassReport = new UniversalTimeUnitPassReport(); if (ObjectToRemoveFromExistanceOnNextOccasion.Count != 0) { foreach (UniversalObject universalObject in ObjectToRemoveFromExistanceOnNextOccasion) { UniversalObjectsInside.Remove(universalObject); } } foreach (UniversalObject universalObject in UniversalObjectsInside) { Being being = universalObject as Being; if (being != null) { if (being.IsALivingBeing) { if (being.IsAlive) { universalTimeUnitPassReport.AliveBeings++; } if (being.IsDecomposed) { ObjectToRemoveFromExistanceOnNextOccasion.Add(universalObject); } } } ActionReport actionReport = universalObject.DoAnythingYoucanDoOrWantTo(this); AttackReport attackReport = actionReport as AttackReport; if (attackReport != null) { universalTimeUnitPassReport.Attacks.Add(attackReport.attackPath); } } return(universalTimeUnitPassReport); }
public async Task <Bitmap> Draw(CancellationToken cancel, List <UniversalObject> objects, bool showAttacks, UniversalTimeUnitPassReport report, Bitmap otherBitmap) { Bitmap bitmap = (Bitmap)otherBitmap.Clone(); await Task.Run(() => { try { cancel.ThrowIfCancellationRequested(); using (Graphics graphics = Graphics.FromImage(bitmap)) { cancel.ThrowIfCancellationRequested(); graphics.Clear(Color.White); List <RectangleF> greenStuff = new List <RectangleF>(); List <RectangleF> redStuff = new List <RectangleF>(); List <RectangleF> grayStuff = new List <RectangleF>(); foreach (UniversalObject universalObject in objects) { cancel.ThrowIfCancellationRequested(); Brush brush = GetBrush(universalObject); Point[] points = new Point[4]; int drawingOffSet = 2; Point backedUpPoint = new Point(universalObject.Position.X - drawingOffSet, universalObject.Position.Y - drawingOffSet); RectangleF rect = new RectangleF(backedUpPoint, new Size(4, 4)); if (universalObject is Being) { Being being = universalObject as Being; if (being.IsAlive) { greenStuff.Add(rect); } else { redStuff.Add(rect); } } else { grayStuff.Add(rect); } } if (showAttacks) { foreach (var attacks in report.Attacks) { cancel.ThrowIfCancellationRequested(); graphics.DrawLine(Pens.Black, attacks.Item1, attacks.Item2); } } cancel.ThrowIfCancellationRequested(); if (greenStuff.Count != 0) { graphics.FillRectangles(GreenBrush, greenStuff.ToArray()); } if (redStuff.Count != 0) { graphics.FillRectangles(RedBrush, redStuff.ToArray()); } if (grayStuff.Count != 0) { graphics.FillRectangles(Graybush, grayStuff.ToArray()); } } } catch (OperationCanceledException) { } } ); return(bitmap); }