Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1))
        {
            //var uimodetextgo = GameObject.FindGameObjectsWithTag("UI").FirstOrDefault(f => f.gameObject.name == "UI_ModeText");
            //UIBuildMode ubmscript = uimodetextgo.GetComponent<UIBuildMode>();
            //ubmscript.ToggleBuildMode();
            gm.SetBuildMode(!gm.buildMode);
        }
        if (Input.GetKeyDown(KeyCode.F2))
        {
            if (gm.selectedBuildingIndex > 0)
            {
                CursorObjectSwap cos = GameObject.FindGameObjectWithTag("Cursor").GetComponent <CursorObjectSwap>();
                cos.SetAllFalse();
                gm.selectedBuildingIndex--;
                //var uimodetextgo = GameObject.FindGameObjectsWithTag("UI").FirstOrDefault(f => f.gameObject.name == "UI_ModeText");
                //UIBuildMode ubmscript = uimodetextgo.GetComponent<UIBuildMode>();
                //ubmscript.SetBuildModeText();
            }
        }
        if (Input.GetKeyDown(KeyCode.F3))
        {
            if (gm.selectedBuildingIndex + 1 < gm.buildingArray.Length)
            {
                CursorObjectSwap cos = GameObject.FindGameObjectWithTag("Cursor").GetComponent <CursorObjectSwap>();
                cos.SetAllFalse();
                gm.selectedBuildingIndex++;
                //var uimodetextgo = GameObject.FindGameObjectsWithTag("UI").FirstOrDefault(f => f.gameObject.name == "UI_ModeText");
                //UIBuildMode ubmscript = uimodetextgo.GetComponent<UIBuildMode>();
                //ubmscript.SetBuildModeText();
            }
        }
        // Building mode
        if (Input.GetMouseButtonDown(0) && gm.buildMode && gm.canBuild)
        {
            BuildingBuilder builder = GameObject.FindGameObjectWithTag("Builder").GetComponent <BuildingBuilder>();
            builder.BuildBuilding(GameObject.FindGameObjectWithTag("Cursor").transform.position);
        }
        // Selection mode
        else if (Input.GetMouseButtonDown(0) && !gm.buildMode)
        {
            var        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit = new RaycastHit();

            if (Physics.Raycast(ray, out hit))
            {
                // If building is hit
                if (hit.collider.gameObject.tag == "Building_hitboxable")
                {
                    GameObject prevGo = null;
                    // If connecting, check if previous building exists basically
                    if (gm.connectButtonToggled && gm.selectedBuilding != null)
                    {
                        prevGo = gm.selectedBuilding;
                    }

                    BuildingScript bs = hit.collider.gameObject.GetComponent <BuildingScript>();
                    bs.Select(hit.collider.gameObject);
                    // Set selected building particle system
                    gm.SetParticleActive(true);

                    // Try connecting two buildings
                    if (prevGo != null && gm.selectedBuilding != null && prevGo != gm.selectedBuilding)
                    {
                        Debug.Log("Selection happened between " + prevGo.name + " :: " + gm.selectedBuilding.name);

                        var cs  = prevGo.GetComponent <BuildingConnections>();
                        var cs2 = gm.selectedBuilding.GetComponent <BuildingConnections>();

                        // Check if connection can be made between two buildings
                        if (cs.ConnectionCanBeAdded(cs2.connectionType) && cs2.ConnectionCanBeAdded(cs.connectionType))
                        {
                            cs.connections.Add(gm.selectedBuilding);

                            cs2.connections.Add(prevGo);

                            var cable  = Instantiate(gm.powerCablePrefab, prevGo.transform.position, gm.powerCablePrefab.transform.rotation);
                            var cableS = cable.GetComponent <PowerCablePath>();
                            cableS.SetPowerCable(prevGo.transform.position, gm.selectedBuilding.transform.position);

                            cs.createdCables.Add(cable);
                            cs2.createdCables.Add(cable);

                            gm.connectButtonToggled = false;
                        }
                        else
                        {
                            Debug.Log("Cannot create connection");
                        }
                    }
                }
            }
        }
    }