private void BuildBuilding(BaseObject target)
    {
        AICamp  camp         = GameObject.Find("AIHandler").GetComponent <AICamp>();
        Vector3 campLocation = camp.GetCampLocation();
        float   campRadius   = camp.GetCampRadius();


        Vector3      possibleBuildingPlacement = GetRandomPositionInsideCamp(campLocation, campRadius);
        BaseBuilding newObject = Instantiate <BaseObject>(target, possibleBuildingPlacement, Quaternion.Euler(Vector3.zero)) as BaseBuilding;

        //Try to place object around camp max 10 times.
        for (int i = 0; i < 10 && BuildingPlacer.HitsObstacle(possibleBuildingPlacement, newObject.transform); i++)
        {
            possibleBuildingPlacement = GetRandomPositionInsideCamp(campLocation, campRadius);
        }


        if (!BuildingPlacer.HitsObstacle(possibleBuildingPlacement, newObject.transform))
        {
            possibleBuildingPlacement.y += newObject.GetComponentInChildren <Collider>().bounds.size.y;
            //possibleBuildingPlacement.y += newObject.transform.position.y; //Offset the building to be above ground
            newObject.SetPlayer(player);
            newObject.AddLifecycleListener(this);
            newObject.OnCreated();
        }
        else
        {
            Debug.LogError("A placement was not found");
            newObject.RemoveObject();
        }
    }
Esempio n. 2
0
    void OnGUI()
    {
        if (placementObject != null)
        {
            if (HitsObstacle(placementObject.transform.position, placementObject.transform))
            {
                placementObject.GetComponent <PlacementEffect>().ApplyInvalidEffect();
            }
            else

            {
                placementObject.GetComponent <PlacementEffect>().ApplyValidEffect();
            }


            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            int        layerMask = ((1 << LayerMask.NameToLayer("Ground")));
            if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask))
            {
                var finalPosition = GetNearestPointOnGrid(hitInfo.point);



                float objectHeight = placementObject.GetComponentInChildren <Renderer>().bounds.size.y;
                finalPosition.y += objectHeight / 2;
                placementObject.transform.position = finalPosition;
            }
        }
    }