Esempio n. 1
0
	void OnEnable()
	{
		Debug.Assert(Cells.X > 0 && Cells.Y > 0 && CellDimensions.x > 0 && CellDimensions.y > 0, "INVALID GRID! Gör om gör rätt!");

		Vector3 centerPosition = Camera.main.transform.position;
		//float centerX = (Cells.X * CellDimensions.x) * 0.5f;
		float centerY = (Cells.Y * CellDimensions.y) * 0.5f;
		float startX = centerPosition.x;//centerPosition.x - centerX;
		float startY = centerPosition.y - centerY;

		m_cells = new List<List<GridCell>>(Cells.Y);
		for (int y = 0; y < Cells.Y; ++y) 
		{
			List<GridCell> newColumn = new List<GridCell>(Cells.X);
			for (int x = 0; x < Cells.X; ++x) 
			{
				GridCell cell = new GridCell();
				cell.SetDimensions(CellDimensions.x, CellDimensions.y);
				cell.SetPosition(startX + CellDimensions.x * 0.5f + (x * CellDimensions.x), (startY + CellDimensions.y * 0.5f + (y * CellDimensions.y)), Camera.main.nearClipPlane);
				newColumn.Add(cell);
			}
			m_cells.Add(newColumn);
		}			
	}
Esempio n. 2
0
	private void LoadFromPath(string path)
	{		
		if(!File.Exists(path))
		{
			Debug.LogError("File doesn't exist!");
			return;
		}

		GameObject grid = GameObject.Find("Grid");
		Grid gridComponent = grid.GetComponent<Grid>();
		List<List<GridCell>> cells = gridComponent.GetCells();

		StreamReader reader = File.OpenText(path);
		gridComponent.Cells.X = int.Parse(reader.ReadLine());
		gridComponent.Cells.Y = int.Parse(reader.ReadLine());
		gridComponent.CellDimensions.x = float.Parse(reader.ReadLine());
		gridComponent.CellDimensions.y = float.Parse(reader.ReadLine());

		Vector3 centerPosition = Camera.main.transform.position;
		//float centerX = (gridComponent.Cells.X * gridComponent.CellDimensions.x) * 0.5f;
		float centerY = (gridComponent.Cells.Y * gridComponent.CellDimensions.y) * 0.5f;
		float startX = Camera.main.transform.position.x; //centerPosition.x - centerX;
		float startY = centerPosition.y - centerY;

		gridComponent.Reset();
		cells.Clear();

		string readLine = "";
		cells = new List<List<GridCell>>(gridComponent.Cells.Y);
		for (int y = 0; y < gridComponent.Cells.Y; ++y) 
		{
			List<GridCell> newColumn = new List<GridCell>(gridComponent.Cells.X);
			for (int x = 0; x < gridComponent.Cells.X; ++x) 
			{
				GridCell cell = new GridCell();
				cell.SetDimensions(gridComponent.CellDimensions.x, gridComponent.CellDimensions.y);
				cell.SetPosition(startX + gridComponent.CellDimensions.x * 0.5f + (x * gridComponent.CellDimensions.x), (startY + gridComponent.CellDimensions.y * 0.5f + (y * gridComponent.CellDimensions.y)), Camera.main.nearClipPlane);

				readLine = reader.ReadLine();
				if(readLine != "null")
				{
					GameObject block = GameObjectPool.Instance.GetFromPool(readLine, true);
					block.transform.position = cell.GetPosition();

					if(cell.GetBlock() != null)
						GameObjectPool.Instance.AddToPool(cell.GetBlock().gameObject);	

					cell.SetBlock(block.GetComponent<BlockBase>());		
				}
				newColumn.Add(cell);
			}
			cells.Add(newColumn);
		}	
		gridComponent.SetCells(cells);
	}