2024-11-29 21:37:01 +08:00

223 lines
5.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Spine.Unity;
using UnityEngine;
using UnityEngine.Serialization;
/// <summary>
/// 格子数据
/// </summary>
public class Grid : MonoBehaviour
{
public GridType Type;
public int X => gX;
[SerializeField] private int gX;
public int Y => gY;
[SerializeField] private int gY;
public SkeletonAnimation Spine => spine;
[SerializeField] private SkeletonAnimation spine;
public bool unLock => gridLock;
[SerializeField] private bool gridLock = true;
public ItemObj item;
public List<ItemObj> stackItem = new List<ItemObj>();
[SerializeField] private GameObject back;
[SerializeField] private GameObject backGrid;
public override string ToString()
{
return "{ "
+ "X:" + X + ","
+ "Y:" + Y + ","
+ "}";
}
public void SetData(int lx,int ly)
{
gX = lx;
gY = ly;
}
public void SetLock(bool gLock)
{
gridLock = gLock;
back.SetActive(false);
backGrid.SetActive(false);
if (gLock)
{
spine.gameObject.SetActive(true);
}
else
{
spine.gameObject.SetActive(false);
}
}
public void SaveItemObj(ItemObj itemObj)
{
stackItem.Add(itemObj);
}
public ItemObj itemObj()
{
RemoveNullStack();
if (stackItem.Count==0)
{
return null;
}
return stackItem[^1];
}
public void RemoveNullStack()
{
for (int i = stackItem.Count-1; i >=0 ; i--)
{
if (stackItem[i]==null)
{
stackItem.Remove(stackItem[i]);
}
}
}
public ItemObj Pop()
{
return stackItem[^1];
}
public void Remove(ItemObj obj)
{
stackItem.Remove(obj);
}
public void PlayEnterAnimation()
{
if (spine.AnimationName!="play_1")
{
spine.AnimationState.SetAnimation(0, "play_1", false);
}
}
public void PlayExitAnimation()
{
BoxDebug.Log(X+"-"+Y);
if (spine.AnimationName!="play_2")
{
spine.AnimationState.SetAnimation(0, "play_2", false);
}
}
public void ResetBack(int index)
{
if (!gridLock)
{
switch (index)
{
case 0:
back.SetActive(true);
backGrid.SetActive(false);
break;
case 1:
back.SetActive(false);
backGrid.SetActive(true);
break;
case 2:
back.SetActive(false);
backGrid.SetActive(false);
break;
}
}
}
public void ResetWaitBack()
{
if (Type==GridType.wait)
{
if (stackItem.Count>1||(itemObj()!=null&&!itemObj().CanSelected()))
{
MaterialPropertyBlock mats = new MaterialPropertyBlock();
mats.SetColor("_VeColor",Color.gray);
spine.GetComponent<MeshRenderer>().SetPropertyBlock(mats);
back.SetActive(false);
backGrid.SetActive(false);
}
else
{
MaterialPropertyBlock mats = new MaterialPropertyBlock();
mats.SetColor("_VeColor",Color.white);
spine.GetComponent<MeshRenderer>().SetPropertyBlock(mats);
back.SetActive(false);
backGrid.SetActive(false);
}
if (itemObj()!=null)
{
spine.GetComponent<MeshRenderer>().sortingOrder=itemObj().orderIndexSet-1;
}
else
{
spine.GetComponent<MeshRenderer>().sortingOrder = 0;
}
}
}
public void ResetWaitWait()
{
MaterialPropertyBlock mats = new MaterialPropertyBlock();
mats.SetColor("_VeColor",Color.white);
spine.GetComponent<MeshRenderer>().SetPropertyBlock(mats);
back.SetActive(false);
backGrid.SetActive(false);
}
private void OnMouseEnter()
{
// GameManager.ins.stopGrid = this;
}
private void OnMouseExit()
{
// if (GameManager.ins.stopGrid==this)
// {
// GameManager.ins.stopGrid = null;
// }
}
/// <summary>
/// 返回区块资源
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public ItemObj GetItemObj()
{
return Type switch
{
GridType.box => item,
GridType.wait => itemObj(),
_ => throw new ArgumentOutOfRangeException()
};
}
private void OnMouseDown()
{
switch (Type)
{
case GridType.box:
if (item!=null&& item.CanSelected())
{
GameManager.ins.seletObj = item;
GameManager.ins.seletObj.SetOrder(100);
}
break;
case GridType.wait:
if (itemObj()!=null && itemObj().CanSelected())
{
GameManager.ins.seletObj = itemObj();
GameManager.ins.seletObj.SetOrder(100);
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public enum GridType
{
box,
wait
}