Exemplo n.º 1
0
        public static void Setup()
        {
            ShipsToPlace = new Ship[] {
                new Ship(2, Ship.Rotation.Down, new Vector2(1, 1)),
                new Ship(2, Ship.Rotation.Down, new Vector2(1, 1)),
                new Ship(3, Ship.Rotation.Down, new Vector2(1, 1)),
                new Ship(3, Ship.Rotation.Down, new Vector2(1, 1)),
                new Ship(3, Ship.Rotation.Down, new Vector2(1, 1)),
                new Ship(4, Ship.Rotation.Down, new Vector2(1, 1)),
                new Ship(4, Ship.Rotation.Down, new Vector2(1, 1)),
                new Ship(5, Ship.Rotation.Down, new Vector2(1, 1))
            };

            localfield = new Localfield();
            enemyfield = new Enemyfield();

            activeField = localfield;
            activeField.Draw();

            foreach (Ship ship in ShipsToPlace)
            {
                ((Localfield)activeField).PlaceShip(ship);
            }
            (activeField).Draw();
            Networking.SendMessage("game:ready");
            Start();
        }
Exemplo n.º 2
0
 private static void StartAttack()
 {
     activeField = enemyfield;
     enemyfield.Move();
     activeField = localfield;
     activeField.Draw();
 }
Exemplo n.º 3
0
        public static void Start()
        {
            while (Networking.GetMessage() != "game:ready")
            {
                if (Networking.error)
                {
                    ShowEndScreen("Lost Connection", false);
                    return;
                }
            }

            InGame = true;

            if (Networking.HostMode == Networking.State.Client)
            {
                StartAttack();
            }

            if (Networking.HostMode == Networking.State.Client)
            {
                activeField = enemyfield;
                enemyfield.Move();
                activeField = localfield;
                activeField.Draw();
            }

            while (InGame)
            {
                string cmd = Networking.GetMessage();

                if (cmd.StartsWith("game:end("))
                {
                    // example: game:end(nomoreships)
                    ShowEndScreen(cmd.Split('(')[1].Split(')')[0], true);
                }
                else if (cmd == "game:yourmove()")
                {
                    if (!localfield.IsAlive())
                    {
                        EndGame("No more Ships");
                    }
                    else
                    {
                        StartAttack();
                    }
                    {
                        activeField = enemyfield;
                        enemyfield.Move();
                        activeField = localfield;
                        activeField.Draw();
                    }
                }
                else if (cmd.StartsWith("game:attack("))
                {
                    // example: game:attack(1,2)
                    Vector2 pos = new Vector2(Convert.ToInt32(cmd.Split('(')[1].Split(',')[0]), Convert.ToInt32(cmd.Split(')')[0].Split(',')[1]));
                    Networking.SendBool(localfield.Attack(pos));
                }

                if (Networking.error)
                {
                    ShowEndScreen("Lost Connection", false);
                    InGame = false;
                }
            }
        }