コード例 #1
0
ファイル: SpringGrid.cs プロジェクト: jlocas/Lutherie
    private void CreateGroups()
    {
        groupLength = (int)(sideLength / lengthDiv);							//groupLength = how many blocks in 1 block group
        groupsPerSide = (int)(sideLength / groupLength); 						//groupsPerSide = how many block groups per side of the grid
        blockGroups = new SpringGridBlockGroup[groupsPerSide, groupsPerSide];	//create new block groups to which we give blocks later

        int[,] i = new int[lengthDiv, lengthDiv];

        /*
         * 2d array of arrays
         * 2d adress of the group to get an array of blocks it contains
         *
         * for a 12 x 12 grid where lengthDiv = 3:
         *
         * z
         * [][][] 	groups	[0,0][1,0][2,0]	content	[ blocks[4] ][ blocks[4] ][ blocks[4] ]
         * [][][]			[0,1][1,1][2,1]			[ blocks[4] ][ blocks[4] ][ blocks[4] ]
         * [][][]x			[0,2][1,2][2,2]			[ blocks[4] ][ blocks[4] ][ blocks[4] ]
         *
         */

        SpringGridBlock[,][] groupArrays = new SpringGridBlock[groupsPerSide, groupsPerSide][];

        for(int x=0; x < groupsPerSide; x++){
            for(int z=0; z < groupsPerSide; z++){

                //initialize the groupArrays ([,][], contains the blocks for each group)
                groupArrays[x,z] = new SpringGridBlock[groupLength*groupLength];
                //create new block groups to which we give blocks later
                blockGroups[x,z] = new SpringGridBlockGroup();
                blockGroups[x,z].Blocks = new SpringGridBlock[groupLength*groupLength];
            }
        }

        //Assign individual blocks to a group
        for(int x=0; x < sideLength; x++){
            for(int z = 0; z < sideLength; z++){
                //find out which group this block belongs to
                int xGroup = (int)(x / groupLength);
                int zGroup = (int)(z / groupLength);

                //add block[x,z] to the correct group
                groupArrays[xGroup, zGroup][ i[xGroup, zGroup]  ] = blocks[x,z];		//add blocks[x,z] to the groupArray[xg, zg] at a new index
                blocks[x,z].Block.name += " (group " + xGroup + ", " + zGroup + ")";//rename
                blocks[x,z].BlockGroup = new int[2]{x,z};							//tell the block which group it belongs to
                i[xGroup, zGroup]++;
            }
        }

        //Assign group arrays
        for(int x=0; x < groupsPerSide; x++){
            for(int z=0; z < groupsPerSide; z++){
                blockGroups[x,z].Blocks = groupArrays[x,z];
                blockGroups[x,z].Initialize();
            }
        }
    }
コード例 #2
0
ファイル: SpringGrid.cs プロジェクト: jlocas/Lutherie
    private void CreateBlocks()
    {
        GameObject springGridObjects = new GameObject();
        springGridObjects.name = "Spring Grid Objects";

        GameObject blockContainer = new GameObject();
        blockContainer.transform.SetParent(springGridObjects.transform);
        blockContainer.name = "Blocks";

        GameObject anchorContainer = new GameObject();
        anchorContainer.transform.SetParent(springGridObjects.transform);
        anchorContainer.name = "Anchors";

        for(int x = 0; x < sideLength; x++){
            for(int z = 0; z < sideLength; z++){
                //creation of blocks
                blocks[x,z] = new SpringGridBlock();

                GameObject newBlock = Instantiate(blockPrefab, new Vector3(x, 0f, z), Quaternion.identity) as GameObject;
                newBlock.transform.SetParent(blockContainer.transform);
                newBlock.name = string.Format("{0}, {1}",x, z);
                newBlock.GetComponent<BoxCollider>().enabled = blocksCollision;
                blocks[x,z].Block = newBlock;

                //creation of anchors
                GameObject newAnchor = Instantiate(anchorPrefab, new Vector3(x, 0f, z), Quaternion.identity) as GameObject;
                newAnchor.transform.SetParent(anchorContainer.transform);
                newAnchor.name = string.Format("{0}, {1}",x, z);
                newAnchor.transform.Rotate(new Vector3(90f, 0f, 0f));
                blocks[x,z].Anchor = newAnchor;

                //fill the component arrays
                blocks[x,z].Body = blocks[x,z].Block.GetComponent<Rigidbody>();
                blocks[x,z].Collider = blocks[x,z].Block.GetComponent<BoxCollider>();
                blocks[x,z].Springs = new SpringJoint[3];
                blocks[x,z].IsRepositioning = false;
                for(int i = 0; i < 3; i++)
                {
                    blocks[x,z].Springs[i] = blocks[x,z].Block.AddComponent<SpringJoint>();
                }
            }
        }
        anchorContainer.transform.position = new Vector3(0f, anchorHeight, 0f);
    }