Esempio n. 1
0
    public void SendAllExcluding(Byteable data, IPEndPoint ip, bool reliable)
    {
        BitBuffer bitBuffer = new BitBuffer();

        data.PutBytes(bitBuffer);
        bitBuffer.Flip();
        SendAllExcluding(bitBuffer.GetByteArray(), ip, reliable);
    }
Esempio n. 2
0
    public void SendAll(Byteable data, bool reliable)
    {
        BitBuffer bitBuffer = new BitBuffer();

        data.PutBytes(bitBuffer);
        bitBuffer.Flip();
        SendAll(bitBuffer.GetByteArray(), reliable);
    }
Esempio n. 3
0
    protected override void HandlePacket(Packet packet)
    {
        BitBuffer bitBuffer = new BitBuffer();

        bitBuffer.PutBytes(packet.getData());
        bitBuffer.Flip();
        ProcessResponse(bitBuffer);
        bitBuffer.Clear();
    }
Esempio n. 4
0
    protected override void HandlePacket(Packet packet)
    {
        BitBuffer bitBuffer = new BitBuffer();

        bitBuffer.PutBytes(packet.getData());
        bitBuffer.Flip();
        PlayerInput input = PlayerInput.fromBytes(bitBuffer);

        switch (input.GetInputType())
        {
        case InputType.MOVEMENT:
            _players[input.GetId()].SetTargetPosition(((MovementInput)input).GetPosition());
            break;

        case InputType.ABILITY:
            AbilityInput ability = (AbilityInput)input;
            Vector3      abilityStartPosition = Vector3.zero;
            switch (ability.GetAbilityType())
            {
            case AbilityType.AUTOATTACK:
                abilityStartPosition = _players[ability.GetId()].transform.position;
                AutoAttack autoAttack = SpawnAutoAttack(ability.GetId(), abilityStartPosition, ability.GetTargetPosition());
                _lastAbilityId = (_lastAbilityId + 1) % 255;
                _autoAttacks.Add(_lastAbilityId, autoAttack);
                break;

            case AbilityType.FREEZE:
                _players[ability.GetId()].SpawnFreeze(ability.GetTargetPosition());
                break;

            case AbilityType.FLASH:
                _players[ability.GetId()].MoveTo(ability.GetTargetPosition());
                _players[ability.GetId()].CancelMovement();
                break;

            case AbilityType.EXPLOSION:
                CreateExplosion(ability.GetTargetPosition());
                break;
            }
            _channel.SendAll(new AbilityResponse(ability, abilityStartPosition), true);
            break;

        case InputType.START_GAME:
            _lastPlayerId++;
            Vector3 startPosition = new Vector3(2f, 1.2f, 0f);
            Player  player        = CreateServerPlayer(new PlayerInfo(_lastPlayerId, startPosition));
            _players.Add(_lastPlayerId, player);
            _channel.Send(new PlayerInfoBroadcast(_lastPlayerId, _players), packet.getAddress(), true);
            PlayerInfo playerInfo = new PlayerInfo(_lastPlayerId, player.GetHealth(), new PositionInfo(startPosition));
            _channel.SendAllExcluding(new NewPlayerEvent(playerInfo), packet.getAddress(), true);
            break;
        }
        packet = _channel.GetPacket();
        bitBuffer.Clear();
    }
Esempio n. 5
0
    override protected void Update()
    {
        base.Update();
        BitBuffer bitBuffer    = new BitBuffer();
        int       totalUpdates = _players.Count;

        bitBuffer.PutByte((byte)totalUpdates);
        foreach (KeyValuePair <int, Player> playerInfoPair in _players)
        {
            Vector3    position   = playerInfoPair.Value.transform.position;
            PlayerInfo playerInfo = new PlayerInfo(
                playerInfoPair.Key,
                playerInfoPair.Value.GetHealth(),
                new PositionInfo(position));
            new PlayerInfoUpdate(playerInfo).PutBytes(bitBuffer);
        }
        bitBuffer.Flip();
        _channel.SendAll(new ResponsesContainer(bitBuffer.GetByteArray()), false);
    }
Esempio n. 6
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;
        }
        }
    }