WXGame/box1/Assets/Script/ItemSystem.cs

319 lines
8.9 KiB
C#
Raw Normal View History

2024-10-23 17:55:55 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-10-24 16:54:32 +08:00
using DG.Tweening;
2024-10-23 17:55:55 +08:00
using UnityEngine;
2024-11-04 17:29:11 +08:00
using Random = System.Random;
2024-10-23 17:55:55 +08:00
public class ItemSystem : MonoBehaviour
{
public static ItemSystem ins;
2024-11-04 17:29:11 +08:00
public List<UnLockGrid> unLockGrids;
[SerializeField] public Transform unLockTrans;
2024-10-24 16:54:32 +08:00
public Transform packPosition;
public List<ItemObj> itemObjs=new List<ItemObj>();
2024-10-23 17:55:55 +08:00
private void Awake()
{
ins = this;
}
2024-10-29 15:22:32 +08:00
/// <summary>
/// 初始化物品列表
/// </summary>
/// <param name="itemDatas"></param>
2024-10-23 17:55:55 +08:00
public void InitItem(List<ItemData> itemDatas)
{
2024-10-24 16:54:32 +08:00
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>();
}
2024-10-23 17:55:55 +08:00
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;
MapManager.ins.WaitGrids[i, j].SaveItemObj(item);
2024-10-23 17:55:55 +08:00
grids.Add(MapManager.ins.WaitGrids[i, j]);
}
}
2024-10-24 16:54:32 +08:00
item.type = GridType.wait;
2024-10-23 17:55:55 +08:00
item.SetOccGrid(grids);
2024-10-24 16:54:32 +08:00
itemObjs.Add(item);
}
}
2024-11-04 17:29:11 +08:00
public void RandomItem(List<RandomItemData> list)
{
List<ItemObj> objs = new List<ItemObj>();
foreach (var randomItem in list)
{
for (int i = 0; i < randomItem.num; i++)
{
var obj = Instantiate(randomItem.item.gameObject, transform);
objs.Add(obj.GetComponent<ItemObj>());
}
}
var randomData = RandomSortList(objs);
ResetPool(randomData);
}
public void ResetPool(List<ItemObj> ListT)
{
List<List<StartGrid>> list = new List<List<StartGrid>>();
list.Add(new List<StartGrid>());
int x = 0;
int y = 0;
int index = 0;
List<ItemObj[,]> itemMatrix = new List<ItemObj[,]>();
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<StartGrid>());
index++;
GetMatrixIndex(itemMatrix[index], item,list[index], out var bos);
}
}
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>();
}
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];
SetPosition(start.item,start.item.startGrid);
List<Grid> grids = new List<Grid>();
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);
}
}
foreach (var item in itemObjs)
{
item.Reset();
}
}
public void GetMatrixIndex(ItemObj[,] itemObjs,ItemObj item,List<StartGrid> 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<T> RandomSortList<T>(List<T> ListT)
{
Random random = new Random();
List<T> newList = new List<T>();
foreach (T item in ListT)
{
newList.Insert(random.Next(newList.Count + 1), item);
}
return newList;
}
2024-10-24 16:54:32 +08:00
public void RestGrid(List<Grid> grids)
{
foreach (var grid in grids)
{
grid.item = null;
2024-10-23 17:55:55 +08:00
}
}
2024-11-04 17:29:11 +08:00
public void InsUnLock()
{
// int index = Random.Range(0, unLockGrids.Count);
// var unLock = Instantiate(unLockGrids[index].gameObject,unLockTrans);
}
2024-10-23 17:55:55 +08:00
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);
}
2024-11-04 17:29:11 +08:00
/// <summary>
/// 设置道具位置
/// </summary>
/// <param name="itemObj"></param>
/// <param name="grid"></param>
2024-10-23 17:55:55 +08:00
public void SetPosition(ItemObj itemObj, Grid grid)
{
itemObj.transform.position = grid.transform.position + itemObj.dev;
2024-10-24 16:54:32 +08:00
itemObj.type = grid.Type;
}
2024-11-04 17:29:11 +08:00
/// <summary>
/// 刷新层级
/// </summary>
public void ResetItemOrder()
{
foreach (var item in itemObjs)
{
item.ResetOrder();
}
}
2024-11-04 17:29:11 +08:00
/// <summary>
/// 打包道具
/// </summary>
2024-10-24 16:54:32 +08:00
public void PackItem()
{
foreach (var item in itemObjs)
{
if (item.type==GridType.box)
{
RestGrid(item._grids);
2024-10-29 15:22:32 +08:00
item.transform.position = packPosition.position;
//item.transform.DOMove(packPosition.position, 0.2f);
}
}
}
public void ItemLevelUp(ItemObj item0,ItemObj item1)
{
if (item0 == item1) return;
if (!item0.LevelUp(item1)) return;
foreach (var grid in item1._grids)
2024-10-29 15:22:32 +08:00
{
grid.Pop();
2024-10-24 16:54:32 +08:00
}
itemObjs.Remove(item1);
Destroy(item1.gameObject);
2024-11-04 17:29:11 +08:00
item0.Reset();
2024-10-23 17:55:55 +08:00
}
// 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;
2024-11-04 17:29:11 +08:00
}
[Serializable]
public class RandomItemData
{
public int num;
public ItemObj item;
}
[Serializable]
public class StartGrid
{
public int x;
public int y;
public ItemObj item;
}
[Serializable]
public class RandomData
{
public List<StartGrid> _list = new List<StartGrid>();
2024-10-23 17:55:55 +08:00
}