示例#1
0
    void Update()
    {
        //Vector3 m = Input.mousePosition;
        //m = new Vector3(m.x, m.y, transform.position.y);
        //Vector3 p = cam.ScreenToWorldPoint(m);

        if (Input.touchCount > 0)
        {
            Touch   touch = Input.touches[0];
            Vector3 m     = touch.position;
            m = new Vector3(m.x, m.y, 20);
            Vector3 p = cam.ScreenToWorldPoint(m);

            if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Began && hasPlaced == false)
            {
                if (currentBuilding != null && !hasPlaced)
                {
                    currentBuilding.position = new Vector3(p.x, 0, p.z);
                }
            }
            else if (touch.phase == TouchPhase.Ended)
            {
                if (IsValidPosition())
                {
                    hasPlaced = true;
                }
            }
            //TODO: Touch on screen outside building and dismiss GUI
            else if (touch.phase == TouchPhase.Began && hasPlaced == true)
            {
                RaycastHit hit = new RaycastHit();
                Ray        ray = new Ray(new Vector3(p.x, 8, p.z), Vector3.down);
                if (Physics.Raycast(ray, out hit, Mathf.Infinity, buildingMask))
                {
                    if (placeableBuildingOld != null)
                    {
                        placeableBuildingOld.SetSelected(false);
                    }
                    hit.collider.gameObject.GetComponent <PlaceableBuilding>().SetSelected(true);
                    placeableBuildingOld = hit.collider.gameObject.GetComponent <PlaceableBuilding>();
                }
                else
                {
                    if (placeableBuildingOld != null)
                    {
                        placeableBuildingOld.SetSelected(false);
                    }
                }
            }
        }
    }
 public void manipulateTree(Vector3 p)
 {
     if (Input.GetMouseButtonDown(0))
     {
         RaycastHit hit = new RaycastHit();
         Ray        ray = new Ray(new Vector3(p.x, 8, p.z), Vector3.down);
         buildingsMask = LayerMask.GetMask("Building");
         if (Physics.Raycast(ray, out hit, Mathf.Infinity, buildingsMask))
         {
             if (placeableBuildingOld != null)
             {
                 placeableBuildingOld.SetSelected(false);
             }
             if (gameState.currentEvent == 2)
             {
                 createdbuildings.Remove(hit.collider.gameObject);
                 Destroy(hit.collider.gameObject);
             }
             else if (gameState.currentEvent == 3)
             {
                 // Debug.Log(createdbuildings.Find(x => x == hit.collider.gameObject));
                 hasPlaced       = false;
                 currentBuilding = hit.collider.gameObject.transform;
             }
             placeableBuildingOld = null;
         }
     }
 }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("click");
            RaycastHit hit = new RaycastHit();

            var p   = GetPosition();
            Ray ray = new Ray(new Vector3(p.x, 8, p.z), Vector3.down);
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, buildingsMask))
            {
                hit.collider.gameObject.GetComponent <PlaceableBuilding>().SetSelected(true);
                placeableBuildingOld = hit.collider.gameObject.GetComponent <PlaceableBuilding>();
            }
            else
            {
                if (placeableBuildingOld != null)
                {
                    placeableBuildingOld.SetSelected(false);
                }
            }

            return;
        }

        var position = Vector3.zero;

        if (currentBuilding != null)
        {
            position = currentBuilding.position;
        }

        if (Input.GetMouseButton(0))
        {
            Vector3 p = GetPosition();
            position = new Vector3(p.x, 0, p.z);
        }

        if (currentBuilding != null && !hasPlaced)
        {
            currentBuilding.position = position;

            if (Input.GetMouseButtonUp(0))
            {
                if (IsLegalPosition() == false)
                {
                    FsmVariables.GlobalVariables.GetFsmInt(placeableBuilding.Resource).Value += placeableBuilding.Amount;

                    GameObject.Destroy(currentBuilding.gameObject);
                }

                hasPlaced         = false;
                currentBuilding   = null;
                placeableBuilding = null;
            }
        }
    }
