public static void SpawnNewCandy(GridManager.CellContents candyType, Vector2Int gridPosition) { if (candyType == GridManager.CellContents.empty || candyType == GridManager.CellContents.hole) { Debug.LogError("GridDisplayer.cs : Couldn't spawn new candy because given CellContents value is not a candy."); return; } GameObject newCandy = null; switch (candyType) { case GridManager.CellContents.candy_blue: newCandy = S.blueCandyPool.TakeFromPool(true, S.gridElementContainer); break; case GridManager.CellContents.candy_red: newCandy = S.redCandyPool.TakeFromPool(true, S.gridElementContainer); break; case GridManager.CellContents.candy_green: newCandy = S.greenCandyPool.TakeFromPool(true, S.gridElementContainer); break; case GridManager.CellContents.candy_orange: newCandy = S.orangeCandyPool.TakeFromPool(true, S.gridElementContainer); break; case GridManager.CellContents.candy_yellow: newCandy = S.yellowCandyPool.TakeFromPool(true, S.gridElementContainer); break; } newCandy.transform.position = GridToWorld(gridPosition); newCandy.transform.rotation = Quaternion.identity; newCandy.transform.localScale = new Vector3(S.cellSize * S.candySize, S.cellSize * S.candySize, S.cellSize * S.candySize); GridElement_Candy gridElementComponent = newCandy.GetComponent <GridElement_Candy>(); // wire the GridElement methods to the GridCell delegates gridElementComponent.RegisterMethodsOnCell(gridPosition); gridElementComponent.x = gridPosition.x; gridElementComponent.y = gridPosition.y; }
/// <summary> /// This static function returns a GridElement_Candy to its pool according to its color variable. /// </summary> /// <param name="candy"></param> public static void ReturnCandyToPool(GridElement_Candy candy) { CandyPool pool = null; switch (candy.color) { case GridElement_Candy.CandyColor.red: pool = S.redCandyPool; break; case GridElement_Candy.CandyColor.blue: pool = S.blueCandyPool; break; case GridElement_Candy.CandyColor.green: pool = S.greenCandyPool; break; case GridElement_Candy.CandyColor.yellow: pool = S.yellowCandyPool; break; case GridElement_Candy.CandyColor.orange: pool = S.orangeCandyPool; break; default: Debug.LogError("GridDisplayer.cs : The candy you are trying to return to its pool has an unrecognized color.", candy.gameObject); break; } if (pool != null) { pool.ReturnToPool(candy.gameObject); } }
public static void NotifyTweenEnd(GridElement_Candy candy) { S.tweeningCandies.Remove(candy); }
public static void NotifyTweenStart(GridElement_Candy candy) { S.tweeningCandies.Add(candy); }
void GridDisplayInit() { numberOfPooledCandies = Mathf.RoundToInt((GridManager.GRID_HEIGHT * GridManager.GRID_WIDTH) * totalCellsToPoolCandiesRatio); InitializeCandyPools(numberOfPooledCandies); float hCellSize = (gridBackgroundSprite.size.x - (horizontalMargin * 2)) / GridManager.GRID_WIDTH; float vCellSize = (gridBackgroundSprite.size.y - (verticalMargin * 2)) / GridManager.GRID_HEIGHT; cellSize = Mathf.Min(hCellSize, vCellSize); GameObject current = null; for (int i = 0; i < GridManager.GRID_WIDTH; ++i) { for (int j = 0; j < GridManager.GRID_HEIGHT; ++j) { current = null; if (GridManager.GRID[i, j].cellContent != GridManager.CellContents.hole) { // instantiate cells and set their sizes // maybe use a pool for cell backgrounds ? (no need probably) GameObject cell = Instantiate(gridCellBackgroundPrefab, GridToWorld(i, j), Quaternion.identity, cellContainer); cell.GetComponent <SpriteRenderer>().size = new Vector2(cellSize, cellSize); } switch (GridManager.GRID[i, j].cellContent) { case GridManager.CellContents.hole: break; case GridManager.CellContents.empty: break; case GridManager.CellContents.candy_blue: current = blueCandyPool.TakeFromPool(true, gridElementContainer); break; case GridManager.CellContents.candy_red: current = redCandyPool.TakeFromPool(true, gridElementContainer); break; case GridManager.CellContents.candy_green: current = greenCandyPool.TakeFromPool(true, gridElementContainer); break; case GridManager.CellContents.candy_orange: current = orangeCandyPool.TakeFromPool(true, gridElementContainer); break; case GridManager.CellContents.candy_yellow: current = yellowCandyPool.TakeFromPool(true, gridElementContainer); break; } if (current != null) { current.transform.position = GridToWorld(i, j); current.transform.rotation = Quaternion.identity; current.transform.localScale = new Vector3(cellSize * candySize, cellSize * candySize, cellSize * candySize); GridElement_Candy gridElementComponent = current.GetComponent <GridElement_Candy>(); // wire the GridElement methods to the GridCell delegates gridElementComponent.RegisterMethodsOnCell(i, j); gridElementComponent.x = i; gridElementComponent.y = j; } } } }