Exemplo n.º 1
0
    private void OnPostRender()
    {
        if (!lineMat)
        {
            Debug.LogError("Please Assign a material on the inspector");
            return;
        }

        GL.Begin(GL.LINES);
        lineMat.SetPass(0);

        Vector3 currentCameraCenterTilePos = Isometric.GetOwnedTilePos(
            Isometric.GetIsometicBasePositionByWorldRay(Camera.main.transform.position, Camera.main.transform.forward));

        Vector3 currentMouseTilePos = Isometric.GetOwnedTilePos(
            Isometric.GetIsometicBasePositionByWorldRay(Camera.main.ScreenToWorldPoint(Input.mousePosition), Camera.main.transform.forward));


        if (activateGrid)
        {
            GL.Color(Color.white);

            for (int x = -10; x < 10; ++x)
            {
                Vector3 left = new Vector3((x + 0.5f) * Isometric.IsometricTileSize.x, 0f
                                           , -9.5f * Isometric.IsometricTileSize.z),
                        right = new Vector3((x + 0.5f) * Isometric.IsometricTileSize.x, 0f,
                                            9.5f * Isometric.IsometricTileSize.z);

                GL.Vertex(Isometric.TranslationIsometricToScreen(left + currentCameraCenterTilePos));
                GL.Vertex(Isometric.TranslationIsometricToScreen(right + currentCameraCenterTilePos));
            }

            for (int z = -10; z < 10; ++z)
            {
                Vector3 down = new Vector3(-9.5f * Isometric.IsometricTileSize.x, 0f,
                                           (z + 0.5f) * Isometric.IsometricTileSize.z),
                        top = new Vector3(9.5f * Isometric.IsometricTileSize.x, 0f,
                                          (z + 0.5f) * Isometric.IsometricTileSize.z);

                GL.Vertex(Isometric.TranslationIsometricToScreen(down + currentCameraCenterTilePos));
                GL.Vertex(Isometric.TranslationIsometricToScreen(top + currentCameraCenterTilePos));
            }
        }


        if (activateHoverGuideLine)
        {
            GL.Color(Color.red);

            Vector3 leftBottom  = currentMouseTilePos - 0.5f * new Vector3(Isometric.IsometricTileSize.x, 0f, Isometric.IsometricTileSize.z);;
            Vector3 leftTop     = currentMouseTilePos + 0.5f * new Vector3(-Isometric.IsometricTileSize.x, 0f, Isometric.IsometricTileSize.z);
            Vector3 rightBottom = currentMouseTilePos + 0.5f * new Vector3(Isometric.IsometricTileSize.x, 0f, -Isometric.IsometricTileSize.z);
            Vector3 rightTop    = currentMouseTilePos + 0.5f * new Vector3(Isometric.IsometricTileSize.x, 0f, Isometric.IsometricTileSize.z);;

            GL.Vertex(Isometric.TranslationIsometricToScreen(leftBottom));
            GL.Vertex(Isometric.TranslationIsometricToScreen(leftTop));

            GL.Vertex(Isometric.TranslationIsometricToScreen(leftBottom));
            GL.Vertex(Isometric.TranslationIsometricToScreen(rightBottom));

            GL.Vertex(Isometric.TranslationIsometricToScreen(leftTop));
            GL.Vertex(Isometric.TranslationIsometricToScreen(rightTop));

            GL.Vertex(Isometric.TranslationIsometricToScreen(rightBottom));
            GL.Vertex(Isometric.TranslationIsometricToScreen(rightTop));
        }
        GL.End();
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            currentEditorMode = (EditorMode)(((int)currentEditorMode + 1) % (int)EditorMode.E_LENGTH);
            if (previewTileTransform != null)
            {
                previewTileTransform.gameObject.SetActive(!(currentEditorMode > 0));
            }
        }

        if (Input.GetKeyDown(KeyCode.Minus))
        {
            brushHeight--;
        }
        else if (Input.GetKeyDown(KeyCode.Equals))
        {
            brushHeight++;
        }

        currentMouseTilePos = Isometric.GetOwnedTilePos(
            Isometric.GetIsometicBasePositionByWorldRay(Camera.main.ScreenToWorldPoint(Input.mousePosition),
                                                        Camera.main.transform.forward));
        currentMouseTilePos.y = brushHeight * Isometric.IsometricTileSize.y;

        if (previewTileTransform != null)
        {
            previewTileTransform.position = currentMouseTilePos;
        }

        if (Input.GetKey(KeyCode.Mouse0))
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }

            switch (currentEditorMode)
            {
            case EditorMode.E_PLACE:
            {
                if (currentTile != null)
                {
                    tileManager.AddTile(currentMouseTilePos, currentTile);
                }
            }
            break;

            case EditorMode.E_DELETE:
            {
                tileManager.RemoveTile(currentMouseTilePos);
            }
            break;

            default:
                break;
            }
        }

        if (Input.GetKey(KeyCode.Mouse1))
        {
            GameObject tile = tileManager.GetTile(currentMouseTilePos);
            if (tile != null)
            {
                CurrentTile = tile;
            }
        }
    }