Пример #1
0
    private void StartClient()
    {
        TankInputModule.Instance.Init(state => Send(Env.ServerName, state));

        for (var i = 0; i < 15; i++)
        {
            Send(i + "." + Env.ServerName, new JoinEvent());
        }

        _client.OnMessage += (src, data) => UnityMainThreadDispatcher.Instance().Enqueue(() =>
        {
            Env.ServerName = src;
            switch (GetMessageType(data))
            {
            case MessageType.InitialTankStates:
                Debug.Log(src);
                Debug.Log(_tanks);
                goto case MessageType.TankStates;

            case MessageType.TankStates:
                var tanks = TankStates.FromByteArray(data);
                for (var i = 0; i < tanks.States.Length; i++)
                {
                    var state = tanks.States[i];
                    TankManager tank;
                    if (tanks.Type == MessageType.InitialTankStates)
                    {
                        tank = AddTank(state.publicKey);
                        if (state.publicKey.Equals(_publicKey))
                        {
                            HUDController.Instance.InitHudPlayer(tank.Health);
                            tank.Follow();
                        }
                    }
                    else
                    {
                        tank = _tanksList[i];
                    }

                    if (state.respawned)
                    {
                        tank.Respawn(
                            new Vector3(state.moveX, 0, state.moveY),
                            new Vector3(0, state.rotation.Value, 0)
                            );
                    }
                    else
                    {
                        tank.SetDesiredMovementPosition(new Vector2(state.moveX, state.moveY));
                    }
                    tank.SetDesiredFirePosition(new Vector3(state.fireX, 0, state.fireY));
                    tank.SetFireIsHeld(state.fire);
                }

                break;

            case MessageType.ChatMessage:
                var message = ChatMessage.FromByteArray(data);
                UIChat.ShowMessage(message.Message);
                break;

            case MessageType.JoinEvent:
                throw new ArgumentOutOfRangeException();

            case MessageType.MovementState:
                throw new ArgumentOutOfRangeException();

            default:
                throw new ArgumentOutOfRangeException();
            }
        });
    }
Пример #2
0
    private void StartServer()
    {
        _isServer = true;

        LocalTank = AddTank(_publicKey);

        LocalTank.Follow();

        _client.OnMessage += (src, data) => UnityMainThreadDispatcher.Instance().Enqueue(() =>
        {
            switch (GetMessageType(data))
            {
            case MessageType.JoinEvent:
                AddTank(src);
                SendStates(true);
                break;

            case MessageType.MovementState:
                var tank  = _tanks[src];
                var state = MovementState.FromByteArray(data);

                var cameraDirection = new Vector3(state.moveX, state.moveY, 0);

                if (cameraDirection.sqrMagnitude > 0.01f)
                {
                    var worldUp = Camera.main.transform.TransformDirection(Vector3.up);
                    worldUp.y   = 0;
                    worldUp.Normalize();
                    var worldRight = Camera.main.transform.TransformDirection(Vector3.right);
                    worldRight.y   = 0;
                    worldRight.Normalize();

                    var worldDirection = worldUp * state.moveY + worldRight * state.moveX;
                    var desiredDir     = new Vector2(worldDirection.x, worldDirection.z);
                    if (desiredDir.magnitude > 1)
                    {
                        desiredDir.Normalize();
                    }
                    tank.SetDesiredMovementDirection(new Vector2(state.moveX, state.moveY));
                }
                else
                {
                    tank.SetDesiredMovementDirection(Vector2.zero);
                }

                tank.SetDesiredFirePosition(new Vector3(state.fireX, 0, state.fireY));

                tank.SetFireIsHeld(state.fire);
                break;

            case MessageType.ChatMessage:
                var message = ChatMessage.FromByteArray(data);
                UIChat.ShowMessage(message.Message);
                break;

            case MessageType.InitialTankStates:
                throw new ArgumentOutOfRangeException();

            case MessageType.TankStates:
                throw new ArgumentOutOfRangeException();

            default:
                throw new ArgumentOutOfRangeException();
            }
        });

        TankInputModule.Instance.Init(LocalTank);
        HUDController.Instance.InitHudPlayer(LocalTank.Health);
    }