示例#1
0
 // Start is called before the first frame update
 void Start()
 {
     //If hasnt been assigned
     if (aiGrid == null)
     {
         // Try find it
         aiGrid = GameObject.FindGameObjectWithTag("AIGrid").GetComponent <AIGrid>();
         //If still null
         if (aiGrid == null)
         {
             //Log error
             Debug.LogError("AI grid has not been assigned or could not be found, there can only be one grid in a scene.");
         }
     }
 }
	private void debugLevelLoad(){
		gridSize = 10;
		gameGrid = new GridBlock[gridSize, gridSize]; 
		GameObject tempObject;      
		for(int x = 0; x < gridSize; x++){
			for(int y = 0; y < gridSize; y++){
				tempObject = Instantiate(gridBlock) as GameObject;
				// Set name for debuging.
				tempObject.name = "gridBlock " + x + "," + y;
				// Start the block relative to the master object.
				tempObject.transform.position = gridStartPoint.position;
				// Move the space to the correct spot.
				tempObject.transform.position = tempObject.transform.position + new Vector3((float)x * 2, 0, (float)y * 2);
				// Parent the grid space to this object.
				tempObject.transform.SetParent(transform); 
				//A pointer to the grid block script from the tempObject is stored in the array for easy access.
				gameGrid[x, y] = tempObject.GetComponent<GridBlock>();
				// Each space has a refrence to this script for easy access.
				gameGrid[x, y].GridManager = this; 
				gameGrid[x, y].gridLocation.x = x;
				gameGrid[x, y].gridLocation.y = y;
			}
		}

		//Setup refrences from one grid block to another to improve unit interaction.
		for(int x = 0; x < gridSize; x++){
			for(int y = 0; y < gridSize; y++){
				if(y + 1 < gridSize){
					gameGrid[x, y].setAdj(Direction.UP, gameGrid[x, y + 1]);
				}
				if(y - 1 >= 0){
					gameGrid[x, y].setAdj(Direction.DOWN, gameGrid[x, y - 1]);
				}
				if(x - 1 >= 0){
					gameGrid[x, y].setAdj(Direction.LEFT, gameGrid[x - 1, y]);
				}
				if(x + 1 < gridSize){
					gameGrid[x, y].setAdj(Direction.RIGHT, gameGrid[x + 1, y]);
				}
			}
		}

		// Setup default spawn blocks for testing purposes.
		gameGrid[2, 2].setSpawn(team[0], false);
		gameGrid[5, 2].setSpawn(team[1], false);
		gameGrid[0, 0].setSpawn(team[2], false);
		gameGrid[1, 1].setSpawn(team[3], false);

		//Initialize AI grid.
		aiGrid = new AIGrid(this);
	}
	public void loadLevel(string levelXmlPath){
		// destroy all current grid blocks
		if(gameGrid != null)
			foreach( var item in gameGrid ){
				foreach( Transform child in item.transform ){
					Destroy(child.gameObject);
				}
				Destroy(item.gameObject);
			}

		FileStream stream = null;
		GridInfo container;

		try{
			XmlSerializer serializer = new XmlSerializer(typeof(GridInfo));
			stream = new FileStream(levelXmlPath, FileMode.Open);
			container = (GridInfo)serializer.Deserialize(stream);
			stream.Close();
		} catch(Exception ex){
			if(stream != null)
				stream.Close();
			Debug.LogError("level load failed, error:/n" + ex);
			return;
		}

		gridSize = container.gridSize;
		gameGrid = new GridBlock[gridSize, gridSize]; 
		GameObject tempObject;  
		for(int x = 0; x < gridSize; x++){
			for(int y = 0; y < gridSize; y++){
				tempObject = Instantiate(gridBlock) as GameObject;
				// Set name for debuging.
				tempObject.name = "gridBlock " + x + "," + y;
				// Start the block relative to the master object.
				tempObject.transform.position = gridStartPoint.position;
				// Move the space to the correct spot.
				tempObject.transform.position = tempObject.transform.position + new Vector3((float)x * 2, 0, (float)y * 2);
				// Parent the grid space to this object.
				tempObject.transform.SetParent(transform); 
				//A pointer to the grid block script from the tempObject is stored in the array for easy access.
				gameGrid[x, y] = tempObject.GetComponent<GridBlock>();
				// Each space has a refrence to this script for easy access.
				gameGrid[x, y].GridManager = this; 
				gameGrid[x, y].gridLocation.x = x;
				gameGrid[x, y].gridLocation.y = y;	

				// setup level from file
				if(container.enabled[x + y * gridSize] == false)
					gameGrid[x, y].toggleSpaceAvailable();
				if(container.isSpawnSpot[x + y * gridSize] && container.team[x + y * gridSize] != -1 && container.isAI[x + y * gridSize] == false){
					gameGrid[x, y].setSpawn(team[container.team[x + y * gridSize]], false);
				} else if(container.isSpawnSpot[x + y * gridSize] && container.team[x + y * gridSize] != -1 && container.isAI[x + y * gridSize]){
					gameGrid[x, y].setSpawn(team[container.team[x + y * gridSize]], true);
				}
			}
		}

		for(int x = 0; x < gridSize; x++){
			for(int y = 0; y < gridSize; y++){
				if(y + 1 < gridSize){
					gameGrid[x, y].setAdj(Direction.UP, gameGrid[x, y + 1]);
				}
				if(y - 1 >= 0){
					gameGrid[x, y].setAdj(Direction.DOWN, gameGrid[x, y - 1]);
				}
				if(x - 1 >= 0){
					gameGrid[x, y].setAdj(Direction.LEFT, gameGrid[x - 1, y]);
				}
				if(x + 1 < gridSize){
					gameGrid[x, y].setAdj(Direction.RIGHT, gameGrid[x + 1, y]);
				}
			}
		}

		//Initialize AI grid.
		aiGrid = new AIGrid(this);
	}
	public void newLevel(int size){
		gridSize = size;
		// destroy all current grid blocks
		if(gameGrid != null)
			foreach( var item in gameGrid ){
				foreach( Transform child in item.transform ){
					Destroy(child.gameObject);
				}
				Destroy(item.gameObject);
			}

		gameGrid = new GridBlock[size, size]; 
		GameObject tempObject;      
		for(int x = 0; x < size; x++){
			for(int y = 0; y < size; y++){
				tempObject = Instantiate(gridBlock) as GameObject;
				// Set name for debuging.
				tempObject.name = "gridBlock " + x + "," + y;
				// Start the block relative to the master object.
				tempObject.transform.position = gridStartPoint.position;
				// Move the space to the correct spot.
				tempObject.transform.position = tempObject.transform.position + new Vector3((float)x * 2, 0, (float)y * 2);
				// Parent the grid space to this object.
				tempObject.transform.SetParent(transform); 
				//A pointer to the grid block script from the tempObject is stored in the array for easy access.
				gameGrid[x, y] = tempObject.GetComponent<GridBlock>();
				// Each space has a refrence to this script for easy access.
				gameGrid[x, y].GridManager = this; 
				gameGrid[x, y].gridLocation.x = x;
				gameGrid[x, y].gridLocation.y = y;
			}
		}

		

		//Setup refrences from one grid block to another to improve unit interaction.
		for(int x = 0; x < size; x++){
			for(int y = 0; y < size; y++){
				if(y + 1 < size){
					gameGrid[x, y].setAdj(Direction.UP, gameGrid[x, y + 1]);
				}
				if(y - 1 >= 0){
					gameGrid[x, y].setAdj(Direction.DOWN, gameGrid[x, y - 1]);
				}
				if(x - 1 >= 0){
					gameGrid[x, y].setAdj(Direction.LEFT, gameGrid[x - 1, y]);
				}
				if(x + 1 < size){
					gameGrid[x, y].setAdj(Direction.RIGHT, gameGrid[x + 1, y]);
				}
			}
		}
		//Initialize AI grid.
		aiGrid = new AIGrid(this);
	}
示例#5
0
 private void Awake()
 {
     Instance = this;
 }
示例#6
0
 // Use this for initialization
 void Start()
 {
     grid = GetComponent <AIGrid>();
 }