using System; using System.Collections; using System.Collections.Generic; using DG.Tweening; using UnityEngine; using Random = System.Random; public class ItemSystem : MonoBehaviour { public static ItemSystem ins; public List unLockGrids; [SerializeField] public Transform unLockTrans; public Transform packPosition; public List itemObjs=new List(); public List LoadItemObjs = new List(); private Dictionary itemObjDic=new Dictionary(); public float moveTime { get; private set; } public float boxMoveTime { get; private set; } public float speed { get; private set; } public float boxSpeed { get; private set; } public float points { get; private set; } public float boxPoints { get; private set; } private void Awake() { ins = this; ResetDic(); moveTime = 10; speed = 100; points = 0; boxMoveTime = 10; boxSpeed = 100; boxPoints = 0; } private void ResetDic() { foreach (var item in LoadItemObjs) { itemObjDic.Add(item.ID,item); } } public ItemObj itemObj(string key) { return itemObjDic[key]; } /// /// 初始化物品列表 /// /// public void InitItem(List itemDatas) { if (itemObjs.Count>0) { for (int i = itemObjs.Count-1; i >= 0; i--) { RestGrid(itemObjs[i]._grids); Destroy(itemObjs[i].gameObject); } itemObjs = new List(); } foreach (var itemData in itemDatas) { var obj = Instantiate(itemData.item, transform); var item = obj.GetComponent(); SetPosition(item,MapManager.ins.WaitGrids[itemData.x,itemData.y]); item.startGrid = MapManager.ins.WaitGrids[itemData.x, itemData.y]; List grids = new List(); for (var i = itemData.x; i < itemData.x+item.itemSize.x; i++) { for (var j = itemData.y; j < itemData.y + item.itemSize.y; j++) { // MapManager.ins.WaitGrids[i, j].item = item; MapManager.ins.WaitGrids[i, j].SaveItemObj(item); grids.Add(MapManager.ins.WaitGrids[i, j]); } } item.type = GridType.wait; item.SetOccGrid(grids); itemObjs.Add(item); } ResetWaitGrid(); } /// /// 开启随机 /// /// public IEnumerator RandomItem(List list,Action action) { BoxDebug.Log("开始随机道具位置"); List objs = new List(); foreach (var randomItem in list) { for (int i = 0; i < randomItem.num; i++) { var obj = Instantiate(randomItem.item.gameObject, transform); obj.GetComponent().SetLevel(randomItem.level,randomItem.itemID); objs.Add(obj.GetComponent()); yield return null; } } var randomData = RandomSortList(objs); ResetPool(randomData); action.Invoke(); } /// /// 放置道具 /// /// public void ResetPool(List ListT) { BoxDebug.Log("开始地块放置"); List> list = new List>(); list.Add(new List()); int x = 0; int y = 0; int index = 0; List itemMatrix = new List(); itemMatrix.Add(new ItemObj[MapManager.ins.waitX, MapManager.ins.waitY]); foreach (var item in ListT) { GetMatrixIndex(itemMatrix[index], item,list[index], out var bo); if (!bo) { itemMatrix.Add(new ItemObj[MapManager.ins.waitX, MapManager.ins.waitY]); list.Add(new List()); index++; GetMatrixIndex(itemMatrix[index], item,list[index], out var bos); } } CoordinateRandom(list); if (itemObjs.Count>0)//清理预制体 { for (int i = itemObjs.Count-1; i >= 0; i--) { RestGrid(itemObjs[i]._grids); Destroy(itemObjs[i].gameObject); } itemObjs = new List(); } for (int i = list.Count-1; i >= 0; i--) { var startData = list[i]; foreach (var start in startData) { start.item.startGrid = MapManager.ins.WaitGrids[start.x, start.y]; start.item.orderIndexSet = (list.Count - i)*2; SetPosition(start.item,start.item.startGrid); List grids = new List(); for (var k = start.x; k < start.x+start.item.itemSize.x; k++) { for (var f = start.y; f < start.y + start.item.itemSize.y; f++) { // MapManager.ins.WaitGrids[i, j].item = item; MapManager.ins.WaitGrids[k, f].SaveItemObj(start.item); grids.Add(MapManager.ins.WaitGrids[k, f]); } } start.item.SetOccGrid(grids); itemObjs.Add(start.item); } } ResetWaitGrid(); } public void ResetWaitGrid() { foreach (var grid in MapManager.ins.WaitList) { grid.RemoveNullStack(); grid.ResetWaitBack(); } foreach (var item in itemObjs) { item.Reset(); } } private void CoordinateRandom(List> list) { foreach (var startList in list) { ItemObj[,] itemMatrix=new ItemObj[MapManager.ins.waitX, MapManager.ins.waitY]; foreach (var start in startList) { for (int i = start.x; i < start.x+start.item.itemSize.x; i++) { for (int j = start.y; j < start.y+start.item.itemSize.y; j++) { itemMatrix[i, j] = start.item; } } } foreach (var start in startList) { RandomCoordinate(itemMatrix, start); } } } private void RandomCoordinate(ItemObj[,] itemMatrix,StartGrid startGrid) { int indexX = UnityEngine.Random.Range(startGrid.x, MapManager.ins.waitX-startGrid.item.itemSize.x); int indexY = UnityEngine.Random.Range(startGrid.y, MapManager.ins.waitY-startGrid.item.itemSize.y); for (int i = startGrid.x; i < startGrid.x+startGrid.item.itemSize.x; i++) { for (int j = startGrid.y; j < startGrid.y+startGrid.item.itemSize.y; j++) { itemMatrix[i, j] = null; } } for (int i = indexX; i < startGrid.item.itemSize.x+indexX; i++) { for (int j = indexY; j < startGrid.item.itemSize.y+indexY; j++) { if (itemMatrix[i,j]!=null) { RandomCoordinate(itemMatrix, startGrid); return; } } } for (int i = indexX; i < startGrid.item.itemSize.x+indexX; i++) { for (int j = indexY; j < startGrid.item.itemSize.y+indexY; j++) { itemMatrix[i, j] = startGrid.item; } } startGrid.x = indexX; startGrid.y = indexY; } /// /// 放置道具 /// /// /// /// /// public void GetMatrixIndex(ItemObj[,] itemObjs,ItemObj item,List grids,out bool bo) { for (int i = 0; i < MapManager.ins.waitX; i++) { for (int j = 0; j < MapManager.ins.waitY; j++) { if (MatrixNull(itemObjs,item,i,j)) { for (int k = i; k < i+item.itemSize.x; k++) { for (int l = 0; l < j+item.itemSize.y; l++) { itemObjs[k, l] = item; } } var itemGrid = new StartGrid(); itemGrid.x = i; itemGrid.y = j; itemGrid.item = item; grids.Add(itemGrid); bo=true; return; } } } bo = false; } /// /// 判断是否可以放置 /// /// /// /// /// /// public bool MatrixNull(ItemObj[,] itemObjs,ItemObj item,int x, int y) { if (x+item.itemSize.x>MapManager.ins.waitX||y + item.itemSize.y>MapManager.ins.waitY) { return false; } for (int i = x; i < x+item.itemSize.x; i++) { for (int j = y; j < y + item.itemSize.y; j++) { if (itemObjs[i,j]!=null) { return false; } } } return true; } public List RandomSortList(List ListT) { BoxDebug.Log("随机道具排序"); Random random = new Random(); List newList = new List(); foreach (T item in ListT) { newList.Insert(random.Next(newList.Count + 1), item); } return newList; } public void RestGrid(List grids) { foreach (var grid in grids) { grid.item = null; } } public void InsUnLock() { // int index = Random.Range(0, unLockGrids.Count); // var unLock = Instantiate(unLockGrids[index].gameObject,unLockTrans); } public void GetGridsRest(Grid grid,ItemObj item) { List grids = new List(); for (int i = grid.X; i < grid.X+item.itemSize.x; i++) { for (int j = grid.Y; j < grid.Y+item.itemSize.y; j++) { switch (grid.Type) { case GridType.box: grids.Add(MapManager.ins.boxGrid[i, j]); MapManager.ins.boxGrid[i, j].item = item; break; case GridType.wait: grids.Add(MapManager.ins.WaitGrids[i, j]); MapManager.ins.WaitGrids[i, j].SaveItemObj(item); break; default: throw new ArgumentOutOfRangeException(); } } } item.SetOccGrid(grids); ResetWaitGrid(); } /// /// 设置道具位置 /// /// /// public void SetPosition(ItemObj itemObj, Grid grid) { itemObj.transform.position = grid.transform.position + itemObj.dev; itemObj.type = grid.Type; } /// /// 刷新层级 /// public void ResetItemOrder() { StartCoroutine(ItemOrder()); } private IEnumerator ItemOrder() { foreach (var item in itemObjs) { item.ResetOrder(); yield return null; } } /// /// 打包道具 /// public void PackItem() { foreach (var item in itemObjs) { if (item.type==GridType.box) { RestGrid(item._grids); item.transform.position = packPosition.position; //item.transform.DOMove(packPosition.position, 0.2f); } } GameManager.ins.ResetPropattributes(); } public void ItemLevelUp(ItemObj item0,ItemObj item1) { if (item0 == item1) return; if (!item0.LevelUp(item1)) return; foreach (var grid in item1._grids) { grid.Remove(item1); } itemObjs.Remove(item1); Destroy(item1.gameObject); GameManager.ins.soundSystem.SynthesisAudio(); item0.Reset(); } public void ResetPropattributes() { float lsMoveTime = 10; float lsSpeed = 100; float lsPoint = 0; float lsBoxMoveTime = 10; float lsBoxSpeed = 100; float lsBoxPoint = 0; List list = new List(); foreach (var itemPropat in itemObjs) { if (itemPropat.type==GridType.box) { var data = JsonTab.Instance.tables.Prop.Get(itemPropat.itemID); switch (data.Propfunction) { case 1: lsMoveTime += data.Propattributes; break; case 2: lsSpeed += data.Propattributes; break; case 3: lsPoint += data.Propattributes; break; } } } foreach (var grid in itemObjs) { if (grid.transform.position.x<-10) { list.Add(grid); } } foreach (var itemObj in list) { if (itemObj.type==GridType.box) { var data = JsonTab.Instance.tables.Prop.Get(itemObj.itemID); switch (data.Propfunction) { case 1: lsBoxMoveTime += data.Propattributes; break; case 2: lsBoxSpeed += data.Propattributes; break; case 3: lsBoxPoint += data.Propattributes; break; } } } moveTime = lsMoveTime; speed = lsSpeed; points = lsPoint; boxMoveTime = lsBoxMoveTime; boxSpeed = lsBoxSpeed; boxPoints = lsBoxPoint; } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } } [Serializable] public class ItemData { public int x = 0; public int y = 0; public GameObject item; } [Serializable] public class RandomItemData { public int num; public int level; public int itemID; public ItemObj item; } [Serializable] public class StartGrid { public int x; public int y; public ItemObj item; } [Serializable] public class RandomData { public List _list = new List(); }