2024-11-21 09:35:48 +08:00

161 lines
3.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Spine.Unity;
// using Unity.VisualScripting;
using UnityEngine;
public class ItemObj : MonoBehaviour
{
public ItemSize itemSize => size;
public GridType type = GridType.wait;
public string ID => id;
[SerializeField] private string id = "";
[SerializeField] private int maxLevel = 4;
[SerializeField] private string itemName;
public int Level => level;//等级
[SerializeField] private int level = 0;
[SerializeField] private ItemSize size;//物体大小
[SerializeField] public List<Grid> _grids;
[SerializeField] private SkeletonAnimation spine;
public Grid startGrid;//开始坐标
public Vector3 dev;
public Vector3 mouseDev;
public int orderIndexSet = 1;
public int itemID = 0;
public void SetOccGrid(List<Grid> _list)
{
_grids = _list;
}
public bool LevelUp(ItemObj itemObj)
{
if (!CanSelected())
{
return false;
}
if (itemObj.ID!=ID)
{
return false;
}
if (itemObj.Level!=Level)
{
return false;
}
if (level + 1 >= maxLevel) return false;
level++;
var itemData = JsonTab.Instance.tables.Prop.Get(itemID);
itemID = itemData.NextLevelID;
ResetSkin();
ResetAni();
GameManager.ins.ResetPropattributes();
return true;
}
/// <summary>
/// 设置等级和id
/// </summary>
/// <param name="saveLevel"></param>
/// <param name="saveItemID"></param>
public void SetLevel(int saveLevel,int saveItemID)
{
level = saveLevel;
itemID = saveItemID;
ResetSkin();
ResetAni();
}
public void ResetSkin()
{
spine.Skeleton.SetSkin(itemName+"/"+itemName+"_"+(level+1));
spine.Skeleton.SetSlotsToSetupPose();
}
/// <summary>
/// 层级计算
/// </summary>
/// <returns></returns>
public int OrderIndex()
{
int order = 0;
foreach (var grid in _grids)
{
if (order<grid.stackItem.Count)
{
order = grid.stackItem.Count;
}
}
return order;
}
private void ResetAni()
{
spine.state.SetAnimation(0, "a_1", false);
}
public void Reset()
{
if (CanSelected())
{
MaterialPropertyBlock mats = new MaterialPropertyBlock();
mats.SetColor("_VeColor",Color.white);
spine.GetComponent<MeshRenderer>().SetPropertyBlock(mats);
}
else
{
MaterialPropertyBlock mats = new MaterialPropertyBlock();
mats.SetColor("_VeColor",Color.gray);
spine.GetComponent<MeshRenderer>().SetPropertyBlock(mats);
}
}
public void ResetOrder()
{
if (_grids.Any(grid => grid.GetItemObj() != this))
{
spine.GetComponent<MeshRenderer>().sortingOrder = orderIndexSet;
return;
}
spine.GetComponent<MeshRenderer>().sortingOrder = orderIndexSet;
}
public bool CanSelected()
{
foreach (var grid in _grids)
{
if (grid.GetItemObj() != this)
{
return false;
}
}
return true;
}
private void Awake()
{
}
// Start is called before the first frame update
void Start()
{
ResetSkin();
}
// Update is called once per frame
void Update()
{
}
}
[Serializable]
public class ItemSize
{
public int x = 1;
public int y = 1;
}