Example #1
0
    /// <summary>
    /// evaluate segment for potential building placement
    /// </summary>
    /// <param name="segment"></param>
    private void checkSegment(RoadSegment segment)
    {
        Vector2 start    = segment.PointA.point;
        Vector2 end      = segment.PointB.point;
        Vector2 dir      = (end - start).normalized;
        float   distance = Vector2.Distance(start, end);

        Vector2 current = start;
        bool    side    = true;

        for (float f = RoadRenderer.RoadWidth; f < distance || side; f += 4.5f)
        {
            //switch side of the road
            if (f > distance && side)
            {
                side = false;
                f    = RoadRenderer.RoadWidth;
            }

            Vector2 per = new Vector2(-dir.y, dir.x);
            if (side)
            {
                per *= -1;
            }

            //try to put some building into the spot
            for (int i = 0; i < 10; i++)
            {
                //get road level adjustment
                float level = 2.0f - (segment.Level / 3f);                //0,0.33,0.66,1

                //get building dimensions
                float width  = Random.Range(1.75f, 2f) * level;
                float length = Random.Range(1.75f, 2f) * level;
                float height = Random.Range(2.5f, 10f) * level;

                //get building center
                Vector2 roadOffset = per.normalized * (RoadRenderer.RoadWidth * 1.25f + length);
                Vector2 tc         = start + (dir * f) + roadOffset;

                if (f - width < 0 || f + width > distance)
                {
                    continue;
                }

                Vector3 center = new Vector3(tc.x, 0, tc.y);

                //get building size
                Vector3 size = new Vector3(length, width, height);

                //set building
                GameObject buildingObj = GameObject.Instantiate(this.BuildingStatic);
                buildingObj.transform.parent = this.Instances.transform;
                buildingObj.transform.name   = "building_" + this.BuildingsList.Count.ToString("D5");

                Building building = new Building(center, size, this.GetRotation(dir) - (side ? 180 : 0));
                building.AddMyGameObject(buildingObj);
                this.AddBuildingMesh(building);
                building.AddCollider();

                if (this.CheckValidPlacement(building))
                {
                    this.BuildingsList.Add(building);
                    break;
                }
                else
                {
                    GameObject.DestroyImmediate(buildingObj);
                }
            }
        }
    }
Example #2
0
    /// <summary>
    /// evaluate segment for potential building placement
    /// </summary>
    /// <param name="segment"></param>
    private void checkSegment(RoadSegment segment)
    {
        Vector2 start = segment.PointA.point;
        Vector2 end = segment.PointB.point;
        Vector2 dir = (end - start).normalized;
        float distance = Vector2.Distance (start, end);

        Vector2 current = start;
        bool side = true;
        for(float f=RoadRenderer.RoadWidth;f<distance || side;f+=4.5f)
        {
            //switch side of the road
            if(f > distance && side)
            {
                side = false;
                f=RoadRenderer.RoadWidth;
            }

            Vector2 per = new Vector2(-dir.y, dir.x);
            if(side)
                per *=-1;

            //try to put some building into the spot
            for(int i=0;i<10;i++)
            {
                //get road level adjustment
                float level = 2.0f - (segment.Level / 3f);//0,0.33,0.66,1

                //get building dimensions
                float width = Random.Range(1.75f,2f) * level;
                float length = Random.Range(1.75f,2f) * level;
                float height = Random.Range(2.5f,10f) * level;

                //get building center
                Vector2 roadOffset = per.normalized * (RoadRenderer.RoadWidth * 1.25f + length);
                Vector2 tc = start + (dir * f) + roadOffset;

                if(f - width < 0 || f + width > distance)
                    continue;

                Vector3 center = new Vector3(tc.x,0,tc.y);

                //get building size
                Vector3 size = new Vector3(length,width,height);

                //set building
                GameObject buildingObj = GameObject.Instantiate(this.BuildingStatic);
                buildingObj.transform.parent = this.Instances.transform;
                buildingObj.transform.name = "building_" + this.BuildingsList.Count.ToString("D5");

                Building building = new Building(center,size,this.GetRotation(dir) - (side ? 180 : 0));
                building.AddMyGameObject(buildingObj);
                this.AddBuildingMesh(building);
                building.AddCollider();

                if(this.CheckValidPlacement(building))
                {
                    this.BuildingsList.Add(building);
                    break;
                }
                else
                    GameObject.DestroyImmediate(buildingObj);
            }
        }
    }