Example #1
0
 /// <summary>
 /// Clears the target positions of all Units on the grid.
 /// </summary>
 protected void ClearAllTargetPositions()
 {
     // Clear the target positions (because this method kinda sucks :/)
     ForEach(obj => {
         if ((obj as Tile).Occupied)
         {
             Pathfinding.ClearTargetPosition((obj as Tile).Unit);
         }
     });
 }
Example #2
0
        /// <summary>
        /// Checks whether a Unit can be moved to a position, and moves it if it's possible.
        /// </summary>
        /// <param name="newPos">The new position for the Unit to be moved.</param>
        /// <param name="unit">The Unit to be moved.</param>
        /// <returns>True if the Unit was moved, false otherwise.</returns>
        public bool CheckMoveUnit(Point newPos, Unit unit)
        {
            Tile targetTile = this[newPos] as Tile;

            // If the Unit can move to its target, move it and tell the caller that we moved.
            if (unit is Army && !(unit as Army).AllUnitsSelected)
            {
                Army partial = (unit as Army).SplitArmy((unit as Army).SelectedUnits);
                if (partial == null)
                {
                    return(false);
                }

                if (targetTile != null && !targetTile.Occupied && partial.Move(newPos.X, newPos.Y))
                {
                    OnUnitStartMoving(partial, newPos, false);
                    Pathfinding.ClearTargetPosition(unit);
                    Pathfinding.ClearTargetPosition(partial);
                    return(true);
                }
                else
                {
                    (unit as Army).MergeArmy(partial);
                }
            }
            else if (targetTile != null && /*!targetTile.Occupied &&*/ unit.Move(newPos.X, newPos.Y))
            {
                if (!CanMoveUnitToTargetPosition(unit, newPos))
                {
                    newPos = GetNearestAccessibleTile(unit, newPos);
                }
                if (newPos.Equals(InvalidTile))
                {
                    return(false);
                }

                OnUnitStartMoving(unit, newPos);
                return(true);
            }

            // Otherwise, tell the caller that we didn't move.
            return(false);
        }