Beispiel #1
0
 protected void FillEndOfRoad(ForkNode parent, bool forward, bool left, bool right)
 {
     if (forward)
     {
         AbstractPathNode e = RandomEndOfRoad;
         if (e == null || parent.forward == null)
         {
             return;
         }
         AbstractPathNode instance = pool.Instantiate(e, parent.forward);
         instance.transform.position += instance.transform.forward * instance.height / 2;
         instance.transform.SetParent(transform, true);
     }
     if (left)
     {
         AbstractPathNode e = RandomEndOfRoad;
         if (e == null || parent.left == null)
         {
             return;
         }
         AbstractPathNode instance = pool.Instantiate(e, parent.left);
         instance.transform.position += instance.transform.forward * instance.height / 2;
         instance.transform.SetParent(transform, true);
     }
     if (right)
     {
         AbstractPathNode e = RandomEndOfRoad;
         if (e == null || parent.right == null)
         {
             return;
         }
         AbstractPathNode instance = pool.Instantiate(e, parent.right);
         instance.transform.position += instance.transform.forward * instance.height / 2;
         instance.transform.SetParent(transform, true);
     }
 }
Beispiel #2
0
        public void Generate()
        {
            bool fork = Nodes.Count == 0;

            if (Nodes.Count > 0)
            {
                for (int i = Nodes.Count - 1; i >= 0; i--)
                {
                    if (Nodes[i] is ForkNode f)
                    {
                        fork = true;
                        switch (way)
                        {
                        default:
                        case Way.Forward:
                            bool r = Random.Range(0f, 1f) >= 0.5;
                            way = r ? Way.Right : Way.Left;
                            float offsetX = Nodes[Nodes.Count - 1].width / 2 * (r ? 1 : -1);
                            float offsetY = -Nodes[Nodes.Count - 1].height / 2;
                            o += new Vector3(offsetX, 0, offsetY);
                            FillEndOfRoad(f, true, r, !r);
                            break;

                        case Way.Left:
                            way = Way.Forward;
                            o  += new Vector3(Nodes[Nodes.Count - 1].height / 2, 0, Nodes[Nodes.Count - 1].width / 2);
                            FillEndOfRoad(f, true, true, false);
                            break;

                        case Way.Right:
                            way = Way.Forward;
                            o  += new Vector3(-Nodes[Nodes.Count - 1].height / 2, 0, Nodes[Nodes.Count - 1].width / 2);
                            FillEndOfRoad(f, true, false, true);
                            break;
                        }
                    }
                    if (Nodes[i] is PathNode)
                    {
                        break;
                    }
                }
            }
            int count          = 0;
            AbstractPathNode t = RandomRoadNode;

            while (fork && t is ForkNode)
            {
                t = RandomRoadNode;
                count++;
                if (count > templates.Length && count > 2000)
                {
                    Debug.LogError("Can't find a road!");
                    break;
                }
            }

            AbstractPathNode node = pool.Instantiate(t, transform);

            if (allowMirrors && Random.Range(0f, 1f) >= 0.5f && (node is PathNode))
            {
                node.transform.localScale = new Vector3(-node.transform.localScale.x, node.transform.localScale.y, node.transform.localScale.z);
            }
            D.Value += node.height;
            switch (way)
            {
            default:
            case Way.Forward:
                o += new Vector3(0, 0, node.height / 2);
                node.transform.position = transform.position + o;
                o += new Vector3(0, 0, node.height / 2);
                break;

            case Way.Left:
                node.transform.Rotate(Vector3.up * -90);

                o -= new Vector3(node.height / 2, 0, 0);
                node.transform.position = transform.position + o;
                o -= new Vector3(node.height / 2, 0, 0);
                break;

            case Way.Right:
                node.transform.Rotate(Vector3.up * 90);

                o += new Vector3(node.height / 2, 0, 0);
                node.transform.position = transform.position + o;
                o += new Vector3(node.height / 2, 0, 0);
                break;
            }
            if (node is PathNode)
            {
                int   len  = (int)function.Evaluate(D.Value);
                float unit = node.height / len;
                for (int i = 0; i < len; i++)
                {
                    AbstractPathNode temp = RandomObjects;

                    Renderer         renderer = temp.GetComponent <Renderer>();
                    float            width    = renderer == null ? temp.width : renderer.bounds.max.x - renderer.bounds.min.x;
                    AbstractPathNode ins      = pool.Instantiate(temp, node.transform);
                    ins.transform.position += node.transform.forward * (-node.height / 2 + i * unit) +
                                              node.transform.right * (-node.width / 2 + Random.Range(width, node.width - width));
                    ins.transform.SetParent(transform);
                }
            }
        }