Beispiel #1
0
    private void MonsterTurn()
    {
        HitByMonsterMessage hitByMonsterMessage = new HitByMonsterMessage();

        for (int i = 0; i < PlayerManager.Instance.Players.Count; i++)
        {
            Players player = PlayerManager.Instance.Players[i];

            Vector2 playerPosition = player.TilePosition;
            Tile    t = GameManager.Instance.CurrentGrid.tilesArray[(int)playerPosition.x, (int)playerPosition.y];

            if ((t.Content == TileContent.Monster) || (t.Content == TileContent.Both))
            {
                if (player.DefendOneTurn)
                {
                    player.DefendOneTurn = false;
                    NetworkManager.SendMessage(networkDriver, hitByMonsterMessage, connections[i]);
                    continue;
                }


                NetworkManager.SendMessage(networkDriver, hitByMonsterMessage, connections[i]);

                player.Health -= 1;
                if (player.Health < 1)
                {
                    PlayerDiesMessage playerDiesMessage = new PlayerDiesMessage();
                    NetworkManager.SendMessage(networkDriver, playerDiesMessage, connections[i]);
                }
            }
        }
    }
    public IEnumerator MonsterAttacks(Monster monster)
    {
        networkJobHandle.Complete();

        //get room
        RoomData currentRoom = serverDataHolder.rooms[monster.roomID[0], monster.roomID[1]];

        //get players insideof room
        List <int> playerIDs = monster.targetPlayers;

        Debug.Log("Monster attack!" + playerIDs.Count);

        if (playerIDs.Count != 0)
        {
            yield return(new WaitForSeconds(.1f));

            networkJobHandle.Complete();

            int randomIndex = Mathf.FloorToInt(Random.Range(0, playerIDs.Count));

            PlayerData data = serverDataHolder.players.Find(x => x.playerIndex == monster.targetPlayers[randomIndex]);

            data.hp -= 3;
            //Debug.Log("random index = " +randomIndex+  "hp" + data.hp);
            //Debug.Log(" id = " +playerIDs[randomIndex]);
            //Debug.Log("target id = " + monster.targetPlayers[randomIndex]);

            if (data.hp <= 0)
            {
                data.hp = 0;
                //player dies
                PlayerDiesMessage dieMessage = new PlayerDiesMessage()
                {
                    PlayerID = monster.targetPlayers[randomIndex]
                };
                monster.targetPlayers.Remove(monster.targetPlayers[randomIndex]);

                serverDataHolder.activePlayerIDs.Remove(data.playerIndex);
                if (serverDataHolder.activePlayerIDs.Count == 0)
                {
                    EndGame();
                }
                SendMessageToAll(dieMessage);
            }
            else
            {
                HitByMonsterMessage hitByMessage = new HitByMonsterMessage()
                {
                    PlayerID = randomIndex,
                    newHP    = (ushort)data.hp
                };

                foreach (int id in playerIDs)
                {
                    SendMessage(hitByMessage, connections[id]);
                }
            }
        }
    }
    public void PlayerDies(ref DataStreamReader reader) // is ok, but needs die animation?
    {
        var message = new PlayerDiesMessage();

        message.DeserializeObject(ref reader);

        if (message.PlayerID == dataHolder.myData.playerIndex)
        {
            dataHolder.game.cRoom.GameOver();
        }
        else
        {
            dataHolder.game.cRoom.PlayerLeftRoom(dataHolder.players.Find(x => x.playerIndex == message.PlayerID));
        }
        dataHolder.game.cRoom.UpdatePlayerUIElements();
    }
Beispiel #4
0
    /// <summary>
    /// Send to all clients when a player dies.
    /// </summary>
    private void SendPlayerDies()
    {
        Debug.Log("Player DED");
        Client activeClient = null;

        inverseActiveDictionary.TryGetValue(currentActivePlayer, out activeClient);

        // remove client from the 2 active clients dictionary
        ActiveClientPlayerDictionary.Remove(activeClient);
        UpdateInverseDictionary();
        playerTurnList.Remove(currentActivePlayer);

        var message = new PlayerDiesMessage()
        {
            PlayerID = activeClient.PlayerID
        };

        foreach (Client c in clientList)
        {
            server.SendMessage(message, c.Connection);
        }
    }
    public void Update()
    {
        networkJobHandle.Complete();

        if (!connection.IsCreated)
        {
            return;
        }

        DataStreamReader reader;

        NetworkEvent.Type cmd;
        while ((cmd = connection.PopEvent(networkDriver, out reader)) != NetworkEvent.Type.Empty)
        {
            if (cmd == NetworkEvent.Type.Connect)
            {
                Debug.Log("Connected to server");
            }
            else if (cmd == NetworkEvent.Type.Data)
            {
                var messageType = (Message.MessageType)reader.ReadUShort();
                Debug.Log("Client Received: " + messageType + " from Host");

                Message message = null;
                switch (messageType)
                {
                case Message.MessageType.None: break;

                case Message.MessageType.NewPlayer: message = new NewPlayerMessage(); break;

                case Message.MessageType.Welcome: message = new WelcomeMessage(); break;

                case Message.MessageType.RequestDenied: message = new RequestDeniedMessage(); break;

                case Message.MessageType.PlayerLeft: message = new PlayerLeftMessage(); break;

                case Message.MessageType.StartGame: message = new StartGameMessage(); break;

                case Message.MessageType.PlayerTurn: message = new PlayerTurnMessage(); break;

                case Message.MessageType.RoomInfo: message = new RoomInfoMessage(); break;

                case Message.MessageType.PlayerEnterRoom: message = new PlayerEnterRoomMessage(); break;

                case Message.MessageType.PlayerLeaveRoom: message = new PlayerLeaveRoomMessage(); break;

                case Message.MessageType.ObtainTreasure: message = new ObtainTreasureMessage(); break;

                case Message.MessageType.HitMonster: message = new HitMonserMessage(); break;

                case Message.MessageType.HitByMonster: message = new HitByMonserMessage(); break;

                case Message.MessageType.PlayerDefends: message = new PlayerDefendsMessage(); break;

                case Message.MessageType.PlayerLeftDungeon: message = new PlayerLeftDungeonMessage(); break;

                case Message.MessageType.PlayerDies: message = new PlayerDiesMessage(); break;

                case Message.MessageType.EndGame:   break;

                default: break;
                }
                if (message != null)
                {
                    message.DeserializeObject(ref reader);
                    receivedMessagesQueue.Enqueue(message);
                }
            }
            else if (cmd == NetworkEvent.Type.Disconnect)
            {
                Debug.Log("Disconnected from server");
                connection = default;
            }
        }
        ProcessMessagesQueue();

        if (Time.time - lastSendTime > STAY_ALIVE_AFTER_SECONDS)
        {
            SendMessage(new NoneMessage());
        }
        networkJobHandle = networkDriver.ScheduleUpdate();
    }
        internal void HandlePlayerDies(Message message)
        {
            PlayerDiesMessage playerDiesMessage = (PlayerDiesMessage)message;

            Menu.Singleton.gameWindow.OutputText(lobby.players[playerDiesMessage.PlayerID].PlayerTextWithColorTag() + " died in the dungeon");
        }