예제 #1
0
        protected override bool HandleDoubleClick(int x, int y, Event.Button button)
        {
            if (!interf.Ingame)
            {
                return(false);
            }

            if (button != Event.Button.Left)
            {
                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);
            var player      = interf.Player;

            if (interf.IsBuildingRoad)
            {
                if (mapPosition != interf.GetMapCursorPosition())
                {
                    var roadEndPosition = interf.GetBuildingRoad().EndPosition;
                    var road            = Pathfinder.FindShortestPath(map, roadEndPosition, mapPosition, interf.GetBuildingRoad(), int.MaxValue, true);

                    if (road.Length != 0)
                    {
                        int result = interf.ExtendRoad(road);

                        if (result < 0)
                        {
                            PlaySound(Freeserf.Audio.Audio.TypeSfx.NotAccepted);
                        }
                        else if (result == 1)
                        {
                            PlaySound(Freeserf.Audio.Audio.TypeSfx.Accepted);
                        }
                        else
                        {
                            if (interf.Game.BuildFlag(interf.GetMapCursorPosition(), player))
                            {
                                interf.BuildRoad();
                                PlaySound(Freeserf.Audio.Audio.TypeSfx.Accepted);
                            }
                            else
                            {
                                PlaySound(Freeserf.Audio.Audio.TypeSfx.Click);
                            }
                        }
                    }
                    else
                    {
                        PlaySound(Freeserf.Audio.Audio.TypeSfx.NotAccepted);
                    }
                }
                else
                {
                    bool result = interf.Game.BuildFlag(interf.GetMapCursorPosition(), player);

                    if (result)
                    {
                        interf.BuildRoad();
                    }
                    else
                    {
                        PlaySound(Freeserf.Audio.Audio.TypeSfx.NotAccepted);
                    }
                }
            }
            else
            {
                interf.UpdateMapCursorPosition(mapPosition);

                if (map.GetObject(mapPosition) == Map.Object.None ||
                    map.GetObject(mapPosition) > Map.Object.Castle)
                {
                    PlaySound(Freeserf.Audio.Audio.TypeSfx.Click);
                    return(false);
                }

                if (map.GetObject(mapPosition) == Map.Object.Flag)
                {
                    if (map.GetOwner(mapPosition) == player.Index)
                    {
                        interf.OpenPopup(PopupBox.Type.TransportInfo);
                    }

                    player.SelectedObjectIndex = map.GetObjectIndex(mapPosition);
                    PlaySound(Freeserf.Audio.Audio.TypeSfx.Click);
                }
                else
                {
                    // Building
                    if (map.GetOwner(mapPosition) == player.Index || interf.AccessRights != Viewer.Access.Player)
                    {
                        PlaySound(Freeserf.Audio.Audio.TypeSfx.Click);

                        var building = interf.Game.GetBuildingAtPosition(mapPosition);

                        if (building.BuildingType == Building.Type.Castle)
                        {
                            interf.OpenPopup(PopupBox.Type.CastleResources);
                        }
                        else if (!building.IsDone)
                        {
                            interf.OpenPopup(PopupBox.Type.OrderedBld);
                        }
                        else if (building.BuildingType == Building.Type.Stock)
                        {
                            if (!building.IsActive)
                            {
                                return(false);
                            }

                            interf.OpenPopup(PopupBox.Type.CastleResources);
                        }
                        else if (building.BuildingType == Building.Type.Hut ||
                                 building.BuildingType == Building.Type.Tower ||
                                 building.BuildingType == Building.Type.Fortress)
                        {
                            interf.OpenPopup(PopupBox.Type.Defenders);
                        }
                        else if (building.BuildingType == Building.Type.StoneMine ||
                                 building.BuildingType == Building.Type.CoalMine ||
                                 building.BuildingType == Building.Type.IronMine ||
                                 building.BuildingType == Building.Type.GoldMine)
                        {
                            interf.OpenPopup(PopupBox.Type.MineOutput);
                        }
                        else
                        {
                            interf.OpenPopup(PopupBox.Type.BuildingStock);
                        }

                        player.SelectedObjectIndex = map.GetObjectIndex(mapPosition);
                    }
                    else
                    {
                        // Foreign building
                        // TODO handle coop mode
                        if (player.PrepareAttack(mapPosition))
                        {
                            PlaySound(Freeserf.Audio.Audio.TypeSfx.Accepted);
                            interf.OpenPopup(PopupBox.Type.StartAttack);
                        }
                        else
                        {
                            PlaySound(Freeserf.Audio.Audio.TypeSfx.NotAccepted);
                        }
                    }
                }
            }

            return(false);
        }