Beispiel #1
0
        /// <summary>
        /// Creates a new path node. The cost (or weight) of this node is automatically
        /// calculated according to the predecessor, cell and goal.
        /// </summary>
        /// <param name="predecessor">preceding node</param>
        /// <param name="cell">grid cell represented by the node</param>
        /// <param name="goal">goal coordinate in 2d map space</param>
        public TilePathNode(TilePathNode predecessor, ITileCell cell, Point2D goal)
        {
            Cell        = cell;
            Predecessor = predecessor;
            mGoal       = goal;

            Update();
        }
Beispiel #2
0
        /// <summary>
        ///          Returns all walk spots.
        ///          The first Spot is the unit spawn, the last is the finish.
        /// </summary>
        /// <returns>The walk spots.</returns>
        /// <exception cref="Exception">No unit start cell found!</exception>
        public List <ITileCell> GetWalkSpots()
        {
            if (mWalkpath != null)
            {
                return(mWalkpath);
            }

            mWalkpath = new List <ITileCell>();

            ITileCell cellSpawn = null;
            ITileCell cellHome  = null;

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    if (CollisionLayer[x, y].HasFlag(ECollisionType.MobHome))
                    {
                        cellHome = Layers[0].GetCell(x, y);
                    }
                    else if (CollisionLayer[x, y].HasFlag(ECollisionType.MobSpawn))
                    {
                        cellSpawn = Layers[0].GetCell(x, y);
                    }
                    else if (CollisionLayer[x, y].HasFlag(ECollisionType.WalkSpot))
                    {
                        mWalkpath.Add(Layers[0].GetCell(x, y));
                    }
                }
            }

            if (cellSpawn == null)
            {
                throw new Exception("No unit start cell found!");
            }
            if (cellHome == null)
            {
                throw new Exception("No unit finish cell found!");
            }

            // Sort points
            mWalkpath.Sort(new TileCellComparer());

            // Append spawn & home
            mWalkpath.Insert(0, cellSpawn);
            mWalkpath.Add(cellHome);

            return(mWalkpath);
        }
Beispiel #3
0
		/// <summary>
		/// Returns a collection of neighbour cells. In a simple 2D grid that
		/// should be 8 cells surrounding the current cell. This method must
		/// not return invalid cells, that means it should not include non
		/// existing or invalid cells to begin with.
		/// </summary>
		/// <param name="layer">The layer</param>
		/// <param name="cell">A grid cell of the strategic map</param>
		/// <returns>Collection of neighbour cells</returns>
		public ICollection<ITileCell> GetNeighbourCells(int layer, ITileCell cell) {
			return Layers[layer].GetNeighbourCells(cell);
		}
Beispiel #4
0
 /// <summary>
 /// Calculates a path from start to end. When no path can be found in
 /// reasonable time the search is aborted and an incomplete path is returned.
 /// When refresh is not set to true a cached path is returned where possible.
 /// </summary>
 /// <param name="start">start position in 2d map space</param>
 /// <param name="end">end position in 2d map space</param>
 /// <param name="refresh">force to recalculate the path</param>
 /// <returns></returns>
 public TilePath CalculatePath(ITileCell start, ITileCell end, bool refresh)
 {
     return(CalculatePath(new Point2D(start.X, start.Y), new Point2D(end.X, end.Y), refresh));
 }
Beispiel #5
0
		/// <summary>
		/// Calculates a path from start to end. When no path can be found in
		/// reasonable time the search is aborted and an incomplete path is returned. 
		/// When refresh is not set to true a cached path is returned where possible.
		/// </summary>
		/// <param name="start">start position in 2d map space</param>
		/// <param name="end">end position in 2d map space</param>
		/// <param name="refresh">force to recalculate the path</param>
		/// <returns></returns>
		public TilePath CalculatePath(ITileCell start, ITileCell end, bool refresh) {
			return CalculatePath(new Point2D(start.X, start.Y), new Point2D(end.X, end.Y), refresh);
		}
Beispiel #6
0
 /// <summary>
 /// Returns a collection of neighbour cells. In a simple 2D grid that
 /// should be 8 cells surrounding the current cell. This method must
 /// not return invalid cells, that means it should not include non
 /// existing or invalid cells to begin with.
 /// </summary>
 /// <param name="layer">The layer</param>
 /// <param name="cell">A grid cell of the strategic map</param>
 /// <returns>Collection of neighbour cells</returns>
 public ICollection <ITileCell> GetNeighbourCells(int layer, ITileCell cell)
 {
     return(Layers[layer].GetNeighbourCells(cell));
 }
Beispiel #7
0
		/// <summary>
		/// Returns a collection of neighbour cells. In a simple 2D grid that
		/// should be 8 cells surrounding the current cell. This method must
		/// not return invalid cells, that means it should not include non
		/// existing or invalid cells to begin with.
		/// </summary>
		/// <param name="cell">A grid cell of the strategic map</param>
		/// <returns>Collection of neighbour cells</returns>
		public ICollection<ITileCell> GetNeighbourCells(ITileCell cell) {
			var cells = new List<ITileCell>();

			// Left
			if (IsValidPoint(cell.X - 1, cell.Y)) {
				cells.Add(GetCell(cell.X - 1, cell.Y));
			}
			// Right
			if (IsValidPoint(cell.X + 1, cell.Y)) {
				cells.Add(GetCell(cell.X + 1, cell.Y));
			}
			// Top
			if (IsValidPoint(cell.X, cell.Y + 1)) {
				cells.Add(GetCell(cell.X, cell.Y + 1));
			}
			// Bottom
			if (IsValidPoint(cell.X, cell.Y - 1)) {
				cells.Add(GetCell(cell.X, cell.Y - 1));
			}

			// Top Left
			if (IsValidPoint(cell.X - 1, cell.Y - 1)) {
				cells.Add(GetCell(cell.X - 1, cell.Y - 1));
			}
			// Top right
			if (IsValidPoint(cell.X + 1, cell.Y - 1)) {
				cells.Add(GetCell(cell.X + 1, cell.Y - 1));
			}
			// Bottom Left
			if (IsValidPoint(cell.X - 1, cell.Y + 1)) {
				cells.Add(GetCell(cell.X - 1, cell.Y + 1));
			}
			// Bottom Right
			if (IsValidPoint(cell.X + 1, cell.Y + 1)) {
				cells.Add(GetCell(cell.X + 1, cell.Y + 1));
			}

			return cells;
		}