Example #1
0
    public void Update()
    {
        while (actions.Count > 0 && actions.First.Value.Item1 <= Time.time)
        {
            actions.First.Value.Item2();
            actions.RemoveFirst();
        }

        if (clientState == ClientState.Countdown || clientState == ClientState.Running)
        {
            if (Input.mouseScrollDelta.y > 0 && gameScale < Config.GAME_MAX_SCALE)
            {
                gameScale += Input.mouseScrollDelta.y;
                if (gameScale > Config.GAME_MAX_SCALE)
                {
                    gameScale = Config.GAME_MAX_SCALE;
                }
                gameArea.transform.localScale = new Vector3(gameScale, gameScale, 1);
            }
            else if (Input.mouseScrollDelta.y < 0 && gameScale > Config.GAME_MIN_SCALE)
            {
                gameScale += Input.mouseScrollDelta.y;
                if (gameScale < Config.GAME_MIN_SCALE)
                {
                    gameScale = Config.GAME_MIN_SCALE;
                }
                gameArea.transform.localScale = new Vector3(gameScale, gameScale, 1);
            }
        }

        if (clientState == ClientState.Running)
        {
            /*if (Input.GetKeyDown(KeyCode.I))
             * {
             *  Interpolate.interpolate = !Interpolate.interpolate;
             *
             *  if (Interpolate.interpolate)
             *  {
             *      //interpolateButton.GetComponentInChildren<TextMeshProUGUI>().text = "Interpolation ON";
             *  }
             *  else
             *  {
             *      foreach (var action in actions)
             *      {
             *          action.Item2();
             *      }
             *      actions.Clear();
             *
             *      //interpolateButton.GetComponentInChildren<TextMeshProUGUI>().text = "Interpolation OFF";
             *  }
             * }*/

            if (Input.GetKeyDown(KeyCode.P))
            {
                playerPosMsgCountText.gameObject.SetActive(!playerPosMsgCountText.gameObject.activeSelf);
            }


            Vector3 mouseWorldPosition = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, mainCamera.nearClipPlane));
            Vector2 mouseGamePosition  = gameArea.transform.InverseTransformPoint(mouseWorldPosition);

            float yDiff = (Input.mousePosition.y - (Screen.height / 2)) * inputMultiplier;
            float xDiff = (Input.mousePosition.x - (Screen.width / 2)) * inputMultiplier;

            float spaceshipDirection = Mathf.Atan2(yDiff, xDiff);

            float targetX = mouseGamePosition.x, targetY = mouseGamePosition.y;

            spaceships[playerNumber].GetComponent <SpaceshipDrawer>().SetRotation(spaceshipDirection * 180.0f / Mathf.PI);

            bool sendSomething = false;

            if (Input.GetMouseButtonDown(0))
            {
                ShootNormalProjectileAction.StartShootNormalProjectileAction(networkHandler.builder);
                ShootNormalProjectileAction.AddFix(networkHandler.builder, ShootNormalProjectileActionFix.CreateShootNormalProjectileActionFix(networkHandler.builder, targetX, targetY));
                var action = ShootNormalProjectileAction.EndShootNormalProjectileAction(networkHandler.builder).Value;

                ClientActionMessage.StartClientActionMessage(networkHandler.builder);
                ClientActionMessage.AddContentType(networkHandler.builder, ClientAction.ShootNormalProjectileAction);
                ClientActionMessage.AddContent(networkHandler.builder, action);
                var message = ClientActionMessage.EndClientActionMessage(networkHandler.builder);

                networkHandler.Send(message);
                sendSomething = true;
            }
            else if (Input.GetMouseButtonDown(1))
            {
                ShootBouncingProjectileAction.StartShootBouncingProjectileAction(networkHandler.builder);
                ShootBouncingProjectileAction.AddFix(networkHandler.builder, ShootBouncingProjectileActionFix.CreateShootBouncingProjectileActionFix(networkHandler.builder, targetX, targetY));
                var action = ShootBouncingProjectileAction.EndShootBouncingProjectileAction(networkHandler.builder).Value;

                ClientActionMessage.StartClientActionMessage(networkHandler.builder);
                ClientActionMessage.AddContentType(networkHandler.builder, ClientAction.ShootBouncingProjectileAction);
                ClientActionMessage.AddContent(networkHandler.builder, action);
                var message = ClientActionMessage.EndClientActionMessage(networkHandler.builder);

                networkHandler.Send(message);
                sendSomething = true;
            }

            timeToSendVelocity -= Time.deltaTime;

            if (timeToSendVelocity <= 0)
            {
                SendVelocity();

                do
                {
                    timeToSendVelocity += 0.05f;
                } while (timeToSendVelocity <= 0);

                sendSomething = true;
            }

            if (sendSomething)
            {
                networkHandler.Listen();
                networkHandler.DelayListen();
            }
        }

        if (clientState == ClientState.Running)
        {
            timeToShowPlayerPositionMessageCount -= Time.deltaTime;
            if (timeToShowPlayerPositionMessageCount <= 0)
            {
                playerPosMsgCountText.text = Convert.ToString(playerPositionMessageCount);
                playerPositionMessageCount = 0;
                do
                {
                    timeToShowPlayerPositionMessageCount += 1;
                } while (timeToShowPlayerPositionMessageCount <= 0);
            }

            CheckWarningState();
            UpdateTimeToDraw();
        }
    }