Exemplo n.º 1
0
        protected override bool HandleClickLeft(int x, int y)
        {
            if (!interf.Ingame)
            {
                return(false);
            }

            // if clicked into the viewport, close notifications and other popups
            interf.CloseMessage();
            interf.ClosePopup();

            var position    = new Position(x, y);
            var mapPosition = map.RenderMap.CoordinateSpace.ViewSpaceToTileSpace(position);

            if (interf.IsBuildingRoad)
            {
                int       distanceX = map.DistanceX(interf.GetMapCursorPosition(), mapPosition) + 1;
                int       distanceY = map.DistanceY(interf.GetMapCursorPosition(), mapPosition) + 1;
                Direction direction;

                if (distanceX == 0)
                {
                    if (distanceY == 1)
                    {
                        direction = Direction.Left;
                    }
                    else if (distanceY == 0)
                    {
                        direction = Direction.UpLeft;
                    }
                    else
                    {
                        return(false);
                    }
                }
                else if (distanceX == 1)
                {
                    if (distanceY == 2)
                    {
                        direction = Direction.Down;
                    }
                    else if (distanceY == 0)
                    {
                        direction = Direction.Up;
                    }
                    else
                    {
                        return(false);
                    }
                }
                else if (distanceX == 2)
                {
                    if (distanceY == 1)
                    {
                        direction = Direction.Right;
                    }
                    else if (distanceY == 2)
                    {
                        direction = Direction.DownRight;
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }

                if (interf.BuildRoadIsValidDirection(direction))
                {
                    var road = interf.GetBuildingRoad();

                    if (road.IsUndo(direction))
                    {
                        // Delete existing path
                        int result = interf.RemoveRoadSegment();

                        if (result < 0)
                        {
                            PlaySound(Freeserf.Audio.Audio.TypeSfx.NotAccepted);
                        }
                        else
                        {
                            PlaySound(Freeserf.Audio.Audio.TypeSfx.Click);
                        }

                        if (!interf.GetBuildingRoad().Valid)
                        {
                            interf.BuildRoadBegin();
                        }
                    }
                    else
                    {
                        // Build new road segment
                        int result = interf.BuildRoadSegment(direction, false);

                        if (result < 0)
                        {
                            PlaySound(Freeserf.Audio.Audio.TypeSfx.NotAccepted);
                        }
                        else if (result == 0)
                        {
                            PlaySound(Freeserf.Audio.Audio.TypeSfx.Click);
                        }
                        else
                        {
                            PlaySound(Freeserf.Audio.Audio.TypeSfx.Accepted);
                        }
                    }
                }
            }
            else
            {
                // Fast building
                if (interf.AccessRights == Viewer.Access.Player &&
                    interf.GetOption(Option.FastBuilding) &&
                    interf.GetMapCursorPosition() == mapPosition)
                {
                    if (!interf.Player.HasCastle)
                    {
                        if (interf.Game.CanBuildCastle(mapPosition, interf.Player))
                        {
                            interf.BuildCastle();
                        }
                    }
                    else if (interf.Game.Map.HasFlag(mapPosition))
                    {
                        interf.BuildRoadBegin();
                    }
                    else if (interf.Game.CanBuildAnything(mapPosition, interf.Player))
                    {
                        if (interf.Game.CanBuildMine(mapPosition))
                        {
                            interf.OpenPopup(PopupBox.Type.MineBuilding);
                        }
                        else if (interf.Game.CanBuildSmall(mapPosition))
                        {
                            interf.OpenPopup(PopupBox.Type.BasicBld);
                        }
                        else if (interf.Game.CanBuildLarge(mapPosition))
                        {
                            interf.OpenPopup(PopupBox.Type.BasicBldFlip);
                        }
                        else
                        {
                            interf.BuildFlag();
                        }
                    }
                }

                interf.UpdateMapCursorPosition(mapPosition);
                PlaySound(Freeserf.Audio.Audio.TypeSfx.Click);
            }

            return(true);
        }