Example #1
0
        /// <summary>
        ///Remove a block from the map (replacing it with an Empty Block)
        /// </summary>
        /// <param name="map">
        /// The map from which you wish to remove the block
        /// </param>
        /// <param name="x">
        /// The x coordinate of the block
        /// </param>
        /// <param name="y">
        /// The y coordinate of the block
        /// </param>
        /// <param name="depth">
        /// The depth of the block
        /// </param>
        /// <param name="addEmptyBlock">
        /// Add an empty block to the void left after removal?
        /// </param>
        /// <param name="destroyExistingImmediate">
        /// Use DestroyImmediate or Destroy when removing the existing block? (You must use immediate from within the Editor, and cannot use it once physics collisions)
        /// </param>
        public static void RemoveBlockFromMap(BlockMap map, int x, int y, int depth, bool addEmptyBlock, bool destroyImmediate)
        {
            MapChunk m = map.GetChunkForBlockCoordinate(x, y, depth);

            if (m != null)
            {
                Block eb = map.GetBlockAt(x, y, depth);

                if (eb != null)
                {
                    map.SetBlockAt(x, y, depth, null, destroyImmediate);
                }

                if (addEmptyBlock)
                {
                    map.SetBlockAt(x, y, depth, GetEmptyBlock(map), destroyImmediate);
                }
            }
        }
