예제 #1
0
    public static ServerResponse fromBytes(BitBuffer bitBuffer)
    {
        ResponseType type = (ResponseType)bitBuffer.GetByte();

        switch (type)
        {
        case ResponseType.PLAYER_UPDATE:
            return(PlayerInfoUpdate.FromBytes(bitBuffer));

        case ResponseType.ABILITY:
            return(AbilityResponse.FromBytes(bitBuffer));

        case ResponseType.NEW_PLAYER:
            return(NewPlayerEvent.FromBytes(bitBuffer));

        case ResponseType.PLAYER_INFO_BROADCAST:
            return(PlayerInfoBroadcast.FromBytes(bitBuffer));

        case ResponseType.RESPONSES_CONTAINER:
            return(ResponsesContainer.FromBytes(bitBuffer));
        }

        return(null);
    }
예제 #2
0
    private void ProcessResponse(BitBuffer bitBuffer)
    {
        ServerResponse response = ServerResponse.fromBytes(bitBuffer);

        switch (response.GetResponseType())
        {
        case ResponseType.PLAYER_UPDATE: {
            PlayerInfoUpdate playerUpdate = (PlayerInfoUpdate)response;
            PlayerInfo       playerInfo   = playerUpdate.GetPlayerInfo();
            if (_players.ContainsKey(playerInfo.GetId()))
            {
                _players [playerInfo.GetId()].SetTargetPosition(playerInfo.GetPosition());
                _players [playerInfo.GetId()].SetHealth(playerInfo.GetHealth());
            }
            break;
        }

        case ResponseType.ABILITY: {
            AbilityResponse auto = (AbilityResponse)response;
            switch (auto.GetAbilityType())
            {
            case AbilityType.AUTOATTACK:
                SpawnAutoAttack(auto.GetId(), auto.GetStartPosition(), auto.GetPosition());
                break;

            case AbilityType.FREEZE:
                _players[auto.GetId()].SpawnFreeze(auto.GetPosition());
                break;

            case AbilityType.FLASH:
                _players[auto.GetId()].MoveTo(auto.GetPosition());
                _players[auto.GetId()].CancelMovement();
                break;

            case AbilityType.EXPLOSION:
                CreateExplosion(auto.GetPosition());
                break;
            }
            break;
        }

        case ResponseType.NEW_PLAYER: {
            NewPlayerEvent newPlayerEvent = (NewPlayerEvent)response;
            PlayerInfo     playerInfo     = newPlayerEvent.GetPlayerInfo();
            Player         player         = CreatePlayer(playerInfo);
            _players.Add(playerInfo.GetId(), player);
            break;
        }

        case ResponseType.PLAYER_INFO_BROADCAST: {
            PlayerInfoBroadcast playerInfoBroadcast = (PlayerInfoBroadcast)response;
            _localId = playerInfoBroadcast.GetId();
            foreach (PlayerInfo playerInfo in playerInfoBroadcast.getPlayersInfo())
            {
                Player player = CreatePlayer(playerInfo);
                _players.Add(playerInfo.GetId(), player);
            }
            break;
        }

        case ResponseType.RESPONSES_CONTAINER: {
            byte[]    responses       = ((ResponsesContainer)response).GetData();
            BitBuffer containerBuffer = new BitBuffer();
            containerBuffer.PutBytes(responses);
            containerBuffer.Flip();
            int totalResponses = containerBuffer.GetByte();
            for (int i = 0; i < totalResponses; i++)
            {
                ProcessResponse(containerBuffer);
            }
            break;
        }
        }
    }