Tidy Tile Mapper‎ > ‎Runtime API‎ > ‎BlockUtilities‎ > ‎

AddBlockToMap

Adds a Block to the targetted BlockMap.

The core of procedural level generation!

Definition:

void AddBlockToMap(BlockMap map, Block b, bool randomise, int variation, bool refreshUponAddition, int x, int y, int depth, bool destroyExistingImmediate, bool addEmptyWhenNull)

Return value Description
void Nought but the satisfaction of a job well done.

Argument DataType Description
map BlockMap The BlockMap to which to add this Block.
b Block The (instantiated) Block to add to this BlockMap. Instantiation is left to the implementer in order to allow you to do whatever voodoo you wish. However, I recommend you strip the "(Clone)" suffix from instantiated Blocks.
randomise    bool Randomise the variation of the Block upon addition?
variation int (If not) What variation should we use when adding this Block to the BlockMap?
refreshUponAddition bool Refresh this Block (and the surrounding Blocks) upon addition to the BlockMap?
x intThe x coordinate to which to add this Block.    
y int The y coordinate to which to add this Block.
depth intThe depth (z) coordinate to which to add this Block.
destroyExistingImmediate boolIf the BlockMap already contains a Block, should we use DestroyImmediate to dispose of it? (Otherwise Destroy() will be used)
addEmptyWhenNull boolIf the Block argument is null, should we add an Empty Block to the map instead?

Example:

using UnityEngine;
using DopplerInteractive.TidyTileMapper.Utilities;

public class MapDemo : MonoBehaviour {
    
  //This is the map that we will create and subsequently modify
  BlockMap map = null;
  
  //Our map properties - correlate directly to the map properties in the Editor Extension
  public Vector3 tileScale = new Vector3(1.0f,1.0f,1.0f);
  public string mapName = "Our demonstration map";
  public BlockMap.GrowthAxis growthAxis = BlockMap.GrowthAxis.Up;
  
  //The dimensions of our demo map
  public int mapWidth;
  public int mapHeight;
  
  //This is the Block that we will be using to populate our map
  public Block blockPrefab;
  
  void Awake(){
    
    //Step one: Let's create a map
    map = BlockUtilities.CreateBlockMap(mapName, tileScale, 5, 5, growthAxis);
    
    //Let's enable pooling and kill many demonstration birds with one demonstration stone
    AssetPool.EnablePooling();
    
    GenerateMap();
    
  }
  
  void Update(){
    
    if(Input.GetMouseButtonDown(0)){
      
      //We're going to cast a ray, see if we hit a block, and toggle it
      
      Ray r = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
      
      RaycastHit hitInfo;
      
      if(Physics.Raycast(r,out hitInfo,mapHeight*1.5f)){
        
        Block b = hitInfo.collider.GetComponent<Block>();
        
        if(b != null){
          
          Block newBlock = null;
          
          if(b.isNullBlock){
            //This block is empty, so make it full  
            GameObject o = AssetPool.Instantiate(blockPrefab.gameObject);
            newBlock = o.GetComponent<Block>();
            
            //Done, add the new block to the map
            BlockUtilities.AddBlockToMap(map,newBlock,false,0,false,b.x,b.y,b.depth,false,false);  
                    
          }
          else{
            //This block is not empty, so make it empty
            BlockUtilities.RemoveBlockFromMap(map,b.x,b.y,b.depth,true,false);
        
          }
          
        }
        
      }
      
    }
    
  }
  
  void GenerateMap(){
    
    //Let's build a map
    for(int x = 0; x < mapWidth; x++){
      
      for(int y = 0; y < mapHeight; y++){
        
        //Controversially, we're going to populate our map entirely with blocks at first
        //After this, we will click on these blocks and replace them with empty blocks
        
        GameObject o = AssetPool.Instantiate(blockPrefab.gameObject);
        Block newBlock = o.GetComponent<Block>();
        
        BlockUtilities.AddBlockToMap(map,newBlock,false,0,false,x,y,0,false,false);
        
      }
      
    }
    
    BlockUtilities.RefreshMap(map,true);    

  }
    
}


Comments