示例#1
0
    void BuildBuilding(GridTile tile)
    {
        // Pick a type of building randomly
        CityObject buildObject = buildings[rand.Next(0, buildings.Length)];


        float diameter = buildObject.prefabs[0].GetComponent <SizeController>().diameter;

        // Rotation quaternion for the building orientation
        Quaternion rotation;

        // List of possible orientations
        float[] possibleOrientations = { 0, 90, 180, 270 };

        // Get an angle from the possible orientation
        float angle = possibleOrientations[rand.Next(0, possibleOrientations.Length)];

        // Make a maximum of 10 degrees offset from the cardinal direction (purely for making it more visibly appealing)
        //angle += (float)rand.NextDouble() * 10;

        // Create the rotation quaternion
        rotation = Quaternion.AngleAxis(angle, Vector3.up);
        if (world.CanBuild(tile.position, diameter, buildObject.prefabs[0], buildObject.scale, rotation, true))
        {
            GameObject parentObj = new GameObject();
            parentObj.transform.parent = transform;
            CityBuildingManager parentController = parentObj.AddComponent <CityBuildingManager>();
            parentController.cityObject = buildObject;
            // Place the building
            GameObject cityObj = world.AddOther(buildObject.prefabs[0], tile.position + Vector3.down * 0.1f, rotation, buildObject.scale, GridTileOccupant.OccupantType.City, parentObj.transform);
            parentController.curObject = cityObj;

            curBuildings.Add(parentController);

            currentPlacedHouses++;
        }
    }
    // Updates the turbine to the mouse cursor until placement is confirmed
    void UpdateSelectedPosition()
    {
        world.ResetTempChunks();

        Vector3    plantPos  = Vector3.zero;                                      // Get zero vector
        GridTile   plantGrid = null;
        Ray        ray       = Camera.main.ScreenPointToRay(Input.mousePosition); // Raycast to find where the mouse is pointing at
        RaycastHit hit;
        bool       canBuild = false;

        Camera.main.GetComponent <CameraController>().SetHaveControl(true);
        if (Physics.Raycast(ray, out hit, Mathf.Infinity, buildMask))
        {
            //plantGrid = GridTile.FindClosestGridTile(hit.point + new Vector3(TerrainController.thisTerrainController.tileSize / 2, 0, TerrainController.thisTerrainController.tileSize / 2)); // Grab the grid where we're hitting
            //plantPos = plantGrid.position; // What is the x,y,z coords?
            plantPos = hit.point;
            if ((!curInstantiated.GetComponent <BuildAttributes>().canRotateAtBuild || (curInstantiated.GetComponent <BuildAttributes>().canRotateAtBuild&& !Input.GetMouseButton(1))))
            {
                curInstantiated.transform.position = plantPos; // We already have a preview turbine, just update it's position to follow the mouse
            }
            if (mouseX * rotateSpeed >= 1 || mouseX * rotateSpeed <= -1)
            {
                curInstantiated.transform.rotation = Quaternion.Euler(curInstantiated.transform.rotation.eulerAngles.x, Mathf.RoundToInt(curInstantiated.transform.rotation.eulerAngles.y + mouseX * rotateSpeed), curInstantiated.transform.rotation.eulerAngles.z);
                mouseX = 0;
            }


            if ((!curInstantiated.GetComponent <BuildAttributes>().canRotateAtBuild || (curInstantiated.GetComponent <BuildAttributes>().canRotateAtBuild&& !Input.GetMouseButton(1))))
            {
                if (world.CanBuild(plantPos, curInstantiated.GetComponent <SizeController>().diameter, curInstantiated, curInstantiated.GetComponent <SizeController>().desiredScale, curInstantiated.transform.rotation, true)) // If we can build here, make the color greenish
                {
                    foreach (Renderer ren in curInstantiated.GetComponentsInChildren <Renderer>())
                    {
                        foreach (Material mat in ren.materials)
                        {
                            mat.shader = blueBuildMaterial.shader;
                            mat.color  = blueBuildMaterial.color;
                        }
                    }
                    plantPos = world.TempEqualTerrain(plantPos, curInstantiated.GetComponent <SizeController>().diameter, curInstantiated.GetComponent <SizeController>().desiredScale);
                    curInstantiated.transform.position = plantPos;

                    canBuild = true;
                }
                else // We can't build here, make the color reddish
                {
                    foreach (Renderer ren in curInstantiated.GetComponentsInChildren <Renderer>())
                    {
                        foreach (Material mat in ren.materials)
                        {
                            mat.shader = redBuildMaterial.shader;
                            mat.color  = redBuildMaterial.color;
                        }
                    }
                    canBuild = false;
                }
            }
        }

        if (curInstantiated.GetComponent <BuildAttributes>().canRotateAtBuild&& Input.GetMouseButton(1))
        {
            mouseX += Input.GetAxis("Mouse X") * Time.deltaTime;
        }
        else if (curInstantiated.GetComponent <BuildAttributes>().canRotateAtBuild&& Input.GetMouseButtonUp(1))
        {
            mouseX = 0;
        }
        if (Input.GetMouseButtonDown(0) && canBuild) // The user clicks and we can build here
        {
            BuildNow(plantGrid, plantPos);           // Run the build function
            infoCamera.enabled = false;
            curSelected        = null;
            inBuildMode        = false;
            instantHere.SetActive(false);
            BuildMenu.Hide();
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            BuildMode2BuildMenu();
        }
    }