Example #1
0
 void Start()
 {
     cam       = Camera.main;
     rayHelper = new RaycastHelper();
     state     = States.Free;
     UIHolder.gameObject.SetActive(false);
     buildSel = BuildingSel.Nothing;
     #if !UNITY_EDITOR
     fingerID = 0;
     #endif
 }
Example #2
0
 void SetFree()
 {
     //free the user by reseting the state and returning the ghost object to this object place
     state    = States.Free;
     buildSel = BuildingSel.Nothing;
     if (Building_ghost[selectedBuilding].activeSelf)
     {
         Building_ghost[selectedBuilding].SetActive(false);
     }
     Building_ghost[selectedBuilding].transform.position = transform.position;
     isConstructing = false;
 }
Example #3
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            isConstructing   = true;
            buildSel         = BuildingSel.TownCenter;
            selectedBuilding = (int)BuildingSel.TownCenter;
        }
        else if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (buildSel != BuildingSel.Nothing)
            {
                SetFree();
            }
            if (units.Count > 0)
            {
                UnSelectUnit();
            }
        }
        #region one one unit selection
        if (Input.GetMouseButtonDown(0) && Input.GetKey(KeyCode.LeftShift))
        {
            GameObject unit = rayHelper.RayTo(selectableUnitMask);
            if (unit != null)
            {
                units.Add(unit.GetComponent <Unit>());
            }
            else
            {
                UnSelectUnit();
            }

            Debug.Log(unit);

            unit = null;
        }
        else if (Input.GetMouseButtonDown(0))
        {
            //select a unit this includes: buildings, villagers, warriors
            startPos = Input.mousePosition;

            if (!isConstructing)
            {
                //selecting a unit
                GameObject unit = rayHelper.RayTo(selectableUnitMask, tileMask);
                if (unit != null)
                {
                    if (unit.tag == "Tile")
                    {
                        UnSelectUnit();
                        return;
                    }
                    Unit u = unit.GetComponent <Unit>();
                    UIHolder.gameObject.SetActive(true);
                    SetOffUI();
                    units.Clear();
                    canvas.nameText.text = u.unitName;
                    switch (unit.gameObject.tag)
                    {
                    case "Villager":
                        state = States.HasVillager;
                        units.Add(u);
                        break;

                    case "TownCenter":
                        state = States.HasTownCenter;
                        TownCenterOptions.gameObject.SetActive(true);
                        units.Add(u);
                        break;

                    default:
                        state = States.Free;
                        break;
                    }
                }
            }
        }
        else if (Input.GetMouseButtonDown(1))
        {
            //make an action with villager, warrior or building
            //get villager properties
            if (units.Count > 0)
            {
                foreach (Unit u in units)
                {
                    if (u.GetType() == typeof(Puddly))    //ask if is a puddly
                    {
                        u.gameObject.GetComponent <Puddly>().MoveDestination(rayHelper.RayTo(tileMask, this.transform));
                    }
                }
            }
        }
        #endregion

        #region multiple selectiom
        //end box selection
        if (Input.GetMouseButtonUp(0))
        {
            if (!isConstructing)
            {
                ReleaseSelectionBox();
            }
        }

        //start box selection
        if (Input.GetMouseButton(0))
        {
            if (!Input.GetKey(KeyCode.LeftShift))
            {
                UpdateSelectionBox(Input.mousePosition);
            }
        }
        #endregion


        if (buildSel == BuildingSel.TownCenter)
        {
            hoverTile();
        }
    }