using System; using System.Collections.Generic; using System.Linq; using Data; using HexFiled; using Runtime.Controller; using UnityEngine; using Object = UnityEngine.Object; using Random = UnityEngine.Random; namespace Items { public class ItemFabric : IExecute { private ItemsData _data; private List _openList; private List _itemTypes; private Dictionary _itemInfos; private float _spawnTime; private float time; public ItemFabric(ItemsData data, List dictionary) { _itemInfos = new Dictionary(); data.ItemInfos.ForEach(info => { _itemInfos.Add(info.Type, info); }); _itemTypes = dictionary; _data = data; _openList = new List(); _spawnTime = Random.Range(data.SpawnTime.from, data.SpawnTime.to); } public void UpdateCellToOpenList(HexCell cell) { if (cell.Color != UnitColor.GREY) { _openList.Add(cell); } else if (_openList.Contains(cell)) { _openList.Remove(cell); } } public void Execute() { if (Time.time - time >= _spawnTime) { List closedList = PaintedController.UnitCurrentCell.Select(unitCells => unitCells.Value.curent) .ToList(); time = Time.time; var cell = _openList[Random.Range(0, _openList.Count - 1)]; while (closedList.Contains(cell) || cell.Item != null) { cell = _openList[Random.Range(0, _openList.Count - 1)]; } var type = _itemTypes[Random.Range(0, _itemTypes.Count - 1)]; var info = _itemInfos[type.ToString().Replace("Items.", "")]; var obj = (Item)Activator.CreateInstance(type, info); var go = obj.Spawn(cell); go.AddComponent().isTrigger = true; var itemView = go.AddComponent(); itemView.SetUp(obj); cell.SetItem(obj); _spawnTime = Random.Range(_data.SpawnTime.from, _data.SpawnTime.to); } } } }