Exemple #1
0
    public void generateGrid()
    {
        Debug.Log("generate called");
        if (mNodeList != null && mNodeList.Count > 0)
        {
            Debug.Log("node list exists and isn't 0");
            reset();
        }
        mLevelSpecs = gs.GetLevelSpecs();
        //set up the dimensions of the level
        int numNodes = mLevelSpecs.getNumNodes();
        //numNodes = number of nodes in the level total
        float aspectRatio = mLevelSpecs.getAspectRatio();

        mLength = (int)Mathf.Floor(Mathf.Sqrt(aspectRatio * (float)numNodes));
        mWidth  = (int)(numNodes / mLength);
        int tempID = 0;

        for (float z = 0 - Mathf.Floor(mWidth / 2.0f); z < Mathf.Ceil(mWidth / 2.0f); z += 1.0f)
        {
            for (float x = 0 - Mathf.Floor(mLength / 2.0f); x < Mathf.Ceil(mLength / 2.0f); x += 1.0f)
            {
                NodeType tempType;
                //based on user-defined wall-floor ratio, make the walls more dense or more sparse
                int ratioCheck = rand.Next(0, 100);
                if (ratioCheck <= (int)(100 * mLevelSpecs.getWallFloorRatio()))
                {
                    tempType = NodeType.WALL;
                }
                else
                {
                    tempType = NodeType.FLOOR;
                }

                //create nodes at each incremental position, with a random type
                Node tempNode = new Node(tempID, new Vector3(z, 0.0f, x), tempType);
                if (tempNode.getType() == NodeType.WALL)
                {
                    numWalls++;
                }
                else if (tempNode.getType() == NodeType.FLOOR)
                {
                    numFloors++;
                }
                //Debug.Log(tempID + tempNode.getType());
                //Debug.Log(tempID);
                mNodeList.Add(tempID, tempNode);
                tempID++;
            }
        }

        for (int i = 0; i < mNodeList.Count; i++)
        {
            float tempX = mNodeList[i].getPosition().x;
            float tempZ = mNodeList[i].getPosition().z;

            for (int j = 0; j < mNodeList.Count; j++)
            {
                float xDiff = Mathf.Abs(mNodeList[j].getPosition().x - tempX);
                float zDiff = Mathf.Abs(mNodeList[j].getPosition().z - tempZ);
                //if the x or z distance is exactly 1.0 (the size of a node) away, it is adjacent
                if (xDiff == 1.0f && zDiff == 0.0f || xDiff == 0.0f && zDiff == 1.0f)
                {
                    Debug.Log("it thinks that " + mNodeList[i].getID() + " (" + tempX + ", " + tempZ + ")[" + mNodeList[i].getType() + "] & " + mNodeList[j].getID() + " (" + mNodeList[j].getPosition().x + ", " + mNodeList[j].getPosition().z + ")[" + mNodeList[j].getType() + "] are adajcent");
                    //since adjacency is a mutual relationship, add i to j and j to i.
                    //duplicates will be rejected by the addAdjacenNode method
                    mNodeList[i].addAdjacentNode(mNodeList[j]);
                    mNodeList[j].addAdjacentNode(mNodeList[i]);
                }
            }
        }

        checkConstraints();
        foreach (KeyValuePair <int, Node> node in mNodeList)
        {
            gs.instantiateNode(node.Value);
        }
    }