public void TakeElement(string elem)
    {
        if (elem == "ramp")
        {
            BlockHover.showRampGrid();
            BlockHover.hideGrid();
        }
        else
        {
            BlockHover.showGrid();
            BlockHover.hideRampGrid();
        }
        holding  = true;
        currHold = elem;
        if (elem == "remove")
        {
            removeToggle = !removeToggle;
            if (!removeToggle)
            {
                holding = false;
            }
        }

        var manager = GameObject.Find("UIManager");

        if (manager.GetComponent <InvOpen> ().open)
        {
            manager.GetComponent <InvOpen> ().open = false;
            panel.transform.Translate(panel.transform.right * 135 * canvas.GetComponent <Canvas>().scaleFactor);
            panel.transform.Find("InventoryArrow").Rotate(0, 0, 180);
        }
    }
 void activateButtons()
 {
     canPlace = false;
     BlockHover.hideGrid();
     BlockHover.hideRampGrid();
     Positioning.showButtons();
 }
    // Update is called once per frame
    void Update()
    {
        foreach (AvailElement ae in availElements)
        {
            GameObject elem = GameObject.Find(ae.type + "(Clone)");

            if (ep.getObjectNum(ae.type) < ae.count)
            {
                if (elem == null)
                {
                    switch (ae.type)
                    {
                    case "block":
                        elem = block;
                        break;

                    case "setdir":
                        elem = setdir;
                        break;

                    case "ramp":
                        elem = ramp;
                        break;

                    case "curve":
                        elem = curve;
                        break;

                    case "pistonblock":
                        elem = pistonBlock;
                        break;
                    }

                    GameObject newElem = Instantiate(elem) as GameObject;
                    newElem.transform.SetParent(GameObject.Find("Buttons").transform, false);
                    newElem.GetComponent <Button> ().onClick.AddListener(delegate { GameObject.Find("Placing").GetComponent <ElementPlacing>().TakeElement(ae.type); });
                    elem = newElem;
                }


                elem.transform.GetChild(0).GetComponent <Text> ().text = "x" + (ae.count - ep.getObjectNum(ae.type));
            }
            else
            {
                if (elem)
                {
                    Destroy(GameObject.Find(ae.go.name + "(Clone)"));

                    ElementPlacing.holding = false;
                    BlockHover.hideGrid();
                    BlockHover.hideRampGrid();
                }
            }
        }
    }
