Exemple #1
0
    /// <summary>
    ///Adds a chunk at the given coordinates and expands the map 
    /// </summary>
    /// <param name="node">
    ///The chunk you wish to add to the map
    /// </param>
    /// <param name="x">
    ///The x target coordinate
    /// </param>
    /// <param name="y">
    ///The y target coordinate
    /// </param>
    public void AddNodeAt(ChunkSet node, int x, int y)
    {
        node.SetCoordinates(x,y);

        if(x < 0){

            //Debug.Log("ABS X: " + x);

            //expand and shuffle by Mathf.Abs(x)
            ExpandWidth(currentWidth + Mathf.Abs(x));
            ShuffleMap(Mathf.Abs(x),0);

            node.SetCoordinates(0,node.GetY());
        }
        else if(x >= currentWidth){
            //we just need to expand out by x - (currentWidth - 1), no need to shuffle

            if(currentWidth == 0){
                ExpandWidth(x+1);
            }
            else{
                ExpandWidth(x+1);
            }
        }

        if(y < 0){

            //Debug.Log("ABS Y: " + x);

            //expand and shuffle by Mathf.Abs(y)
            ExpandHeight(currentHeight + Mathf.Abs(y));
            ShuffleMap(0,Mathf.Abs(y));
            node.SetCoordinates(node.GetX(),0);

        }
        else if(y >= currentHeight){
            //we just need to expand up by y - (currentHeight - 1), no need to shuffle
            ExpandHeight(y+1);
        }

        SetNodeAt(node,node.GetX(),node.GetY());
    }
	void ExpandHeight(int newHeight){
		
		ChunkSet[] newMap = new ChunkSet[newHeight * currentWidth];
	
		if(map != null){
			
			int upperBound = currentHeight;
			
			if(currentHeight > newHeight){
				upperBound = newHeight;
			}
			
			//When expanding height, we don't need to do any tricky mathematics to cater for one-dimensional array storage
			for(int x = 0; x < currentWidth; x++){
				for(int y = 0; y < upperBound; y++){
					
					int index = y * currentWidth + x;
					
					ChunkSet n = map[index];
					
					newMap[index] = n;
				}
			}
			
		}
		map = newMap;
		
		currentHeight = newHeight;
	
	}
	void ExpandWidth(int newWidth){
		
		ChunkSet[] newMap = new ChunkSet[currentHeight * newWidth];
	
		if(map != null){
			
			int upperBound = currentWidth;
			
			if(currentWidth > newWidth){
				upperBound = newWidth;
			}
			
			for(int x = 0; x < upperBound; x++){
				for(int y = 0; y < currentHeight; y++){
					
					int index = y * currentWidth + x;
					
					ChunkSet n = map[index];
					
					index = y * newWidth + x;
					
					newMap[index] = n;
				}
			}
			
		}
		
		map = newMap;
		
		currentWidth = newWidth;
	}
	void SetNodeAt(ChunkSet node, int x, int y){
				
		int index = y * currentWidth + x;
		
		map[index] = node;
		
		node.SetCoordinates(x,y);
	}
	public void Editor_AddChunkAt(int x, int y, int depth, MapChunk chunkPrefab, bool placeAbsolute){
						
		ChunkSet cs = GetNodeAt(x,y);
				
		if(depth > mapUpperDepth){
			mapUpperDepth = depth;
		}
		
		if(depth < mapLowerDepth){
			mapLowerDepth = depth;
		}
		
		MapChunk mc = null;
		
		if(cs == null){
			cs = new ChunkSet();
			
			cs.InitializeChunkSet(this,x,y);
			
			AddNodeAt(cs,x,y);
			
			mc = GameObject.Instantiate(chunkPrefab) as MapChunk;
			
			mc.name = "MapChunk_"+x+"_"+y;
						
			cs.AddChunkAt(mc,depth);
			
			mc.Editor_Activate(chunkWidth,chunkHeight,cs.x,cs.y,depth,this,placeAbsolute);
						
			return;
		}
		else{
			
			if(cs.x != x){
				cs.x = x;
			}
			
			if(cs.y != y){
				cs.y = y;
			}
			
		}
				
		if(cs.GetChunkAt(depth,false) != null){
									
			return;
		}
		
		mc = GameObject.Instantiate(chunkPrefab) as MapChunk;
			
		mc.name = "MapChunk_"+x+"_"+y;
			
		//at this point we want to do a different activation, as the chunkset already exists
		//and this is therefore at a different depth
		cs.AddChunkAt(mc,depth);
		
		int upper = depth+1;
		int lower = depth-1;
		
		MapChunk u = cs.GetChunkAt(upper,false);
		MapChunk l = cs.GetChunkAt(lower,false);
				
		if(u != null){
			
			mc.Editor_ActivateBound(chunkWidth,chunkHeight,cs.x,cs.y,depth,this,u);
			
		}
		else if(l != null){
			
			mc.Editor_ActivateBound(chunkWidth,chunkHeight,cs.x,cs.y,depth,this,l);
			
		}
		else{
			
			mc.Editor_Activate(chunkWidth,chunkHeight,cs.x,cs.y,depth,this,placeAbsolute);
		}
				
	}