Example #2
0
        /// <summary>
        ///Add a block to the desired map
        /// </summary>
        /// <param name="map">
        /// The map to which you wish to add a block
        /// </param>
        /// <param name="b">
        /// The instantiated Block object that you wish to add to the map (if null, will add an EmptyBlock object)
        /// </param>
        /// <param name="randomise">
        /// Randomise the variant of this Block upon addition to the map?
        /// </param>
        /// <param name="variation">
        /// If not randomising, feel free to pass through the index of the variation you wish to display
        /// </param>
        /// <param name="refreshUponAddition">
        /// Refresh the surrounding blocks upon addition to the map?
        /// </param>
        /// <param name="x">
        /// The target x coordinate
        /// </param>
        /// <param name="y">
        /// The target y coordinate
        /// </param>
        /// <param name="depth">
        /// The depth to which to add the block
        /// </param>
        /// <param name="destroyExistingImmediate">
        /// Use DestroyImmediate or Destroy when removing the existing block? (You must use immediate from within the Editor, and cannot use it once physics collisions)
        /// </param>
        /// <param name="addEmptyWhenNull">
        /// When passing 'null', should we add an empty block, or should we simply destroy the space and leave it?
        /// </param>
        public static void AddBlockToMap(BlockMap map, Block b, bool randomise, int variation, bool refreshUponAddition, int x, int y, int depth, bool destroyExistingImmediate, bool addEmptyWhenNull)
        {
            if (b == null && addEmptyWhenNull)
            {
                b = GetEmptyBlock(map);
            }

            MapChunk m = map.GetChunkForBlockCoordinate(x, y, depth);

            if (m == null)
            {
                int mx = 0;

                mx = (int)((float)x / (float)map.chunkWidth);

                int my = 0;

                my = (int)((float)y / (float)map.chunkHeight);

                MapChunk mc = GetChunkPrefab();

                map.Editor_AddChunkAt(mx, my, depth, mc, true);

                if (destroyExistingImmediate)
                {
                    GameObject.DestroyImmediate(mc.gameObject);
                }
                else
                {
                    GameObject.Destroy(mc.gameObject);
                }

                m = map.GetChunkForBlockCoordinate(x, y, depth);

                Block[] cb = new Block[map.chunkWidth * map.chunkHeight];

                m.Editor_InitializeChunk(cb);
            }

            //Block eb = map.GetBlockAt(x,y,depth);

            map.SetBlockAt(x, y, depth, null, destroyExistingImmediate);

            map.SetBlockAt(x, y, depth, b, destroyExistingImmediate);

            if (randomise && b != null)
            {
                b.RandomiseVariant();
            }
            else if (b != null && variation != 0)
            {
                b.SetVariant(variation);
            }

            if (refreshUponAddition)
            {
                RefreshBlock(map, x, y, depth);
            }

            if (b != null && !b.retainCollider)
            {
                Collider c = b.GetComponent <Collider>();

                if (c != null)
                {
                    if (addEmptyWhenNull)
                    {
                        c.isTrigger = true;
                    }
                    else
                    {
                        if (destroyExistingImmediate)
                        {
                            GameObject.DestroyImmediate(c);
                        }
                        else
                        {
                            GameObject.Destroy(c);
                        }
                    }
                }
            }
        }
		/// <summary>
		///Add a block to the desired map 
		/// </summary>
		/// <param name="map">
		/// The map to which you wish to add a block
		/// </param>
		/// <param name="b">
		/// The instantiated Block object that you wish to add to the map (if null, will add an EmptyBlock object)
		/// </param>
		/// <param name="randomise">
		/// Randomise the variant of this Block upon addition to the map?
		/// </param>
		/// <param name="variation">
		/// If not randomising, feel free to pass through the index of the variation you wish to display
		/// </param>
		/// <param name="refreshUponAddition">
		/// Refresh the surrounding blocks upon addition to the map?
		/// </param>
		/// <param name="x">
		/// The target x coordinate
		/// </param>
		/// <param name="y">
		/// The target y coordinate
		/// </param>
		/// <param name="depth">
		/// The depth to which to add the block
		/// </param>
		/// <param name="destroyExistingImmediate">
		/// Use DestroyImmediate or Destroy when removing the existing block? (You must use immediate from within the Editor, and cannot use it once physics collisions)
		/// </param>
		/// <param name="addEmptyWhenNull">
		/// When passing 'null', should we add an empty block, or should we simply destroy the space and leave it?
		/// </param>
		public static void AddBlockToMap(BlockMap map, Block b, bool randomise, int variation, bool refreshUponAddition, int x, int y, int depth, bool destroyExistingImmediate, bool addEmptyWhenNull){
			
			if(b == null && addEmptyWhenNull){
				b = GetEmptyBlock(map);
			}
			
			MapChunk m = map.GetChunkForBlockCoordinate(x,y,depth);
			
			if(m == null){
								
				int mx = 0; 
				
				mx = (int)((float)x / (float)map.chunkWidth);
				
				int my = 0; 
				
				my = (int)((float)y / (float)map.chunkHeight);
				
				MapChunk mc = GetChunkPrefab();
				
				map.Editor_AddChunkAt(mx,my,depth,mc,true);
				
				if(destroyExistingImmediate){
					GameObject.DestroyImmediate(mc.gameObject);
				}
				else{
					GameObject.Destroy(mc.gameObject);
				}
				
				m = map.GetChunkForBlockCoordinate(x,y,depth);
				
				Block[] cb = new Block[map.chunkWidth * map.chunkHeight];
				
				m.Editor_InitializeChunk(cb);
				
			}
			
			//Block eb = map.GetBlockAt(x,y,depth);
			
			map.SetBlockAt(x,y,depth,null,destroyExistingImmediate);		
			
			map.SetBlockAt(x,y,depth,b,destroyExistingImmediate);
			
			if(randomise && b != null){
				b.RandomiseVariant();
			}
			else if(b != null && variation != 0){
				b.SetVariant(variation);
			}
			
			if(refreshUponAddition){
				RefreshBlock(map,x,y,depth);
			}
			
			if(b != null && !b.retainCollider){
				Collider c = b.GetComponent<Collider>();
				
				if(c != null){
					
					if(addEmptyWhenNull){
						c.isTrigger = true;
					}
					else{
						if(destroyExistingImmediate){
							GameObject.DestroyImmediate(c);
						}
						else{
							GameObject.Destroy(c);
						}
					}
				}
				
			}
			
		}
		/// <summary>
		///Remove a block from the map (replacing it with an Empty Block) 
		/// </summary>
		/// <param name="map">
		/// The map from which you wish to remove the block
		/// </param>
		/// <param name="x">
		/// The x coordinate of the block
		/// </param>
		/// <param name="y">
		/// The y coordinate of the block
		/// </param>
		/// <param name="depth">
		/// The depth of the block
		/// </param>
		/// <param name="addEmptyBlock">
		/// Add an empty block to the void left after removal?
		/// </param>
		/// <param name="destroyExistingImmediate">
		/// Use DestroyImmediate or Destroy when removing the existing block? (You must use immediate from within the Editor, and cannot use it once physics collisions)
		/// </param>
		public static void RemoveBlockFromMap(BlockMap map, int x, int y, int depth, bool addEmptyBlock, bool destroyImmediate){
			
			MapChunk m = map.GetChunkForBlockCoordinate(x,y,depth);
			
			if(m != null){
				
				Block eb = map.GetBlockAt(x,y,depth);
			
				if(eb != null){
					
					map.SetBlockAt(x,y,depth,null,destroyImmediate);
					
				}
				
				if(addEmptyBlock){
					map.SetBlockAt(x,y,depth,GetEmptyBlock(map),destroyImmediate);
				}
				
			}
			
		}