Пример #4
0
    public void PlaySim()
    {
        Positioning.hideButtons();
        Positioning.isPositioning = false;

        BlockHover.hideGrid();
        BlockHover.hideRampGrid();

        foreach (GameObject child in GameObject.FindGameObjectsWithTag("Spawned Objects"))
        {
            foreach (MonoBehaviour script in child.GetComponents <MonoBehaviour>())
            {
                if (!script.name.Equals("ElementPlacing"))
                {
                    if (!isSimActive)
                    {
                        script.enabled = true;
                    }
                    else
                    {
                        script.enabled = false;
                    }
                }
            }
        }

        if (!isSimActive)
        {
            GameObject.Find("PlaySimButton").GetComponent <Image> ().sprite = GameObject.Find("StopSprite").GetComponent <SpriteRenderer> ().sprite;
            //ball.transform.position = new Vector3 (2, 0.21f, -3.07f);


            BallRespawn.respawnBall();
            ball.GetComponent <Rigidbody> ().isKinematic      = false;
            ball.GetComponent <Rigidbody> ().detectCollisions = true;
        }
        else
        {
            GameObject.Find("PlaySimButton").GetComponent <Image> ().sprite = oldSprite;
            BallRespawn.respawnBall();
            //ball.transform.position = new Vector3 (2, 0.21f, -3.07f);
            ball.GetComponent <Rigidbody> ().isKinematic      = true;
            ball.GetComponent <Rigidbody> ().detectCollisions = false;
            ball.GetComponent <Rigidbody> ().velocity         = Vector2.zero;
            ball.GetComponent <Rigidbody> ().angularVelocity  = Vector2.zero;
        }

        isSimActive = !isSimActive;
        if (isSimActive)
        {
            wt.OnMouseDown();
        }
    }
 public void clearAll()
 {
     foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Spawned Objects"))
     {
         if (obj.GetComponent <ElementProperties> ().removable)
         {
             ep.decNum(obj.name);
             BlockHover.hideGrid();
             BlockHover.hideRampGrid();
             ElementPlacing.holding  = false;
             ElementPlacing.currHold = "";
             Destroy(obj);
             ep.canPlace = true;
         }
     }
 }
 public void RemoveElem()
 {
     if (placedElem.GetComponent <ElementProperties> ().removable)
     {
         ElementPlacing.currHold = ElementPlacing.truncateNumbers(placedElem.name);
         ElementPlacing.holding  = true;
         if (placedElem.name.Contains("ramp"))
         {
             BlockHover.showRampGrid();
             BlockHover.hideGrid();
         }
         else
         {
             BlockHover.showGrid();
             BlockHover.hideRampGrid();
         }
         ep.decNum(placedElem.name);
         Destroy(placedElem);
         placedElem  = null;
         ep.canPlace = true;
     }
 }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (canPlace && !PlaySimulation.isSimActive && (!EventSystem.current.IsPointerOverGameObject()))
            {
                Vector2 mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
                Ray     ray;
                ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                Vector3    pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                allowPlacing();

                BlockHover.hideGrid();
                BlockHover.hideRampGrid();
                if (Physics.Raycast(ray, out hit, 100))
                {
                    bool placed = false;
                    if (holding)
                    {
                        if (hit.collider.gameObject != null && (hit.collider.gameObject.tag == "Spawned Objects"))
                        {
                            Positioning.placedElem = hit.collider.gameObject;
                            activateButtons();
                        }
                        else
                        {
                            bool hitGround     = false;
                            bool hitRampGround = false;
                            foreach (Transform child in ground.transform)
                            {
                                Bounds bounds = child.GetComponent <MeshCollider> ().bounds;
                                if (bounds.Contains(hit.point))
                                {
                                    hitGround = true;
                                    break;
                                }
                            }

                            if (!hitGround && rampGround != null)
                            {
                                foreach (Transform child in rampGround.transform)
                                {
                                    Bounds bounds = child.GetComponent <MeshCollider> ().bounds;
                                    if (bounds.Contains(hit.point))
                                    {
                                        hitRampGround = true;
                                        break;
                                    }
                                }
                            }

                            if ((hitGround || hitRampGround) && !overlaping(hit.point))
                            {
                                var script = invPanel.GetComponent <AvailElemManager> ();

                                GameObject newObj = null;

                                if ((currHold == "ramp") && hitRampGround)
                                {
                                    BlockHover.showRampGrid();
                                    BlockHover.hideGrid();
                                    foreach (Transform child in GameObject.Find("RampGridBlocks").transform)
                                    {
                                        if (child.gameObject.GetComponent <BlockHover> ().checkMouseOver())
                                        {
                                            newObj = Instantiate(ramp);
                                            rampNum++;
                                            newObj.name = "ramp" + rampNum;
                                            newObj.transform.position = new Vector3(Mathf.Round(hit.point.x), hit.point.y + 0.18f, Mathf.Round(hit.point.z));

                                            newObj.transform.rotation = child.rotation;
                                            newObj.transform.position = child.position;
                                            placed = true;
                                            break;
                                        }
                                    }
                                }
                                else if (currHold == "curve" && hitGround)
                                {
                                    newObj = Instantiate(curve);
                                    curveNum++;
                                    newObj.name = "curve" + curveNum;
                                    newObj.transform.position = new Vector3(Mathf.Round(hit.point.x), hit.point.y + 0.375f - 0.12f, Mathf.Round(hit.point.z));
                                    placed = true;
                                }
                                if (placed && newObj)
                                {
                                    newObj.transform.parent = spawnedObjects.transform;
                                    Positioning.placedElem  = newObj;
                                    activateButtons();
                                }
                            }
                        }
                    }

                    if (!placed && hit.collider.gameObject && (hit.collider.gameObject.tag == "Spawned Objects"))
                    {
                        if (hit.collider.gameObject.name.Contains("ramp"))
                        {
                            BlockHover.showRampGrid();
                            BlockHover.hideGrid();
                        }
                        else
                        {
                            BlockHover.showGrid();
                            BlockHover.hideRampGrid();
                        }

                        Positioning.placedElem = hit.collider.gameObject;
                        activateButtons();
                    }
                }
                preventPlacing();
                if (holding && Positioning.placedElem == null)
                {
                    if (currHold == "ramp")
                    {
                        BlockHover.showRampGrid();
                        BlockHover.hideGrid();
                    }
                    else
                    {
                        BlockHover.showGrid();
                        BlockHover.hideRampGrid();
                    }
                }
            }
        }
    }