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 = HexManager.UnitCurrentCell.Select(unitCells => unitCells.Value.cell) .ToList(); time = Time.time; var cell = _openList[Random.Range(0, _openList.Count - 1)]; while (closedList.Contains(cell) || cell.Item != null) { return; } var type = GetWeightedType(); while (type == null) { return; } 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); } } private Type GetWeightedType() { float randomNum = Random.Range(1, 101)/100f; List possibleTypes = new List(); _itemTypes.ForEach(type => { if (_itemInfos[type.ToString().Replace("Items.", "")].SpawnChance >= randomNum) { possibleTypes.Add(type); } }); return possibleTypes.Count > 0 ? possibleTypes[Random.Range(0, possibleTypes.Count - 1)] : null; } } }