/// <summary> /// Runs a series of tasks related to a frame. /// </summary> /// <param name="deltaTime">The timefactor to consider</param> private static void runGameLoopTasks(double deltaTime) { List <Task> tasks = new List <Task>(); // Move opponents foreach (IMoveable opponent in Opponents.Values) { tasks.Add(Task.Factory.StartNew(() => opponent.Move(deltaTime))); } //Remove out of bounds bullets, and move the rest foreach (IBullet bullet in Bullets.Values) { tasks.Add(Task.Factory.StartNew(() => { Point p = UIDispatcher.Invoke(() => { return(bullet.BulletShip.Shape.Ray.CenterPoint); }); if (bulletOutOfBounds(p)) { BulletAdapter.removeBulletFromCanvas(bullet.ID); Bullets.TryRemove(bullet.ID, out IBullet bulletOut); } else { bullet.BulletShip.Move(deltaTime); } })); } Task.WaitAll(tasks.ToArray()); }
private static void removeBullet(BulletDTO bullet) { BulletAdapter.removeBulletFromCanvas(bullet.ID); }
/// <summary> /// Checks for rough collissions on a entity and then runs a intersects detection /// </summary> public static void runCollisionDetection() { if (GameController.DebugFrameTimings) { FrameDebugTimer.startCollisionTimer(); } List <Task> taskList = new List <Task>(); if (GameController.Resources != null) { RoughCollition rc = new RoughCollition(); ConcurrentBag <IResource> collidedWithResource = new ConcurrentBag <IResource>(); ConcurrentBag <IBullet> collidedWithBullet = new ConcurrentBag <IBullet>(); IShape player = GameController.Player.PlayerShip.Shape; List <Task> tl = new List <Task>(); IEnumerable <IResource> roughResourceCollitions = rc.checkCollision(player, GameController.Resources.Values, (r) => { return(r.Shape); }); foreach (IResource resource in roughResourceCollitions) { tl.Add(Task.Factory.StartNew(() => { UIDispatcher.Invoke(() => { if (resource.Shape.Polygon.RenderedGeometry.Bounds.IntersectsWith(player.Polygon.RenderedGeometry.Bounds)) { collidedWithResource.Add(resource); } }); })); } Task.WaitAll(tl.ToArray()); tl.Clear(); IEnumerable <IBullet> roughBulletCollitions = rc.checkCollision(player, GameController.Bullets.Values /*.Where(x => x.ID == GameController.Username)*/, (b) => { return(b.BulletShip.Shape); }); foreach (IBullet bullet in roughBulletCollitions) { tl.Add(Task.Factory.StartNew(() => { UIDispatcher.Invoke(() => { if (bullet.PlayerID != GameController.Player.Name && player.Polygon.RenderedGeometry.Bounds.IntersectsWith(bullet.BulletShip.Shape.Polygon.RenderedGeometry.Bounds)) { collidedWithBullet.Add(bullet); } }); })); } Task.WaitAll(tl.ToArray()); tl.Clear(); tl.Add(Task.Factory.StartNew(() => { while (!collidedWithResource.IsEmpty) { if (collidedWithResource.TryTake(out IResource resource)) { taskList.Add(taskFactory.StartNew(async() => { bool result = await NetworkController.GameService.playerCollectedResource(resource); if (result) { if (GameController.Resources != null) { System.Windows.Shapes.Polygon p = GameController.Resources[resource.ID].Shape.Polygon; UIDispatcher.Invoke(() => { ArenaController.ArenaCanvas.Children.Remove(p); }); if (!GameController.Resources.TryRemove(resource.ID, out IResource res)) { collidedWithResource.Add(resource); } } } })); } } })); tl.Add(Task.Factory.StartNew(() => { while (!collidedWithBullet.IsEmpty) { if (collidedWithBullet.TryTake(out IBullet bullet)) { taskList.Add(taskFactory.StartNew(async() => { bool result = await NetworkController.GameService.playerGotShot(BulletAdapter.bulletToDTO(bullet)); if (result) { if (GameController.Bullets != null) { BulletAdapter.removeBulletFromCanvas(bullet.ID); } } })); } else { collidedWithBullet.Add(bullet); } } // TODO DEBUG - Stops collision Timer })); Task.WaitAll(taskList.ToArray()); } }