private void PathFinding(GameTime gameTime, SimpleTileLayer layer, string UnitID, List <Sentry> Units) { #region Calculate Location if (updateTime > UPDATE_TIME_MAX) // Check every millisecond. { astarThreadWorker = null; AstarManager.AddNewThreadWorker( new Node(new Vector2( (int)(PixelPosition.X / FrameWidth), (int)(PixelPosition.Y / FrameHeight))), new Node(new Vector2( (int)(Target.X) / FrameWidth, (int)(Target.Y) / FrameHeight)), Game, layer, false, UnitID); updateTime = 0f; } else { updateTime += (float)gameTime.ElapsedGameTime.TotalSeconds; } #endregion AstarManager.AstarThreadWorkerResults.TryPeek(out astarThreadWorkerTemp); #region Add Location to WayPoints if (astarThreadWorkerTemp != null) { if (astarThreadWorkerTemp.WorkerIDNumber == UnitID) { AstarManager.AstarThreadWorkerResults.TryDequeue(out astarThreadWorker); if (astarThreadWorker != null) { wayPoint = new WayPoint(); WayPointsList = astarThreadWorker.astar.GetFinalPath(); for (int i = 0; i < WayPointsList.Count; i++) { WayPointsList[i] = new Vector2( WayPointsList[i].X * FrameWidth, WayPointsList[i].Y * FrameHeight); } } } } #endregion #region Avoid Obstacles and Move to Target if (WayPointsList.Count > 0) { Avoidance(gameTime, UnitID); wayPoint.MoveTo(gameTime, this, WayPointsList); } #endregion }
void Astar(GameTime gameTime, MouseRectangle mouseRectangle, Map map, int UnitID, List <Unit> Units) { if (MouseCursor.CurrentMouseState.RightButton == ButtonState.Pressed && MouseCursor.LastMouseState.RightButton == ButtonState.Released && UnitState == State.Selected) { astarThreadWorker = null; AstarManager.AddNewThreadWorker(new Node(new Vector2((int)Position.X / 16, (int)Position.Y / 16)), new Node(new Vector2((int)MouseCursor.CurrentMouseState.X / 16, (int)MouseCursor.CurrentMouseState.Y / 16)), map, false, UnitID); } AstarManager.AstarThreadWorkerResults.TryPeek(out astarThreadWorkerTemp); if (astarThreadWorkerTemp != null) { if (astarThreadWorkerTemp.WorkerIDNumber == UnitID) { AstarManager.AstarThreadWorkerResults.TryDequeue(out astarThreadWorker); if (astarThreadWorker != null) { wayPoint = new WayPoint(); WayPointsList = astarThreadWorker.astar.GetFinalPath(); for (int i = 0; i < WayPointsList.Count; i++) { WayPointsList[i] = new Vector2(WayPointsList[i].X * 16, WayPointsList[i].Y * 16); } } } } if (WayPointsList.Count > 0) { Avoidence(gameTime, Units, UnitID); wayPoint.MoveTo(gameTime, this, WayPointsList, Speed); } }