Пример #1
0
        public void OnMouseClicked(MouseButtonID button, GameTime gameTime)
        {
            var mousePosition = MouseManager.GetMousePosition();

            if (button == MouseButtonID.Left && Dragging)
            {
                Dragging = false;

                if (DragRectWorldSpace.Width == 0 && DragRectWorldSpace.Height == 0)
                {
                    var mouseWorldPos = GameplayState.Camera.ScreenToWorldPosition(mousePosition);

                    DragRectWorldSpace.X      = (int)mouseWorldPos.X;
                    DragRectWorldSpace.Y      = (int)mouseWorldPos.Y;
                    DragRectWorldSpace.Width  = 1;
                    DragRectWorldSpace.Height = 1;

                    CheckSingleSelection();
                }

                if (SelectedShips.Count == 0)
                {
                    SelectHomeShip();
                }
            }

            if (button == MouseButtonID.Left && MinimapDragging)
            {
                var relativeMousePos = mousePosition - GameplayState.MinimapFrame.Position;

                var worldSize    = new Vector2(Config.WorldWidth, Config.WorldHeight);
                var minimapSize  = new Vector2(GameplayState.MinimapFrame.Width, GameplayState.MinimapFrame.Height);
                var minimapRatio = minimapSize / worldSize;

                var minimapWorldMousePos = relativeMousePos / minimapRatio;
                GameplayState.Camera.CenterPosition(minimapWorldMousePos);

                MinimapDragging = false;
            }

            if (button == MouseButtonID.Right)
            {
                var mouseWorldPos = GameplayState.Camera.ScreenToWorldPosition(mousePosition);

                if (SelectedShips.Count == 0)
                {
                    GameplayState.WorldManager.PlayerEntity.StateMachine.GetState <PlayerTravelingState>().Target = mouseWorldPos;
                    GameplayState.WorldManager.PlayerEntity.SetState <PlayerTravelingState>();
                }
                else
                {
                    foreach (var kvp in SelectedShipTypes)
                    {
                        switch (kvp.Key)
                        {
                        case ShipType.Miner:
                        {
                            Asteroid target = GameplayState.WorldManager.GetAsteroidAtWorldPosition(mouseWorldPos);

                            if (target != null && target.ResourceType != ResourceType.None)
                            {
                                foreach (var miner in kvp.Value)
                                {
                                    var traveling = miner.StateMachine.GetState <MinerTravelingState>();
                                    traveling.Target = target;
                                    miner.SetState(traveling);
                                }
                            }
                            else
                            {
                                var homeShip = GameplayState.WorldManager.GetShipAtWorldPosition(mouseWorldPos, true, new List <ShipType>()
                                    {
                                        ShipType.HomeShip
                                    });

                                if (homeShip != null)
                                {
                                    foreach (Miner miner in kvp.Value)
                                    {
                                        miner.CurrentMiningTarget = null;
                                        var returning = miner.GetState <MinerReturningState>();
                                        returning.Target = miner.Owner;
                                        miner.SetState(returning);
                                    }
                                }
                                else
                                {
                                    foreach (Miner miner in kvp.Value)
                                    {
                                        miner.CurrentMiningTarget = null;
                                        var followPosition = miner.GetState <ShipPatrolPositionState>();
                                        followPosition.Target = mouseWorldPos;
                                        miner.SetState(followPosition);
                                    }
                                }
                            }
                        }
                        break;

                        case ShipType.Fighter:
                        case ShipType.Bomber:
                        {
                            var newDefendTarget = GameplayState.WorldManager.GetShipAtWorldPosition(mouseWorldPos);

                            if (newDefendTarget != null)
                            {
                                if (newDefendTarget.IsPlayerShip)
                                {
                                    foreach (Ship ship in kvp.Value)
                                    {
                                        AIHelper.SmallDefendTarget(ship, newDefendTarget);
                                    }
                                }
                                else
                                {
                                    foreach (Ship ship in kvp.Value)
                                    {
                                        AIHelper.TrySmallAttackTarget(ship, newDefendTarget);
                                    }
                                }
                            }
                            else
                            {
                                foreach (Ship ship in kvp.Value)
                                {
                                    AIHelper.SmallDefendPosition(ship, mouseWorldPos);
                                }
                            }
                        }
                        break;

                        case ShipType.RepairShip:
                        {
                            var newDefendTarget = GameplayState.WorldManager.GetShipAtWorldPosition(mouseWorldPos);

                            if (newDefendTarget != null)
                            {
                                if (newDefendTarget.IsPlayerShip && newDefendTarget.CurrentArmourHP < newDefendTarget.BaseArmourHP)
                                {
                                    foreach (RepairShip ship in kvp.Value)
                                    {
                                        ship.SetRepairTarget(newDefendTarget);
                                    }
                                }
                                else
                                {
                                    foreach (Ship ship in kvp.Value)
                                    {
                                        AIHelper.BigDefendTarget(ship, newDefendTarget);
                                    }
                                }
                            }
                            else
                            {
                                foreach (Ship ship in kvp.Value)
                                {
                                    AIHelper.BigDefendPosition(ship, mouseWorldPos);
                                }
                            }
                        }
                        break;

                        case ShipType.Battleship:
                        case ShipType.Carrier:
                        case ShipType.SupportCruiser:
                        case ShipType.HeavyCruiser:
                        case ShipType.MissileFrigate:
                        case ShipType.BeamFrigate:
                        {
                            var newDefendTarget = GameplayState.WorldManager.GetShipAtWorldPosition(mouseWorldPos);

                            if (newDefendTarget != null)
                            {
                                if (newDefendTarget.IsPlayerShip)
                                {
                                    foreach (Ship ship in kvp.Value)
                                    {
                                        AIHelper.BigDefendTarget(ship, newDefendTarget);
                                    }
                                }
                                else
                                {
                                    foreach (Ship ship in kvp.Value)
                                    {
                                        AIHelper.TryBigAttackTarget(ship, newDefendTarget);
                                    }
                                }
                            }
                            else
                            {
                                foreach (Ship ship in kvp.Value)
                                {
                                    AIHelper.BigDefendPosition(ship, mouseWorldPos);
                                }
                            }
                        }
                        break;
                        }
                    }
                }
            } // right mouse button
        }     // OnMouseClicked