Beispiel #1
0
    public void InitializeRequestItems()
    {
        List <Item> items = new List <Item>();

        if (_gridLevel != null)
        {
            foreach (Vector2 coord in _gridLevel.chunks.Keys)
            {
                RoomChunk chunk = _gridLevel.chunks[coord];
                if (chunk == null)
                {
                    continue;
                }
                if (chunk.HasItems())
                {
                }
                else
                {
                    // randomize handles.
                    Transform[] handles = chunk.GetItemHandles();
                    for (int i = 0; i < handles.Length; i++)
                    {
                        Transform temp        = handles[i];
                        int       randomIndex = Random.Range(i, handles.Length);
                        handles[i]           = handles[randomIndex];
                        handles[randomIndex] = temp;
                    }
                    // spawn and set items.
                    int    chunkItemCount = Mathf.Min(handles.Length, _itemPerChunk);
                    Item[] newChunkItems  = new Item[chunkItemCount];
                    for (int i = 0; i < chunkItemCount; i++)
                    {
                        Transform  t = handles[i];
                        GameObject itemGameObject = GameObject.Instantiate(_itemPrefabs[(prefabId++) % _itemPrefabs.Length]);
                        itemGameObject.transform.position = t.position;
                        itemGameObject.transform.rotation = t.rotation;
                        itemGameObject.transform.parent   = t.parent;
                        Item   newItem    = itemGameObject.GetComponent <Item>();
                        int    letterId   = chunk.letterID;
                        string chunkLabel = "?";
                        if (letterId >= 0 && letterId < lettres.Length)
                        {
                            chunkLabel = lettres[letterId];
                        }
                        newItem.SetChunkLabel(chunkLabel);
                        newItem.SetRequestManager(this);
                        newChunkItems[i]          = newItem;
                        _transformToItemLookup[t] = newItem;
                    }
                    chunk.SetItems(newChunkItems);
                }



                if (chunk as ComptoirChunk != null)
                {
                    (chunk as ComptoirChunk).RegisterListener(this);
                }
                Item[] chunkItems = chunk.GetItems();
                if (chunkItems != null)
                {
                    items.AddRange(chunkItems);
                }
            }
        }
        for (int i = 0; i < items.Count; i++)
        {
            Item temp        = items[i];
            int  randomIndex = Random.Range(i, items.Count);
            items[i]           = items[randomIndex];
            items[randomIndex] = temp;
        }

        _requestedItems = items.ToArray();
    }