bc2b92a4bc
# Conflicts: # Blacksmith/UserSettings/Layouts/default-2022.dwlt # box1/Assets/Scenes/SampleScene.unity # box1/Assets/Script/UI/MainPanel.cs # box1/UserSettings/Layouts/default-2022.dwlt
644 lines
18 KiB
C#
644 lines
18 KiB
C#
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<UnLockGrid> unLockGrids;
|
|
[SerializeField] public Transform unLockTrans;
|
|
public Transform packPosition;
|
|
public List<ItemObj> itemObjs=new List<ItemObj>();
|
|
public List<ItemObj> LoadItemObjs = new List<ItemObj>();
|
|
private Dictionary<string, ItemObj> itemObjDic=new Dictionary<string, ItemObj>();
|
|
private List<RandomItemData> levelItemDatas=new List<RandomItemData>();
|
|
|
|
|
|
|
|
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();
|
|
ResetData();
|
|
}
|
|
|
|
public void ResetData()
|
|
{
|
|
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];
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化物品列表
|
|
/// </summary>
|
|
/// <param name="itemDatas"></param>
|
|
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;
|
|
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();
|
|
}
|
|
/// <summary>
|
|
/// 开启随机
|
|
/// </summary>
|
|
/// <param name="list"></param>
|
|
public IEnumerator RandomItem(List<RandomItemData> list,Action action)
|
|
{
|
|
BoxDebug.Log("开始随机道具位置");
|
|
List<ItemObj> objs = new List<ItemObj>();
|
|
levelItemDatas = list;
|
|
foreach (var randomItem in list)
|
|
{
|
|
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);
|
|
ResetPool(randomData);
|
|
ResetData();
|
|
ResetPropattributes();
|
|
action.Invoke();
|
|
}
|
|
|
|
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();
|
|
}
|
|
/// <summary>
|
|
/// 放置道具
|
|
/// </summary>
|
|
/// <param name="ListT"></param>
|
|
public void ResetPool(List<ItemObj> ListT)
|
|
{
|
|
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 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);
|
|
}
|
|
}
|
|
|
|
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<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];
|
|
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();
|
|
}
|
|
|
|
public void ResetWaitGrid()
|
|
{
|
|
foreach (var grid in MapManager.ins.WaitList)
|
|
{
|
|
grid.RemoveNullStack();
|
|
grid.ResetWaitBack();
|
|
}
|
|
|
|
foreach (var item in itemObjs)
|
|
{
|
|
item.Reset();
|
|
}
|
|
}
|
|
|
|
public bool WaitGridIsNull()
|
|
{
|
|
foreach (var grid in MapManager.ins.WaitList)
|
|
{
|
|
if (grid.itemObj()!=null)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
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>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// 判断是否可以放置
|
|
/// </summary>
|
|
/// <param name="itemObjs"></param>
|
|
/// <param name="item"></param>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
/// <returns></returns>
|
|
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)
|
|
{
|
|
BoxDebug.Log("随机道具排序");
|
|
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;
|
|
}
|
|
public void RestGrid(List<Grid> 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<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].SaveItemObj(item);
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
}
|
|
item.SetOccGrid(grids);
|
|
ResetWaitGrid();
|
|
}
|
|
/// <summary>
|
|
/// 设置道具位置
|
|
/// </summary>
|
|
/// <param name="itemObj"></param>
|
|
/// <param name="grid"></param>
|
|
public void SetPosition(ItemObj itemObj, Grid grid)
|
|
{
|
|
itemObj.transform.position = grid.transform.position + itemObj.dev;
|
|
itemObj.type = grid.Type;
|
|
}
|
|
/// <summary>
|
|
/// 刷新层级
|
|
/// </summary>
|
|
public void ResetItemOrder()
|
|
{
|
|
StartCoroutine(ItemOrder());
|
|
}
|
|
|
|
private IEnumerator ItemOrder()
|
|
{
|
|
foreach (var item in itemObjs)
|
|
{
|
|
item.ResetOrder();
|
|
yield return null;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 打包道具
|
|
/// </summary>
|
|
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<ItemObj> list = new List<ItemObj>();
|
|
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<StartGrid> _list = new List<StartGrid>();
|
|
} |