コード例 #1
0
 private void AttemptChangeDirection(Ship.Ship InpShip, Ship.Ship.Direction NewDirection)
 {
     Ship.Ship.Direction OldDirection = InpShip.LocationDirection;
     InpShip.LocationDirection = NewDirection;
     if (InpShip.WithinArea(Map))
     {
         return;
     }
     InpShip.LocationDirection = OldDirection;
 }
コード例 #2
0
        /// <summary>Adds ships to the map based on user input</summary>
        /// <param name="InpShips">The list of ships to add to the map.</param>
        /// <returns>A string representation of the set up pattern of the ships, used for LAN games</returns>
        public string ManualAddShips(List <Ship.Ship> InpShips)
        {
            List <Ship.Ship> q = new List <Ship.Ship>();
            int num            = 0;

            foreach (Ship.Ship i in InpShips)
            {
                Ship.Ship t = (Ship.Ship)i.Clone();
                t.Number = num++;
                q.Add(t);
            }


            bool movingExisting = false;

            do
            {
                if (!movingExisting)
                {
                    Map.Selected = q[QIndexSelected];
                }
                else
                {
                    Map.Selected = Map.Ships[MapIndexSelected];
                }
                Console.Clear();

                Map.PrintMap(true, Map.Display.Ship, -1);

                Console.WriteLine("Ship selected: " + Map.Selected.ShipName);

                if (Advanced)
                {
                    string t = "";
                    if (Map.Selected.Attacks != null)
                    {
                        foreach (Attack.Attack i in Map.Selected.Attacks)
                        {
                            t += ", " + i.AttackString;
                        }
                        t = t.Substring(1, t.Length - 1);
                    }

                    Console.WriteLine("Anti-ship attacks:" + t);
                    t = "";
                    if (Map.Selected.AA != null)
                    {
                        foreach (Attack.Attack i in Map.Selected.AA)
                        {
                            if (i.Amount > 0)
                            {
                                t += ", " + i.AttackString;
                            }
                            else if (i.Amount != 0)
                            {
                                t += ", " + i.AttackName;
                            }
                        }
                        t = t.Substring(1, t.Length - 1);
                    }
                    Console.WriteLine("Anti-air attacks:" + t);
                    Console.WriteLine("Aircraft Storage: " + (!Map.Selected.CanCarryPlanes ? "un" : "") + "availabe");
                }
                Console.WriteLine();
                if (movingExisting)
                {
                    Console.WriteLine("The ship selected has been placed on the map, use \"c\" to pick it up.");
                }
                else
                {
                    Game.DisplayControl("The ship selected has not been placed on the map, use SPACE to place it.", Map.CanPlace(Map.Selected));
                }
                Game.DisplayControl("Use wasd to change the ship's direction", !movingExisting);
                Game.DisplayControl("Use arrow keys to change the ship's location", !movingExisting);
                Game.DisplayControl("Use R/T to select ships that have not been placed.", q.Count > 0);
                Game.DisplayControl("Use F/G to select ships that have been placed.", Map.Ships.Count > 0);
                Game.DisplayControl("Use ENTER to finish placing ships when all ships have been placed.", q.Count <= 0);

                Ship.Ship.Direction lastDir = Map.Selected.LocationDirection;
                ConsoleKey          k;
                switch (k = Console.ReadKey().Key)
                {
                case ConsoleKey.RightArrow:
                case ConsoleKey.UpArrow:
                case ConsoleKey.LeftArrow:
                case ConsoleKey.DownArrow:
                    if (!movingExisting)
                    {
                        AttemptMove(Map.Selected, k);
                    }
                    break;

                case ConsoleKey.D:
                case ConsoleKey.W:
                case ConsoleKey.A:
                case ConsoleKey.S:
                    if (!movingExisting)
                    {
                        AttemptChangeDirection(Map.Selected, k);
                    }
                    break;

                case ConsoleKey.R:
                case ConsoleKey.T:
                    if (q.Count > 0)
                    {
                        if (!movingExisting)
                        {
                            Program.AdjustListSelected(ref QIndexSelected, k, q.Count);
                        }
                        else
                        {
                            movingExisting = false;
                        }
                    }
                    break;

                case ConsoleKey.Spacebar:
                    if (!movingExisting && Map.CanPlace(Map.Selected))
                    {
                        Map.AddShip(Map.Selected);
                        q.Remove(Map.Selected);
                        if (q.Count == 0)
                        {
                            movingExisting = true;
                        }
                        Program.WrapAdd(ref QIndexSelected, 0, q.Count);
                    }
                    break;

                case ConsoleKey.F:
                case ConsoleKey.G:
                    if (Map.Ships.Count > 0)
                    {
                        if (movingExisting)
                        {
                            Program.AdjustListSelected(ref MapIndexSelected, k, Map.Ships.Count);
                        }
                        else
                        {
                            movingExisting = true;
                        }
                    }
                    break;

                case ConsoleKey.C:
                    if (movingExisting)
                    {
                        movingExisting = false;
                        q.Insert(QIndexSelected, Map.Selected);
                        Map.Ships.Remove(Map.Selected);
                        Program.WrapAdd(ref MapIndexSelected, 0, Map.Ships.Count);
                    }
                    break;

                case ConsoleKey.Enter:
                    if (q.Count == 0)
                    {
                        string outp = "";
                        foreach (Ship.Ship i in Map.Ships)
                        {
                            outp += i.Number + LanHost.FieldDelimiter + ((int)i.LocationDirection).ToString() + LanHost.FieldDelimiter + i.Location[0] + LanHost.FieldDelimiter + i.Location[1] + LanHost.FieldDelimiter;
                        }
                        return(outp.Substring(0, outp.Length - LanHost.FieldDelimiter.Length));
                    }
                    break;
                }
            } while (true);
        }