Exemplo n.º 1
0
    private INetworkMessage ReadCaveQuestMessage(DataStreamReader reader, Message message)
    {
        switch (message.messageType)
        {
        case MessageType.PlayerTurn:
            var playerTurnMessage = new PlayerMessage(message.id, message.messageType);
            playerTurnMessage.Deserialize(ref reader);

            turn = playerTurnMessage.playerID == playerID;

            UpdateUI(turn);

            return(playerTurnMessage);

        case MessageType.RoomInfo:
            var roomInfo = new RoomInfo(message.id);
            roomInfo.Deserialize(ref reader);

            currentRoomInfo = roomInfo;
            StartCoroutine(DrawRoom(roomInfo));

            return(roomInfo);

        case MessageType.PlayerEnterRoom:
            var playerEnterRoom = new PlayerMessage(message.id, message.messageType);
            playerEnterRoom.Deserialize(ref reader);

            foreach (var textMesh in FindObjectsOfType <TextMesh>())
            {
                if (textMesh.text == playerEnterRoom.playerID.ToString())
                {
                    Destroy(textMesh.transform.parent.gameObject);
                    break;
                }
            }

            currentRoomInfo.numberOfOtherPlayers++;
            var otherPlayerIDs = new NativeArray <int>(currentRoomInfo.numberOfOtherPlayers, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            for (int i = 0; i < currentRoomInfo.otherPlayerIDs.Length; i++)
            {
                var otherPlayerID = currentRoomInfo.otherPlayerIDs[i];
                otherPlayerIDs[i] = otherPlayerID;
            }
            otherPlayerIDs[currentRoomInfo.numberOfOtherPlayers - 1] = playerEnterRoom.playerID;
            currentRoomInfo.otherPlayerIDs = otherPlayerIDs.ToArray();
            otherPlayerIDs.Dispose();

            DrawPlayer(playerEnterRoom.playerID);

            return(playerEnterRoom);

        case MessageType.PlayerLeaveRoom:
            var playerLeaveRoom = new PlayerMessage(message.id, message.messageType);
            playerLeaveRoom.Deserialize(ref reader);

            foreach (var textMesh in FindObjectsOfType <TextMesh>())
            {
                if (textMesh.text == playerLeaveRoom.playerID.ToString())
                {
                    Destroy(textMesh.transform.parent.gameObject);
                    break;
                }
            }

            currentRoomInfo.numberOfOtherPlayers--;
            otherPlayerIDs = new NativeArray <int>(currentRoomInfo.numberOfOtherPlayers, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            bool tempBool = false;
            for (int i = 0; i < currentRoomInfo.otherPlayerIDs.Length; i++)
            {
                var otherPlayerID = currentRoomInfo.otherPlayerIDs[i];
                if (otherPlayerID == playerLeaveRoom.playerID)
                {
                    tempBool = true;
                    continue;
                }

                var i2 = (tempBool) ? i - 1 : i;
                otherPlayerIDs[i2] = otherPlayerID;
            }
            currentRoomInfo.otherPlayerIDs = otherPlayerIDs.ToArray();
            otherPlayerIDs.Dispose();

            return(playerLeaveRoom);

        case MessageType.ObtainTreasure:
            var obtainTreasureMessage = new ObtainTreasure(message.id);
            obtainTreasureMessage.Deserialize(ref reader);

            coinDisplay.text = (int.Parse(coinDisplay.text) + obtainTreasureMessage.amount).ToString();
            currentRoomInfo.treasureInRoom = 0;

            foreach (var hit in Physics2D.BoxCastAll(Vector2.zero, Vector2.one, 0, Vector2.zero, float.PositiveInfinity, LayerMask.GetMask("Raycast Target")))
            {
                if (hit.transform.name == "Treasure(Clone)")
                {
                    Destroy(hit.transform.gameObject);
                    break;
                }
            }

            return(obtainTreasureMessage);

        case MessageType.HitMonster:
            var hitMonsterMessage = new AttackMessage(message.id, message.messageType);
            hitMonsterMessage.Deserialize(ref reader);

            if (hitMonsterMessage.playerID == playerID || currentRoomInfo.otherPlayerIDs.Contains(hitMonsterMessage.playerID))
            {
                if (!(currentRoomInfo.containsMonster = false))
                {
                    foreach (var hit in Physics2D.BoxCastAll(Vector2.zero, Vector2.one, 0, Vector2.zero, float.PositiveInfinity, LayerMask.GetMask("Raycast Target")))
                    {
                        if (hit.transform.name == "Monster(Clone)")
                        {
                            Destroy(hit.transform.gameObject);
                            break;
                        }
                    }
                }
            }

            return(hitMonsterMessage);

        case MessageType.HitByMonster:
            var hitByMonsterMessage = new AttackMessage(message.id, message.messageType);
            hitByMonsterMessage.Deserialize(ref reader);

            if (playerID == hitByMonsterMessage.playerID)
            {
                healthDisplay.text = hitByMonsterMessage.value.ToString();
            }

            return(hitByMonsterMessage);

        case MessageType.PlayerDefends:
            var playerDefendMessage = new AttackMessage(message.id, message.messageType);
            playerDefendMessage.Deserialize(ref reader);

            return(playerDefendMessage);

        case MessageType.PlayerLeftDungeon:
            var playerLeftDungeon = new PlayerMessage(message.id, message.messageType);
            playerLeftDungeon.Deserialize(ref reader);

            foreach (var textMesh in FindObjectsOfType <TextMesh>())
            {
                if (textMesh.text == playerLeftDungeon.playerID.ToString())
                {
                    Destroy(textMesh.transform.parent.gameObject);
                    break;
                }
            }

            return(playerLeftDungeon);

        case MessageType.PlayerDies:
            var playerDiesMessage = new PlayerMessage(message.id, message.messageType);
            playerDiesMessage.Deserialize(ref reader);

            return(playerDiesMessage);

        case MessageType.EndGame:
            var endGameMessage = new EndGameMessage(message.id);
            endGameMessage.Deserialize(ref reader);

            SceneManager.LoadSceneAsync("Ending Screen").completed += (asyncOperation) =>
            {
                foreach (var highScore in endGameMessage.highScores.OrderByDescending(pair => pair.Value))
                {
                    var endingScreenUIBinder = FindObjectOfType <EndingScreenUIBinder>();
                    endingScreenUIBinder.SpawnHighScoreUIField(highScore.Key, highScore.Value);
                }
            };
            Destroy(this);

            return(endGameMessage);

        case MessageType.RequestDenied:
            var requestDeniedMessage = new RequestDeniedMessage(message.id);
            requestDeniedMessage.Deserialize(ref reader);

            return(requestDeniedMessage);

        case MessageType.PlayerLeft:
            var playerLeftMessage = new PlayerMessage(message.id, message.messageType);
            playerLeftMessage.Deserialize(ref reader);

            foreach (var textMesh in FindObjectsOfType <TextMesh>())
            {
                if (textMesh.text == playerLeftMessage.playerID.ToString())
                {
                    Destroy(textMesh.transform.parent.gameObject);
                    break;
                }
            }

            return(playerLeftMessage);

        default:
            return(null);
        }
    }