Exemple #1
0
 /// <summary>
 /// Executed when the player left-clicks on the grid.
 /// </summary>
 /// <param name="helper">The InputHelper used for mouse input.</param>
 public virtual void OnLeftClick(InputHelper helper)
 {
 }
        /// <summary>
        /// Executed when the player left-clicks on the grid.
        /// </summary>
        /// <param name="helper">The InputHelper used for mouse input.</param>
        public override void OnLeftClick(InputHelper helper)
        {
            bool animationBusy = false;

            ForEach(obj => {
                Tile t = obj as Tile;

                if (t.Occupied && (t.Unit as Soldier).duringAttack)
                {
                    animationBusy = true;
                    return;
                }
            });
            if (animationBusy)
            {
                return;
            }

            // Get the current Tile under the mouse
            Tile clickedTile = GetTileUnderMouse(helper, true);

            // Do nothing if there is no clicked tile.
            if (clickedTile == null)
            {
                return;
            }

            // If the player had a tile selected and it contains a Unit...
            if (SelectedTile != null && SelectedTile.Occupied)
            {
                // ... move the Unit there, if the square is not occupied and the unit is capable, then unselect the tile.
                SelectedTile.Unit.TargetPosition = clickedTile.PositionInGrid;
                Point movePos = Pathfinding.MoveTowardsTarget(SelectedTile.Unit);

                if (CheckMoveUnit(movePos, SelectedTile.Unit) || CheckAttackSoldier(clickedTile.PositionInGrid, (Soldier)SelectedTile.Unit) || movePos.Equals(SelectedTile.Unit.PositionInGrid))
                {
                    SelectTile(InvalidTile);
                    return;
                }
            }

            // Check if the player clicked a tile with a Unit on it, and select it if it's there.
            else if (clickedTile.Occupied && clickedTile.Unit.Owner == CurrentPlayer && clickedTile.Unit.HasAction)
            {
                // If the Unit can walk, show where it is allowed to walk.
                if (!clickedTile.Unit.HasMoved)
                {
                    walkablePositions = Pathfinding.ReachableTiles(clickedTile.Unit, Width, Height);
                    SetUnitWalkingOverlay(walkablePositions);
                }

                // This unit can be selected. Show the player it is selected too
                SelectTile(clickedTile.PositionInGrid);

                // Add an overlay for enemy units that can be attacked
                if (!(clickedTile.Unit as Soldier).HasAttacked)
                {
                    SetUnitAttackingOverlay((Soldier)clickedTile.Unit);
                }
            }
        }