本篇介绍对铲子功能的实现,主要功能为点击铲子后进入想要铲除植物状态,如果所选网格有植物进行植物的铲除。如果点击右键进行铲除状态的取消,以及将铲子放回功能。
首先需要进行对铲子状态的判断,即是否在使用铲子,没有使用中则可以点击铲子进行选择。
public bool IsShove
{
get { return isShove; }
set {
isShove = value;
// 需要铲植物
if (isShove)
{
AudioManager.Instance.PlayEFAudio(GameManager.Instance.GameConf.Shovel);
shoveImg.localRotation = Quaternion.Euler(0, 0, 45);
}
// 把铲子放回去
else
{
AudioManager.Instance.PlayEFAudio(GameManager.Instance.GameConf.Shovel);
shoveImg.localRotation = Quaternion.Euler(0, 0, 0);
shoveImg.transform.position = transform.position;
}
}
}
当进行铲除功能实现时,要判断当前网格是否有植物,以及植物与铲子的距离是否小于一定距离,当满足有植物并且距离小于一定距离时,单机左键可以将植物铲除,通过调用植物基类的方法将植物放入缓存池中,并将当前网格状态置为空实现对植物的铲除。
如果想要取消铲除状态,则点击右键,将铲子状态设为不在使用。
void Update()
{
// 如果需要铲植物
if (IsShove)
{
Vector3 mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
shoveImg.transform.position = new Vector3(mousePoint.x, mousePoint.y, 0);
// 点击左键,判断是否要铲除植物
if (Input.GetMouseButtonDown(0))
{
Grid grid= GridManager.Instance.GetGridByMouse();
// 如果没有植物,直接跳过所有逻辑
if (grid.CurrPlantBase == null) return;
// 如果鼠标距离网格的距离小于1.5米,则杀死这个植物
if (Vector2.Distance(Camera.main.ScreenToWorldPoint(Input.mousePosition),grid.CurrPlantBase.transform.position)<1.5f)
{
grid.CurrPlantBase.Dead();
IsShove = false;
}
}
// 点击右键,取消铲子状态
if (Input.GetMouseButtonDown(1))
{
IsShove = false;
}
}
}