Ejemplo n.º 1
0
    void Update()
    {
        if (!currentButton)
        {
            return;
        }

        var worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        worldPos.z = 0;

        bool overlap = false;

        if (currentHeldObject)
        {
            var halfWidth = currentHeldObject.GetOffset();
            //halfWidth.x *= currentHeldObject.transform.localScale.x;
            //halfWidth.y *= currentHeldObject.transform.localScale.y;
            worldPos.x -= worldPos.x % gridSize.x;
            worldPos.y -= worldPos.y % gridSize.y;
            worldPos   += halfWidth;

            currentHeldObject.transform.position = worldPos;

            if (Input.GetMouseButtonUp(0) && currentTool == Tool.Move)
            {
                currentHeldObject = null;
            }

            for (int i = 0; i < levelObjects.Count; i++)
            {
                //TODO fix error here
                var c2D = currentHeldObject.bc;
                if (c2D.bounds.Intersects(levelObjects[i].bc.bounds))
                {
                    overlap = true;
                    break;
                }
            }

            var objColor = Color.green;
            if (overlap || !bounds)
            {
                objColor = Color.red;
            }

            var sr = new [] { currentHeldObject.GetComponent <SpriteRenderer>() };
            if (sr[0] == null)
            {
                sr = currentHeldObject.GetComponentsInChildren <SpriteRenderer>();
            }

            for (int i = 0; i < sr.Length; i++)
            {
                sr[i].color = objColor;
            }
        }

        if (!overlap && currentTool == Tool.Create && !EventSystem.current.IsPointerOverGameObject() && Input.GetMouseButtonDown(0))
        {
            var o = Instantiate(currentButton.createdObject);
            o.Init(this);
            o.transform.position = worldPos;
            levelObjects.Add(o);
        }
    }