137 lines
2.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-29 15:22:32 +08:00
using Spine.Unity;
2024-10-23 17:55:55 +08:00
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;
2024-10-29 15:22:32 +08:00
public SkeletonAnimation Spine => spine;
[SerializeField] private SkeletonAnimation spine;
public bool unLock => gridLock;
[SerializeField] private bool gridLock = true;
2024-10-23 17:55:55 +08:00
public ItemObj item;
2024-10-29 15:22:32 +08:00
public Stack<ItemObj> stackItem=new Stack<ItemObj>();
2024-10-23 17:55:55 +08:00
public void SetData(int lx,int ly)
{
gX = lx;
gY = ly;
}
public void SetLock(bool gLock)
{
gridLock = gLock;
if (gLock)
{
spine.gameObject.SetActive(true);
}
else
{
spine.gameObject.SetActive(false);
}
}
2024-10-29 15:22:32 +08:00
public void SaveItemObj(ItemObj itemObj)
{
stackItem.Push(itemObj);
}
public ItemObj itemObj()
{
RemoveNullStack();
if (stackItem.Count==0)
{
return null;
}
2024-10-29 15:22:32 +08:00
return stackItem.Peek();
}
public void RemoveNullStack()
{
if (stackItem.Count>0&&stackItem.Peek()==null)
{
stackItem.Pop();
RemoveNullStack();
}
}
public ItemObj Pop()
2024-10-29 15:22:32 +08:00
{
return stackItem.Pop();
}
public void PlayEnterAnimation()
{
if (spine.AnimationName!="play_1")
{
spine.AnimationState.SetAnimation(0, "play_1", false);
}
}
public void PlayExitAnimation()
{
if (spine.AnimationName!="play_2")
{
spine.AnimationState.SetAnimation(0, "play_2", false);
}
}
2024-10-23 17:55:55 +08:00
private void OnMouseEnter()
{
GameManager.ins.stopGrid = this;
}
private void OnMouseExit()
{
if (GameManager.ins.stopGrid==this)
{
GameManager.ins.stopGrid = null;
}
}
public ItemObj GetItemObj()
{
return Type switch
{
GridType.box => item,
GridType.wait => itemObj(),
_ => throw new ArgumentOutOfRangeException()
};
}
2024-10-23 17:55:55 +08:00
private void OnMouseDown()
{
switch (Type)
{
case GridType.box:
if (item!=null&& item.CanSelected())
{
GameManager.ins.seletObj = item;
}
break;
case GridType.wait:
if (itemObj()!=null && itemObj().CanSelected())
{
GameManager.ins.seletObj = itemObj();
}
break;
default:
throw new ArgumentOutOfRangeException();
}
2024-10-23 17:55:55 +08:00
}
}
public enum GridType
{
box,
wait
}