Ejemplo n.º 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);
        }
Ejemplo n.º 2
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);
        }