WXGame/box1/Assets/Script/ItemSystem.cs
2024-10-24 16:54:32 +08:00

122 lines
3.2 KiB
C#

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<ItemObj> itemObjs=new List<ItemObj>();
private void Awake()
{
ins = this;
}
public void InitItem(List<ItemData> 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<ItemObj>();
}
foreach (var itemData in itemDatas)
{
var obj = Instantiate(itemData.item, transform);
var item = obj.GetComponent<ItemObj>();
SetPosition(item,MapManager.ins.WaitGrids[itemData.x,itemData.y]);
item.startGrid = MapManager.ins.WaitGrids[itemData.x, itemData.y];
List<Grid> grids = new List<Grid>();
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<Grid> grids)
{
foreach (var grid in grids)
{
grid.item = null;
}
}
public void GetGridsRest(Grid grid,ItemObj item)
{
List<Grid> grids = new List<Grid>();
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;
}