示例#4
0
    public GameObject SetItem(GameObject b)
    {
        if (currentBuilding != null)
        {
            Destroy(currentBuilding.gameObject);
            if (manager != null)
            {
                manager.Refund();
            }
        }
        hasPlaced = false;
        Debug.Log("Set Item");
        GameObject res = (GameObject)Instantiate(b);

        currentBuilding = res.transform;
        //		GameObject go = transform.Find ("BuildingFootprint").gameObject;
        //		placeableBuilding = (PlaceableBuilding)go;
        placeableBuilding = currentBuilding.GetComponentsInChildren <PlaceableBuilding>()[0];
        placeableBuilding.SetSelected(true);
        return(res);
    }
 public GameObject SetItem(GameObject b)
 {
     if (currentBuilding != null) {
         Destroy (currentBuilding.gameObject);
         if (manager != null) {
             manager.Refund ();
         }
     }
     hasPlaced = false;
     Debug.Log ("Set Item");
     GameObject res = (GameObject)Instantiate(b);
     currentBuilding = res.transform;
     //		GameObject go = transform.Find ("BuildingFootprint").gameObject;
     //		placeableBuilding = (PlaceableBuilding)go;
     placeableBuilding = currentBuilding.GetComponentsInChildren<PlaceableBuilding>()[0];
     placeableBuilding.SetSelected (true);
     return res;
 }
示例#6
0
    // Update is called once per frame
    void Update()
    {
        Camera  mainCam = GetComponent <Camera> ();
        Vector3 m       = Input.mousePosition;
        //		m = new Vector3(m.x,m.y,transform.position.y);
        //		Vector3 p = mainCam.ScreenToWorldPoint(m);

        Ray   ray = mainCam.ScreenPointToRay(m);
        Plane xz  = new Plane(Vector3.up, new Vector3(0, 0.5f, 0));
        float distance;

        xz.Raycast(ray, out distance);
        Vector3 p = ray.GetPoint(distance);


        //		Vector3 p = new Vector3 (0, 0, 0);
        //		Ray ray = mainCam.ScreenPointToRay (m);
        //		if (Physics.Raycast (ray, out hit, Mathf.Infinity, groundMask)) {
        //			m = new Vector3(m.x,m.y,hit.distance);
        //			p = mainCam.ScreenToWorldPoint(m);
        //		}


        if (currentBuilding != null && !hasPlaced)
        {
            currentBuilding.position = new Vector3(p.x, p.y, p.z);

            if (Input.GetMouseButtonDown(0))
            {
                if (IsLegalPosition(p))
                {
                    Debug.Log("Placing Building");
                    hasPlaced = true;
                    //					placeableBuilding.SetSelected (false);
                    placeableBuilding.SetPlaced(true);
                    placeableBuildingOld = placeableBuilding;
                    currentBuilding      = null;
                }
            }
            else if (Input.GetMouseButtonDown(1))
            {
                Destroy(currentBuilding.gameObject);
                if (manager != null)
                {
                    manager.Refund();
                }
                currentBuilding = null;
            }
        }
        else
        {
            if (Input.GetMouseButtonDown(0))
            {
                // hasPlaced = true;
                RaycastHit hit = new RaycastHit();
                ray = new Ray(new Vector3(p.x, 10, p.z), Vector3.down);
                if (Physics.Raycast(ray, out hit, Mathf.Infinity, buildingsMask))
                {
                    if (placeableBuildingOld != null)
                    {
                        placeableBuildingOld.transform.parent.FindChild("Turret").GetComponent <CannonFireAOE> ().SetPlayerTarget(null);
                        placeableBuildingOld.SetSelected(false);
                    }
                    hit.collider.gameObject.GetComponent <PlaceableBuilding>().SetSelected(true);
                    placeableBuildingOld = hit.collider.gameObject.GetComponent <PlaceableBuilding>();
                }
                else
                {
                    if (placeableBuildingOld != null)
                    {
                        RaycastHit unitHit = new RaycastHit();
                        ray = new Ray(new Vector3(p.x, 10, p.z), Vector3.down);
                        if (Physics.Raycast(ray, out unitHit, Mathf.Infinity, unitMask))
                        {
                            if (unitHit.collider is BoxCollider)
                            {
                                Unit u = unitHit.collider.gameObject.GetComponent <Unit> ();
                                placeableBuildingOld.transform.parent.FindChild("Turret").GetComponent <CannonFireAOE> ().SetPlayerTarget(u);
                            }
                        }
                        else
                        {
                            placeableBuildingOld.transform.parent.FindChild("Turret").GetComponent <CannonFireAOE> ().SetPlayerTarget(null);
                            placeableBuildingOld.SetSelected(false);
//							placeableBuildingOld = null;
                        }
                    }
                }
            }
        }
    }    //Update()