void Awake() { //First we will create our map using the parameters we have set createdMap = BlockUtilities.CreateBlockMap(mapName, tileSize, chunkWidth, chunkHeight, growthAxis); //Now we will create a big old block of cubes, according to the dimensions we've set above for (int x = 0; x < levelWidth; x++) { for (int y = 0; y < levelWidth; y++) { for (int z = 0; z < levelDepth; z++) { //We instantiate the block outside of the BlockUtilites library //I've made it this way to allow you to pool GameObjects or anything you like //So you have control over where your blocks come from Block b = GameObject.Instantiate(blockPrefab) as Block; //Now we add the block to the map at the desired coordinates //We won't randomise or refresh at this point, as we're creating a large map //We will do that as one call later BlockUtilities.AddBlockToMap(createdMap, b, false, 0, false, x, y, z, false, true); } } } //Now that our map has been created, we will refresh it //Refreshing your map sets your blocks to orient correctly //And gives you an opportunity to randomise your variants en'masse BlockUtilities.RefreshMap(createdMap, randomiseInitialMap); }
void RandomlyModifyMap() { //We'll retrieve the bounds of our map in order to pick a nice random block int x = UnityEngine.Random.Range(0, BlockUtilities.GetMapWidth(createdMap)); int y = UnityEngine.Random.Range(0, BlockUtilities.GetMapHeight(createdMap)); //The depth is a little different from the width/height, as depth may be negative int z = UnityEngine.Random.Range(BlockUtilities.GetMapLowerDepth(createdMap), BlockUtilities.GetMapUpperDepth(createdMap)); //Here we'll flip a figurative coin to decide if we'll add or remove if (UnityEngine.Random.Range(0, 2) == 1) { //We'll remove a block in this case BlockUtilities.RemoveBlockFromMap(createdMap, x, y, z, true, false); } else { //Again, we'll create a block Block b = GameObject.Instantiate(blockPrefab) as Block; //And then add it to the map //We'll randomise on addition //And pending our CleanMap parameter, we'll refresh the surrounding blocks BlockUtilities.AddBlockToMap(createdMap, b, true, 0, true, x, y, z, false, true); } }
void DestroyBlock(Vector3 coords) { int x = (int)coords.x; int y = (int)coords.y; int z = (int)coords.z; Block b = BlockUtilities.GetBlockAt(blockMap, x, y, z); OrientedBlock ob = null; if (b != null) { ob = b as OrientedBlock; } if (ob != null) { StreamingMapNode n = GetNodeAt(x, y, z); if (n != null) { n.variantIndex = ob.GetCurrentVariant(); } GameObject cio = ob.GetCurrentInstantiatedObject(); if (cio != null) { TidyMapBoundObject mbo = cio.GetComponentInChildren <TidyMapBoundObject>(); if (mbo != null) { if (!mbo.DestroyWhenStreaming()) { return; } } } } BlockUtilities.AddBlockToMap(blockMap, null, false, 0, true, x, y, z, false, false); }
//We'll construct the map here void ConstructLevel() { bool[,] map = GetLevelMap(levelWidth, levelHeight); //We'll go ahead and create our map now createdMap = BlockUtilities.CreateBlockMap(mapName, tileSize, chunkWidth, chunkHeight, growthAxis); //We're just going to iterate through the level we got back from the //function (trusting that it's correctly sized) for (int x = 0; x < levelWidth; x++) { for (int y = 0; y < levelHeight; y++) { if (map[x, y]) { Block b = GameObject.Instantiate(blockPrefab) as Block; //We'll add our instantiated block here //And we will not refresh just yet - //it's better to refresh and clean at the end BlockUtilities.AddBlockToMap(createdMap, b, false, 0, false, x, y, 0, false, false); } else { if (useEmptyBlocks) { BlockUtilities.AddBlockToMap(createdMap, null, false, 0, false, x, y, 0, false, true); } } } } //And now we refresh our map BlockUtilities.RefreshMap(createdMap, randomiseInitialMap); //Done! //Enjoy! }
//Instantiate our block! Wrap the AssetPool functions nicely void InstantiateBlock(Vector3 coords) { int x = (int)coords.x; int y = (int)coords.y; int z = (int)coords.z; StreamingMapNode n = GetNodeAt(x, y, z); Block b = null; int bv = 0; if (n != null) { b = n.blockPrefab; bv = n.variantIndex; } Block toAdd = null; if (b != null) { GameObject o = AssetPool.Instantiate(b.gameObject) as GameObject; #if UNITY_4_0 o.SetActive(true); #else // o.SetActiveRecursively (true); #endif toAdd = o.GetComponent <Block>(); OrientedBlock ob = toAdd as OrientedBlock; if (ob != null) { ob.PreRandomiseBlockOrientations(); } } if (n != null && n.HasVariant()) { BlockUtilities.AddBlockToMap(blockMap, toAdd, false, bv, true, x, y, z, false, false); } else { BlockUtilities.AddBlockToMap(blockMap, toAdd, true, bv, true, x, y, z, false, false); } if (n != null) { if (!n.HasVariant()) { Vector3 focus = new Vector3(focus_x, focus_y, focus_z); if (!IsOnOuterRim(focus, coords)) { //Let's save the variant so that we always get a consistent map //It saves the programmer having to randomise this themselves OrientedBlock ob = BlockUtilities.GetBlockAt(blockMap, x, y, z) as OrientedBlock; if (ob != null) { n.variantIndex = ob.GetCurrentVariant(); } } } } }