Exemplo n.º 1
0
 public void StartGame()
 {
     gameState           = "Setup";
     attackFeedback      = "";
     attackedCoords      = "";
     latestAttackMessage = null;
     defense.StartGame(totalNumberShips);
     attack.StartGame(totalNumberShips);
     // Establishes connection with the server
     // and sends first message
     Debug.Log("Connecting to the Gameplay server");
     udp = new UdpClient();
     udp.Connect(GameServer, GamePort);
     udp.BeginReceive(new AsyncCallback(OnReceived), udp);
     SendHello();
 }
Exemplo n.º 2
0
    void OnReceived(IAsyncResult result)
    {
        // this is what had been passed into BeginReceive as the second parameter:
        UdpClient socket = result.AsyncState as UdpClient;

        // points towards whoever had sent the message:
        IPEndPoint source = new IPEndPoint(0, 0);

        // get the actual message and fill out the source:
        byte[] message = socket.EndReceive(result, ref source);

        // do what you'd like with `message` here:
        string returnData = Encoding.ASCII.GetString(message);

        Debug.Log("***********************************");
        Debug.Log(returnData);
        var latestMessage = JsonUtility.FromJson <GameCommand>(returnData);

        try{
            switch (latestMessage.cmd)
            {
            case "table":
                Debug.Log("Setting Rival board value");
                UIManager.CurrentRival.boardSet = true;
                break;

            case "attack":
                Debug.Log("Receving Attack Data");
                latestAttackMessage = JsonUtility.FromJson <ReturnAttack>(returnData);
                break;
            }
        }
        catch (Exception e) {
            Debug.Log(e.ToString());
        }
        // schedule the next receive operation once reading is done:
        socket.BeginReceive(new AsyncCallback(OnReceived), socket);
    }
Exemplo n.º 3
0
 void Update()
 {
     if (gameState.StartsWith("Setup"))
     {
         if (gameState == "Setup")
         {
             var missingQtd = totalNumberShips - defense.shipCount;
             StatusText = "Still need to deploy " + missingQtd.ToString() + " ship" + (missingQtd > 1? "s" : "");
         }
         Setup();
     }
     if (latestAttackMessage != null)
     {
         var currentAM = latestAttackMessage;
         latestAttackMessage = null;
         Vector2Int loc = FromCoord(currentAM.coordinates);
         Debug.Log("Dealing with the latest attack message");
         if (gameState == "Turn")
         {
             Debug.Log("Turn");
             if (currentAM.win == "true")
             {
                 Debug.Log("It's a win!");
                 attack.ShowTileText(loc, "H");
                 gameState = "Win";
                 attack.SetIsActive(false);
                 StatusText      = "You won it!";
                 UIManager.State = 5;
                 UpdatePlayerScore(true);
             }
             else
             {
                 if (currentAM.hit == "true")
                 {
                     Debug.Log("You hit something");
                     attack.ShowTileText(loc, "H");
                 }
                 else
                 {
                     Debug.Log("You hit water");
                     attack.ShowTileText(loc, "M");
                 }
                 Debug.Log("Now it's your opponent's turn");
                 gameState  = "Wait";
                 StatusText = "It's opponent's time to attack";
                 attack.SetIsActive(false);
             }
         }
         else if (gameState == "Wait")
         {
             Debug.Log("Wait");
             defense.AttackTile(loc);
             if (currentAM.win == "true")
             {
                 Debug.Log("you lost the game!");
                 gameState = "Lost";
                 attack.SetIsActive(false);
                 StatusText      = "You Lost the game!";
                 UIManager.State = 5;
                 UpdatePlayerScore(false);
             }
             else
             {
                 Debug.Log("Now it's your time to attack");
                 gameState  = "Turn";
                 StatusText = "It's your time to attack";
                 attack.SetIsActive(true);
             }
         }
     }
 }