Ejemplo n.º 1
0
    public void InstantiateItems()
    {
        Debug.Assert(_IsInitialized, "GridModule doesn't initialized. Call 'Initialize(int colMax, int rowMax)' method.");
        var gameTableItems = _gameTable.GetTableItems();

        for (int gameTableItemIndex = 0; gameTableItemIndex < gameTableItems.Length; gameTableItemIndex++)
        {
            if (gameTableItems[gameTableItemIndex] != null)
            {
                int col = gameTableItemIndex % _gameTable.ColMax;
                int row = gameTableItemIndex / _gameTable.ColMax;

                Vector3Int cellPosition = new Vector3Int(
                    (int)((col - _colOffset)),
                    (int)((row - _rowOffset)),
                    (int)_grid.cellSize.z);

                Vector3 cellCenterPosition = _grid.GetCellCenterWorld(cellPosition);

                var instantiatedItem = Instantiate(GameTableItemPrefab,
                                                   new Vector3(cellCenterPosition.x, cellCenterPosition.y, cellCenterPosition.z),
                                                   Quaternion.identity, this.transform);
                if (instantiatedItem != null)
                {
                    var sr = instantiatedItem.GetComponent <SpriteResolver>();
                    if (sr != null)
                    {
                        switch (gameTableItems[gameTableItemIndex].ItemType)
                        {
                        case GameTableItem.Type.Bacon:
                            sr.SetCategoryAndLabel("Game Table Items", "bacon");
                            break;

                        case GameTableItem.Type.Bread:
                            sr.SetCategoryAndLabel("Game Table Items", "bread");
                            break;

                        case GameTableItem.Type.Meat:
                            sr.SetCategoryAndLabel("Game Table Items", "meat");
                            break;

                        case GameTableItem.Type.Onion:
                            sr.SetCategoryAndLabel("Game Table Items", "onion");
                            break;

                        case GameTableItem.Type.Salad:
                            sr.SetCategoryAndLabel("Game Table Items", "salad");
                            break;

                        case GameTableItem.Type.Tomato:
                            sr.SetCategoryAndLabel("Game Table Items", "tomato");
                            break;

                        default:
                            sr.SetCategoryAndLabel("Game Table Items", "bread");
                            break;
                        }
                    }
                    sr.ResolveSpriteToSpriteRenderer();
                    instantiatedItem.name = $"GameTableItem(C:{col},R:{row})";
                    gameTableItems[gameTableItemIndex].InstantiatedPrefab = instantiatedItem;
                }
            }
            else
            {
                Debug.LogWarning($"gameTableItems[{gameTableItemIndex}] is null!");
            }
        }
    }