コード例 #1
0
 void OnCollisionStay2D(Collision2D coll)
 {
     if (!inGrid &&
         rb.velocity.magnitude < .01f &&
         !pickedUp &&
         (coll.gameObject.layer == LayerMask.NameToLayer("Block") || coll.gameObject.layer == LayerMask.NameToLayer("Wall")))
     {
         Vector2  gridPos = Grid.me.ToGrid(transform.position);
         GridCell cell    = Grid.me.GetCell(gridPos);
         if (!cell.ocupied && Grid.me.GetCell(gridPos.x, gridPos.y - 1).ocupied)
         {
             inGrid         = true;
             rb.isKinematic = true;
             cell.GetBlock(this);
         }
         else if (dontPushAgainTimer <= 0)
         {
             GridCell nextCell = Grid.me.NextNearest((Vector2)transform.position);
             if (!nextCell.ocupied &&
                 Grid.me.GetCell(nextCell.gridPos.x, nextCell.gridPos.y - 1).ocupied&&
                 ((Vector2)transform.position - nextCell.worldPos).magnitude < .7f)
             {
                 inGrid         = true;
                 rb.isKinematic = true;
                 cell.GetBlock(this);
             }
             else
             {
                 rb.AddForce((cell.worldPos - (Vector2)transform.position).normalized * 1.75f, ForceMode2D.Impulse);
                 rb.AddForce(Vector2.up * 1.75f, ForceMode2D.Impulse);
                 dontPushAgainTimer = .05f;
             }
         }
     }
 }
コード例 #2
0
ファイル: IOManager.cs プロジェクト: clovos/SpaceSlider
	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);
	}