Esempio n. 1
0
 void Start()
 {
     buildCursor = transform.Find("BuildCursor");
     cursor      = buildCursor.GetComponent <BuildCursor>();
     Debug.Assert(cursor != null, "CURSOR IS NULL");
     buildCursor.gameObject.SetActive(false);
 }
        /// <summary>
        /// Is called when player is in build-mode
        /// but doesn't click on map but on a button instead.
        /// </summary>
        private void ClickOnButtonInsteadOfMap()
        {
            Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
            activeCursor = BuildCursor.NONE;

            cursorIcon_Pipe.SetActive(false);
            cursorIcon_Filter.SetActive(false);

            UIManager.UICamera.cullingMask = lastLayerMask;

            GameManager.Instance.Event("TIMELINE",
                                       "SIM",
                                       "Unpause");
        }
        //------------------------------------------------------------------------------
        /// <summary>
        /// Build a construction.
        /// This function is called when the user clicks
        /// on the "BUILD" button in the build selection panel.
        /// The function will change the icon to the selected
        /// consturction model and will hide the build panels.
        /// Parameter is the name of the button.
        /// </summary>
        public void BuildConstruction(string param)
        {
            activeMenu = SubMenu.NONE;
            buildMenu.SetActive(false);
            buildInfoPanel.SetActive(false);
            buildButton.Up();
            txtBuildMenu.FontColor = new Color32(136, 137, 137, 255);

            lastLayerMask = UIManager.UICamera.cullingMask;
            UIManager.UICamera.cullingMask =  1 << LayerMask.NameToLayer("Map")
                                            | 1 << LayerMask.NameToLayer("Menu");

            string textureName ="";
            if(param == "btn_Build_1")
            {
                //textureName = "cursor_PipeSystem_50px";
                cursorIcon_Pipe.SetActive(true);
                cursorIcon_Pipe.transform.position = UIManager.UICamera.ScreenToWorldPoint(Input.mousePosition);
                activeCursor = BuildCursor.PIPE;
            }
            else if (param == "btn_Build_2")
            {
                //textureName = "cursor_SmallFilterPlant_50px";
                cursorIcon_Filter.SetActive(true);
                cursorIcon_Filter.transform.position = UIManager.UICamera.ScreenToWorldPoint(Input.mousePosition);
                activeCursor = BuildCursor.FILTER;
            }

            textureName = "Game/Sprites/HUD/symb_Cursor_Build"; //+ textureName;

            Log.Info("textureName:"+textureName);
            Vector2 hotSpot = Vector2.zero;
            Texture2D cursorTexture = Resources.Load<Texture2D>(textureName);
            Cursor.SetCursor(cursorTexture, hotSpot, CursorMode.Auto);
            mouseDown = true;

            //TODO remove this once Map draws on layer MAP only!
            //UIManager.ShowLayer("Default", true);
        }
        private IEnumerator BuildComfirmation(Vector3 point)
        {
            while (buildConfirmation == 0)
            {
                yield return new WaitForSeconds(0.1f);
                //WebPlayerDebugManager.addOutput("waiting", 1);
            }
            //0: not decided, 1: yes, 2: no
            if (buildConfirmation == 1)
            {
                //WebPlayerDebugManager.addOutput("yes", 1);
                string buildItem =
                    (activeCursor  == BuildCursor.PIPE) ? "PIPE" : "FILTER";

                if (activeCursor == BuildCursor.FILTER)
                {
                    GameManager.Instance.Event("SIMULATION",
                                               "CONSTRUCTION",
                                               "E0006");
                }
                else if (activeCursor == BuildCursor.PIPE)
                {
                    GameManager.Instance.Event("SIMULATION",
                                               "CONSTRUCTION",
                                               "E0001");
                }
                Log.Info(
                    string.Format(
                    "BUILD {0} ON MAP AT POSITON: {1}",
                    buildItem, point));
                activeCursor = BuildCursor.NONE;

                //notify the world about building
                if(buildFunction != null)
                    buildFunction(point, buildItem);

                cursorIcon_Pipe.SetActive(false);
                cursorIcon_Filter.SetActive(false);
                //UIManager.ShowLayer("Player", true);
                UIManager.UICamera.cullingMask = lastLayerMask;

                GameManager.Instance.Event("TIMELINE",
                                           "SIM",
                                           "Unpause");
                yield return true;
            }
            else if (buildConfirmation == 2)
            {
                //WebPlayerDebugManager.addOutput("no", 1);

                activeCursor = BuildCursor.NONE;
                cursorIcon_Pipe.SetActive(false);
                cursorIcon_Filter.SetActive(false);
                //UIManager.ShowLayer("Player", true);
                UIManager.UICamera.cullingMask = lastLayerMask;

                GameManager.Instance.Event("TIMELINE",
                                           "SIM",
                                           "Unpause");
                yield return false;
            }
        }
    void UpdateInput()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (mode == Mode.Selection)
            {
                //Unselect all units and plots on left-click, always.
                UnselectAll();
                if (!EventSystem.current.IsPointerOverGameObject())
                {
                    UnselectPlot();
                }

                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out RaycastHit hit))
                {
                    //If a left-click hits a unit, select it.
                    Unit hitUnit = hit.transform.GetComponent <Unit> ();
                    if (hitUnit)
                    {
                        Select(hitUnit);
                    }
                    else
                    {
                        BuildingPlot plot = hit.transform.GetComponent <BuildingPlot> ();
                        if (plot)
                        {
                            SelectPlot(plot);
                        }
                    }
                }
            }
            else
            {
                if (cursor)
                {
                    if (cursor.CanBuild() && cursor.transform.position.y < maxBuildHeight)
                    {
                        Debug.Log(cursor.gameObject.transform.position);
                        GameObject newPlot = Instantiate(buildingPlotPrefab, cursor.transform.position, Quaternion.identity);
                        ToggleMode();
                        ui.SelectBuildingPlot(newPlot.gameObject.GetComponent <BuildingPlot>());
                    }
                }
                else
                {
                    cursor = ui.GetCursor();
                }
            }
        }
        if (Input.GetMouseButtonDown(1) && selected.Count > 0)
        {
            //On a right-click, try to give orders to selected units, if there are any.

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out RaycastHit hit))
            {
                //If a left click hits a unit, select it.
                Targetable hitTarget = hit.transform.gameObject.GetComponent <Targetable> ();

                if (hitTarget)
                {
                    Debug.Log("hit target");
                    //Pass hit object and hit position to every unit, let them figure out what to do with it.
                    foreach (Unit u in selected)
                    {
                        u.Order(hitTarget, hit.point);
                    }
                }
            }
        }

        if (Input.GetAxis("Drop") > 0)
        {
            foreach (Unit u in selected)
            {
                u.DropItems();
            }
            ui.ShowDropText(false);
        }

        if (Input.GetKeyDown(KeyCode.B))
        {
            ToggleMode();
        }
    }