WXGame/box1/Assets/Script/ItemSystem.cs

644 lines
18 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-11-08 18:21:41 +08:00
public List<ItemObj> LoadItemObjs = new List<ItemObj>();
private Dictionary<string, ItemObj> itemObjDic=new Dictionary<string, ItemObj>();
2024-12-28 15:00:14 +08:00
private List<RandomItemData> levelItemDatas=new List<RandomItemData>();
2024-11-21 09:35:48 +08:00
2024-11-26 15:44:50 +08:00
public float moveTime
2024-11-21 09:35:48 +08:00
{
get;
private set;
}
2024-11-26 15:44:50 +08:00
public float boxMoveTime
2024-11-21 09:35:48 +08:00
{
get;
private set;
}
2024-11-26 15:44:50 +08:00
public float speed
{
get;
private set;
}
public float boxSpeed
{
get;
private set;
}
public float points
{
get;
private set;
}
public float boxPoints
2024-11-21 09:35:48 +08:00
{
get;
private set;
}
2024-10-23 17:55:55 +08:00
private void Awake()
{
ins = this;
2024-11-08 18:21:41 +08:00
ResetDic();
2024-12-18 11:12:52 +08:00
ResetData();
}
public void ResetData()
{
2024-11-26 15:44:50 +08:00
moveTime = 10;
speed = 100;
2024-11-21 09:35:48 +08:00
points = 0;
2024-11-26 15:44:50 +08:00
boxMoveTime = 10;
boxSpeed = 100;
boxPoints = 0;
2024-11-08 18:21:41 +08:00
}
private void ResetDic()
{
foreach (var item in LoadItemObjs)
{
itemObjDic.Add(item.ID,item);
}
2024-10-23 17:55:55 +08:00
}
2024-11-21 09:35:48 +08:00
public ItemObj itemObj(string key)
{
return itemObjDic[key];
}
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-29 21:37:01 +08:00
ResetWaitGrid();
2024-10-24 16:54:32 +08:00
}
2024-11-05 18:15:49 +08:00
/// <summary>
/// 开启随机
/// </summary>
/// <param name="list"></param>
2024-11-29 21:37:01 +08:00
public IEnumerator RandomItem(List<RandomItemData> list,Action action)
2024-11-04 17:29:11 +08:00
{
2024-11-21 09:35:48 +08:00
BoxDebug.Log("开始随机道具位置");
2024-11-04 17:29:11 +08:00
List<ItemObj> objs = new List<ItemObj>();
2024-12-28 15:00:14 +08:00
levelItemDatas = list;
2024-11-04 17:29:11 +08:00
foreach (var randomItem in list)
{
for (int i = 0; i < randomItem.num; i++)
{
var obj = Instantiate(randomItem.item.gameObject, transform);
2024-11-21 09:35:48 +08:00
obj.GetComponent<ItemObj>().SetLevel(randomItem.level,randomItem.itemID);
2024-11-04 17:29:11 +08:00
objs.Add(obj.GetComponent<ItemObj>());
2024-11-29 21:37:01 +08:00
yield return null;
2024-11-04 17:29:11 +08:00
}
}
var randomData = RandomSortList(objs);
ResetPool(randomData);
2024-12-18 11:12:52 +08:00
ResetData();
ResetPropattributes();
2024-11-29 21:37:01 +08:00
action.Invoke();
2024-11-04 17:29:11 +08:00
}
2024-12-28 15:00:14 +08:00
public IEnumerator AddNewItem()
{
List<ItemObj> objs = new List<ItemObj>();
foreach (var randomItem in levelItemDatas)
{
for (int i = 0; i < randomItem.num; i++)
{
var obj = Instantiate(randomItem.item.gameObject, transform);
obj.GetComponent<ItemObj>().SetLevel(randomItem.level,randomItem.itemID);
objs.Add(obj.GetComponent<ItemObj>());
yield return null;
}
}
var randomData = RandomSortList(objs);
BoxDebug.Log("开始地块放置");
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 randomData)
{
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);
}
}
CoordinateRandom(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<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);
}
}
ResetWaitGrid();
ResetItemOrder();
}
2024-11-05 18:15:49 +08:00
/// <summary>
/// 放置道具
/// </summary>
/// <param name="ListT"></param>
2024-11-04 17:29:11 +08:00
public void ResetPool(List<ItemObj> ListT)
{
2024-11-21 09:35:48 +08:00
BoxDebug.Log("开始地块放置");
2024-11-04 17:29:11 +08:00
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);
}
}
2024-11-05 18:15:49 +08:00
CoordinateRandom(list);
if (itemObjs.Count>0)//清理预制体
2024-11-04 17:29:11 +08:00
{
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];
2024-11-26 15:44:50 +08:00
start.item.orderIndexSet = (list.Count - i)*2;
2024-11-04 17:29:11 +08:00
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);
}
}
2024-11-26 15:44:50 +08:00
ResetWaitGrid();
}
2024-11-05 18:15:49 +08:00
2024-11-26 15:44:50 +08:00
public void ResetWaitGrid()
{
2024-11-05 18:15:49 +08:00
foreach (var grid in MapManager.ins.WaitList)
{
2024-11-26 15:44:50 +08:00
grid.RemoveNullStack();
2024-11-05 18:15:49 +08:00
grid.ResetWaitBack();
}
2024-11-26 15:44:50 +08:00
foreach (var item in itemObjs)
{
item.Reset();
}
2024-11-04 17:29:11 +08:00
}
2024-12-28 15:00:14 +08:00
public bool WaitGridIsNull()
{
foreach (var grid in MapManager.ins.WaitList)
{
if (grid.itemObj()!=null)
{
return false;
}
}
return true;
}
2024-11-26 15:44:50 +08:00
2024-11-05 18:15:49 +08:00
private void CoordinateRandom(List<List<StartGrid>> 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;
}
/// <summary>
/// 放置道具
/// </summary>
/// <param name="itemObjs"></param>
/// <param name="item"></param>
/// <param name="grids"></param>
/// <param name="bo"></param>
2024-11-04 17:29:11 +08:00
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;
}
2024-11-05 18:15:49 +08:00
/// <summary>
/// 判断是否可以放置
/// </summary>
/// <param name="itemObjs"></param>
/// <param name="item"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
2024-11-04 17:29:11 +08:00
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)
{
2024-11-21 09:35:48 +08:00
BoxDebug.Log("随机道具排序");
2024-11-04 17:29:11 +08:00
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]);
2024-11-29 21:37:01 +08:00
MapManager.ins.WaitGrids[i, j].SaveItemObj(item);
2024-10-23 17:55:55 +08:00
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
item.SetOccGrid(grids);
2024-11-29 21:37:01 +08:00
ResetWaitGrid();
2024-10-23 17:55:55 +08:00
}
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()
2024-11-29 21:37:01 +08:00
{
StartCoroutine(ItemOrder());
}
private IEnumerator ItemOrder()
{
foreach (var item in itemObjs)
{
item.ResetOrder();
2024-11-29 21:37:01 +08:00
yield return null;
}
}
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);
}
}
2024-11-29 21:37:01 +08:00
GameManager.ins.ResetPropattributes();
2024-10-29 15:22:32 +08:00
}
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
{
2024-11-21 09:35:48 +08:00
grid.Remove(item1);
2024-10-24 16:54:32 +08:00
}
itemObjs.Remove(item1);
Destroy(item1.gameObject);
2024-11-26 15:44:50 +08:00
GameManager.ins.soundSystem.SynthesisAudio();
2024-11-04 17:29:11 +08:00
item0.Reset();
2024-10-23 17:55:55 +08:00
}
2024-11-21 09:35:48 +08:00
public void ResetPropattributes()
{
2024-11-26 15:44:50 +08:00
float lsMoveTime = 10;
float lsSpeed = 100;
float lsPoint = 0;
float lsBoxMoveTime = 10;
float lsBoxSpeed = 100;
float lsBoxPoint = 0;
List<ItemObj> list = new List<ItemObj>();
2024-11-21 09:35:48 +08:00
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;
}
}
}
2024-11-26 15:44:50 +08:00
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;
}
}
}
2024-11-21 09:35:48 +08:00
moveTime = lsMoveTime;
speed = lsSpeed;
points = lsPoint;
2024-11-26 15:44:50 +08:00
boxMoveTime = lsBoxMoveTime;
boxSpeed = lsBoxSpeed;
boxPoints = lsBoxPoint;
2024-11-21 09:35:48 +08:00
}
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;
2024-11-21 09:35:48 +08:00
public int level;
public int itemID;
2024-11-04 17:29:11 +08:00
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
}