Contains Bounds of the building, and the position
Inheritance: Voronoi.Point
        private void CreateBuildingOnRoad(Road road)
        {
            var         cell        = road.ParentCell;
            const int   offset      = 6;
            const float minDistance = 0.5f;

            //Create an offset line of this road towards the inside of the cell
            var offsetLine = road.GenerateOffsetParallelTowardsPoint(offset, cell.SitePoint);

            //calculate total length of the line
            var length   = offsetLine.Length();
            var traveled = minDistance;

            //keep repeating until the end is reached
            while (traveled < length - minDistance)
            {
                //get point on line using normalized values [0,1]
                var pc  = traveled / length;
                var pos = offsetLine.FindRandomPointOnLine(pc, pc);

                //Create a building site from this point
                var bs = BuildingSite.FromPoint(pos);
                bs.ParentRoad = road;
                road.Buildings.Add(bs);


                //travel along the line using the width of the building site
                traveled += (minDistance + bs.Width / 2);
            }
        }
 public static BuildingSite FromPoint(Point p)
 {
     var b = new BuildingSite(p.X, p.Y);
     return b;
 }
Example #3
0
        /// <summary>
        /// Spawn a building on the position
        /// </summary>
        private void SpawnBuilding(Vector3 position, GameObject prefab, BuildingSite building)
        {
            var road = building.ParentRoad;

            //Make the building face the road, make a perpendicular line from the building pos to the road
            var lookAtPos = road.FindPerpendicularPointOnLine(new Point(position.x, position.z)).ToVector3();

            //get correct spawn height from the terrain
            position.y = _terrain.SampleHeight(position);
            lookAtPos.y = position.y;

            //Get bounds of the prefab and store them
            var size = prefab.GetPrefabBounds();
            building.Width = (int)size.x;
            building.Height = (int)size.z;

            //don't spawn when there is a collision
            var colliderPos = position;
            colliderPos.y += size.y / 2;
            if (isOverlapping(prefab, position))
            {
                return;
            }

            //instantiate the building
            var go = (GameObject)Object.Instantiate(prefab, position, prefab.transform.rotation);

            //look at the road
            go.transform.LookAt(lookAtPos);

            //flip on Y
            go.transform.RotateAround(position, go.transform.up, 180);
            go.isStatic = true;
            go.SetParent(_buildingParent);

            building.UserData = go;

            //Remove grass and other details around the object
            //RemoveDetailsAroundGameObject((GameObject)go);

            //Add to the buildings list
            _buildings.Add(building);
        }
        public static BuildingSite FromPoint(Point p)
        {
            var b = new BuildingSite(p.X, p.Y);

            return(b);
        }