using System; using System.Collections; using System.Collections.Generic; using DG.Tweening; using UnityEngine; public class ItemSystem : MonoBehaviour { public static ItemSystem ins; public Transform packPosition; public List itemObjs=new List(); private void Awake() { ins = this; } 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; grids.Add(MapManager.ins.WaitGrids[i, j]); } } item.type = GridType.wait; item.SetOccGrid(grids); itemObjs.Add(item); } } public void RestGrid(List grids) { foreach (var grid in grids) { grid.item = null; } } 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].item = item; break; default: throw new ArgumentOutOfRangeException(); } } } item.SetOccGrid(grids); } public void SetPosition(ItemObj itemObj, Grid grid) { itemObj.transform.position = grid.transform.position + itemObj.dev; itemObj.type = grid.Type; } public void PackItem() { foreach (var item in itemObjs) { if (item.type==GridType.box) { RestGrid(item._grids); item.transform.DOMove(packPosition.position, 0.2f); } } } // 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; }