示例#1
0
    public BspLeaf(int _x, int _y, int _width, int _height, BspLeaf _parent)
    {
        x        = _x;
        y        = _y;
        width    = _width;
        height   = _height;
        parent   = _parent;
        hasRoom  = false;
        centre   = new Vector3(x + width / 2f, 0, y + height / 2f);
        centre2D = new Vector2(x + width / 2f, y + height / 2f);

        quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
        quad.transform.position   = new Vector3(x + (width * 0.5f), -1f, y + (height * 0.5f));
        quad.transform.localScale = new Vector3(width, height, 1f);
        quad.transform.Rotate(new Vector3(90f, 0, 0));
        quad.GetComponent <Renderer> ().material.color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 0.25f);
        quad.gameObject.name             = x + " - " + y;
        quad.gameObject.transform.parent = GameObject.Find("debugQuads").transform;

        // Debug.DrawLine (new Vector3 (x + (width * 0.5f), -1f, y + (height * 0.5f)), Vector3.zero, Color.red, 100f);

        if (parent != null)
        {
            // Debug.DrawLine (centre, parent.centre, Color.red, 100f);
            Corridor corridor = new Corridor(new Vector2(Mathf.Abs(centre2D.x - parent.centre2D.x), Mathf.Abs(centre2D.y - parent.centre2D.y)), (centre2D + parent.centre2D) / 2f, 1f);
        }
    }
示例#2
0
    public bool Split()
    {
        // If this leaf already has children, skip it
        if (firstChild != null || secondChild != null)
        {
            return(false);
        }

        // 50:50 chance of splitting leaf horizontally
        bool splitH = Random.Range(0f, 1f) < 0.5f;

        // If the width is >25% larger than height, we split vertically
        // If the height is >25% larger than the width, we split horizontally
        if (width > height && width / height >= 1.25f)
        {
            splitH = false;
        }
        else if (height > width && height / width >= 1.25f)
        {
            splitH = true;
        }

        // Determine the max height/width of child leaf
        int max = (splitH ? height : width) - MIN_LEAF_SIZE;

        // If we can't generate a large enough room, break
        if (max <= MIN_LEAF_SIZE)
        {
            return(false);
        }

        // Generate split
        int split = Random.Range(MIN_LEAF_SIZE, max);

        if (splitH)
        {
            firstChild  = new BspLeaf(x, y, width, split, this);
            secondChild = new BspLeaf(x, y + split, width, height - split, this);
        }
        else
        {
            firstChild  = new BspLeaf(x, y, split, height, this);
            secondChild = new BspLeaf(x + split, y, width - split, height, this);
        }


        if (quad != null)
        {
            GameObject.Destroy(quad);
            quad = null;
        }

        // We've successfully split the leaf
        return(true);
    }
示例#3
0
        private Collision CheckLeaf(int leafIdx, Point3D start, Vector movement, float startFraction, float endFraction, float sphere)
        {
            BspLeaf   leaf = Leaves[leafIdx];
            Collision c    = null;

            for (int i = 0; i < leaf.numMarkFaces; i++)
            {
                c = ClosestCollision(c, CheckFace(MarkFaces[leaf.firstMarkFace + i], start, movement, startFraction, endFraction, sphere));
            }
            return(c);
        }
示例#4
0
        public static BspLeaf ReadBspLeaf(this BinaryReader br)
        {
            var result = new BspLeaf();

            result.contents         = br.ReadInt32();
            result.pvs              = br.ReadInt32();
            br.BaseStream.Position += 12;
            result.firstMarkFace    = br.ReadUInt16();
            result.numMarkFaces     = br.ReadUInt16();
            br.BaseStream.Position += 4;

            return(result);
        }
示例#5
0
        public Room(Vector2 _size, Vector2 _pos, BspLeaf _parent)
        {
            size   = _size;
            pos    = _pos;
            parent = _parent;

            GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);

            quad.transform.position   = new Vector3(pos.x, 0, pos.y);
            quad.transform.localScale = new Vector3(size.x, size.y, 1f);
            quad.transform.Rotate(new Vector3(90f, 0f, 0f));

            quad.GetComponent <Renderer> ().material.color = Color.white;
            quad.gameObject.name             = "Room";
            quad.gameObject.transform.parent = GameObject.Find("bspTree").transform;
        }
示例#6
0
    private void CreateRooms()
    {
        root = new BspLeaf(0, 0, (int)treeSize.x, (int)treeSize.y, null);
        leaves.Add(root);

        bool didSplit = true;

        while (didSplit)
        {
            didSplit = false;

            for (int i = 0; i < leaves.Count; i++)
            {
                // If we haven't already split this leaf
                if (leaves [i].firstChild == null && leaves [i].secondChild == null)
                {
                    // Attempt to split it
                    if (leaves [i].Split())
                    {
                        // If successful, add it's children to the list of leaves
                        leaves.Add(leaves [i].firstChild);
                        leaves.Add(leaves [i].secondChild);
                        didSplit = true;
                    }
                }
            }
        }

        root.CreateRooms();
        GameObject baseQuad = GameObject.CreatePrimitive(PrimitiveType.Quad);

        baseQuad.transform.position   = new Vector3(treeSize.x / 2f, -2f, treeSize.y / 2f);
        baseQuad.transform.localScale = new Vector3(treeSize.x, treeSize.y, 0f);
        baseQuad.transform.Rotate(new Vector3(90f, 0f, 0f));
        baseQuad.GetComponent <Renderer> ().material.color = Color.black;
    }