private void cancelBreaking()
    {
        if (breakingBlock == null)
        {
            return;
        }
        // Cancelled breaking
        ClientActionMessage a = new ClientActionMessage();

        a.Action = ClientActionMessage.Types.ActionType.CancelBreak;
        client.GetClient().sendMessage(a);
        // Debug.Log("Cancelled breaking");
        breakingBlock = null;

        displayingStageTexture = -1;
        selection.SetActive(false);
        selection.GetComponent <MeshRenderer>().material.mainTexture = null;
    }
    // Update is called once per frame
    void LateUpdate()
    {
        // if (Camera.current == null) return;

        if (WindowManager.INSTANCE.transform.childCount > 0)
        {
            return;
        }
        if (WindowManager.INSTANCE.busyTick)
        {
            WindowManager.INSTANCE.busyTick = false;
            return;
        }

        Ray        r = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
        RaycastHit h;
        bool       hit = Physics.Raycast(r, out h);

        if (hit && h.transform.GetComponent <Entity>() != null)
        {
            // hit an entity
            Entity target = h.transform.GetComponent <Entity>();

            if (Input.GetMouseButtonDown(0))
            {
                hitEntity(target, 0);
            }
            else if (Input.GetMouseButtonDown(1))
            {
                hitEntity(target, 1);
            }
            else if (Input.GetMouseButtonDown(2))
            {
                hitEntity(target, 2);
            }

            if (aimingEntity == null || !aimingEntity.IsAlive || target.entityId != ((Entity)aimingEntity.Target).entityId)
            {
                aimingEntity = new WeakReference(target, true);
            }

            cancelBreaking();
            return;
        }
        else
        {
            aimingEntity = null;
        }

        if (!hit || Vector3.Distance(h.point, ClientComponent.INSTANCE.playerObject.transform.position) > 10f)
        {
            hideSelection();
            return;
        }

        Vector3 b = h.point - (0.5f * h.normal);
        int     x = Mathf.FloorToInt(b.x);
        int     y = Mathf.FloorToInt(b.y);
        int     z = Mathf.FloorToInt(b.z);

        showSelection(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));

        if (breakingBlock != null)
        {
            if (x != breakingX || y != breakingY || z != breakingZ)
            {
                cancelBreaking();

                if (Input.GetMouseButton(1))
                {
                    c = true;
                }
            }
            else
            {
                long span = (long)((DateTime.Now - breakingTime).TotalMilliseconds);
                if (span > breakingBlock.GetBreakTime(inventory.GetSelectedItem()))
                {
                    // Should be finished
                    client.chunkManager.setBlockAt(x, y, z, 0);
                    ClientRemoveBlockMessage removeBlockMessage = new ClientRemoveBlockMessage();
                    removeBlockMessage.X = x;
                    removeBlockMessage.Y = y;
                    removeBlockMessage.Z = z;
                    client.GetClient().sendMessage(removeBlockMessage);

                    selection.SetActive(false);
                    selection.GetComponent <MeshRenderer>().material.mainTexture = null;

                    if (Input.GetMouseButtonDown(1))
                    {
                        c = true;
                    }
                    else
                    {
                        c = false;
                    }
                }
                else
                {
                    handAnimation.Play("hand_breaking");

                    int stageTextureId = (int)(((DateTime.Now - breakingTime).TotalMilliseconds / breakingBlock.GetBreakTime(inventory.GetSelectedItem())) * 10f);
                    if (stageTextureId > 9)
                    {
                        stageTextureId = 9;
                    }
                    if (!selection.activeSelf)
                    {
                        selection.SetActive(true);
                    }
                    if (stageTextureId != displayingStageTexture)
                    {
                        displayingStageTexture = stageTextureId;
                        selection.GetComponent <MeshRenderer>().material.mainTexture = breakStageTextures[displayingStageTexture];
                    }
                }
            }
        }

        if (Input.GetMouseButtonDown(1) || c)
        {
            cancelBreaking();
            int id = client.chunkManager.getBlockAt(x, y, z);
            if (id == 0)
            {
                cancelBreaking();
                return;
            }
            c = false;
            // int meta = f & 0xFFFF;
            breakingBlock = Block.prototypes[id];
            breakingX     = x;
            breakingY     = y;
            breakingZ     = z;
            breakingTime  = DateTime.Now;
            // Debug.Log("START BREAKING <" + breakingBlock.GetType().Name + ">");

            ClientActionMessage startBreakingMessage = new ClientActionMessage();
            startBreakingMessage.Action = ClientActionMessage.Types.ActionType.StartBreak;
            startBreakingMessage.BlockX = x;
            startBreakingMessage.BlockY = y;
            startBreakingMessage.BlockZ = z;
            client.GetClient().sendMessage(startBreakingMessage);
        }

        if (Input.GetMouseButtonUp(0))
        {
            cancelBreaking();
            Vector3        adding  = h.point + (0.5f * h.normal);
            SerializedItem holding = inventory.items[inventory.currentSelection];

            touchBlock(holding, x, y, z, h.normal);

            if (holding != null)
            {
                if (holding.Id < Block.prototypes.Length)
                {
                    int addingX = Mathf.FloorToInt(adding.x);
                    int addingY = Mathf.FloorToInt(adding.y);
                    int addingZ = Mathf.FloorToInt(adding.z);
                    Debug.Log(string.Format("ADDING AT ({0},{1},{2})", addingX, addingY, addingZ));
                    // Place block
                    client.chunkManager.setBlockAt(addingX, addingY, addingZ, holding.Id);

                    holding.Count--;
                    if (holding.Count <= 0)
                    {
                        inventory.items[inventory.currentSelection] = null;
                    }

                    handAnimation.Play("hand_place");
                }
            }
        }

        if (Input.GetMouseButtonUp(1))
        {
            cancelBreaking();
            c = false;
        }
    }
Esempio n. 3
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();
        }
    }