Пример #1
0
    /*
     * building has been pressed
     */
    private void pressedHandler(object sender, EventArgs e)
    {
        //are we choosing building at the beginning of the game?
        if (state == BuildingState.Menu)
        {
        origin = Instantiate (gameObject) as GameObject;
        origin.transform.position = gameObject.transform.position;
        origin.transform.localScale = new Vector3(1,1,1);
        origin.transform.parent = gameObject.transform.parent;
        origin.SetActive (false);
        gameObject.transform.parent = null;
        } else {
        //check to see that the game is running properly
        var controller = GameObject.FindGameObjectWithTag (Tags.GameController);
        var clock = controller.GetComponent<Clock> ();
        if (clock == null) {
            Debug.LogError ("Oh shit no clock!");
        }
        //building is built
        if (state == BuildingState.Built) {
        //create building menu if one doesn't exist
        if (menu == null){
            GameObject obj = Instantiate (menuPrefab) as GameObject;
            obj.transform.position = gameObject.transform.position;
            obj.name = gameObject.name + "Menu";
            obj.transform.parent = gameObject.transform;
            obj.GetComponent<Popup>().setTarget(gameObject);
            menu = obj;
        }
        //if one does exist destroy it
        else {
            Destroy (menu);
            menu = null;
        }
        }
        //if the building is in the pallet
        if (state == BuildingState.Pallet) {
        //create a copy of the building, but disable it's creation methods etc.
        var obj = Instantiate (gameObject) as GameObject;
        obj.name = (gameObject.name);
        obj.GetComponent<Cube> ().enabled = true;
        obj.GetComponent<TapGesture> ().enabled = true;
        obj.transform.parent = gameObject.transform.parent;
        obj.GetComponent<Cube>().setState (BuildingState.Pallet);
        var pos = obj.transform.position;
        pos.z = -3.0f;
        gameObject.transform.position = pos;
        state = BuildingState.Dragging;
        tag = Tags.Placing;

        //spend materials for building
        switch(obj.name){
                case "City":
                    clock.spendMaterial(buildings.City);
                    break;
                case "Factory":
                    clock.spendMaterial(buildings.Factory);
                    break;
                case "Farm":
                    clock.spendMaterial(buildings.Farm);
                    break;
                case "Fishing":
                    clock.spendMaterial(buildings.Fishing);
                    break;
                case "Lumber":
                    clock.spendMaterial(buildings.Lumber);
                    break;
                case "Mine":
                    clock.spendMaterial(buildings.Mine);
                    break;
                case "Power":
                    clock.spendMaterial(buildings.Power);
                    break;
                case "School":
                    clock.spendMaterial(buildings.School);
                    break;
        }

        //create blueprint for the building
        GameObject newBlueprint = (GameObject)GameObject.Instantiate (blueprintPrefab, gameObject.transform.position, gameObject.transform.rotation);
        blueprint = newBlueprint.GetComponent<Blueprint> ();
        blueprint.setParent (this.gameObject);
        }
        }
    }