using System;
|
using System.Collections.Generic;
|
using Snxxz.UI;
|
using UnityEngine;
|
using UnityEngine.Events;
|
|
public class GA_NpcClientCollect : GA_NpcClientFunc
|
{
|
public static UnityAction<uint> OnCollectFinished;
|
private static List<uint> m_ArrivedList = new List<uint>();
|
public static event UnityAction<uint, int> OnArrive;
|
public static event UnityAction<uint, int> OnLeave;
|
|
private bool m_IsMainWinOpen = false;
|
|
protected override void OnInit(GameNetPackBasic package)
|
{
|
base.OnInit(package);
|
|
WindowCenter.Instance.windowAfterOpenEvent += CheckOpenCollectIcon;
|
WindowCenter.Instance.windowAfterCloseEvent += AfterCloseMainWin;
|
|
NPCInteractProcessor.s_NpcInteractEvent += HandleCallback;
|
}
|
|
private void HandleCallback(E_NpcType type, int npcID, uint sid)
|
{
|
if (sid == ServerInstID)
|
{
|
OnClick();
|
}
|
}
|
|
protected override void OnUnit()
|
{
|
base.OnUnit();
|
|
if (m_ArrivedList.Contains(ServerInstID))
|
{
|
m_ArrivedList.Remove(ServerInstID);
|
}
|
|
if (OnLeave != null)
|
{
|
OnLeave(ServerInstID, NpcConfig.NPCID);
|
}
|
|
WindowCenter.Instance.windowAfterOpenEvent -= CheckOpenCollectIcon;
|
WindowCenter.Instance.windowAfterCloseEvent -= AfterCloseMainWin;
|
}
|
|
private void CheckOpenCollectIcon(Window obj)
|
{
|
if (obj is MainInterfaceWin)
|
{
|
m_IsMainWinOpen = true;
|
|
if (NpcConfig.NPCID != GeneralDefine.GatherSoulDZ)
|
{
|
GA_Hero _hero = PlayerDatas.Instance.hero;
|
|
if (_hero != null)
|
{
|
float _distSqrt = MathUtility.DistanceSqrtXZ(_hero.Pos, Pos);
|
|
if (_distSqrt < Mathf.Pow(NpcConfig.ModelRadius + 0.4f + GeneralDefine.CloseNpcDist, 2))
|
{
|
Arrive();
|
}
|
}
|
}
|
}
|
}
|
|
private void AfterCloseMainWin(Window obj)
|
{
|
if (obj is MainInterfaceWin)
|
{
|
m_IsMainWinOpen = false;
|
Leave();
|
}
|
}
|
|
public override void OnClick()
|
{
|
if (GA_Hero.s_MapSwitching)
|
{
|
return;
|
}
|
|
GA_Hero _hero = PlayerDatas.Instance.hero;
|
|
if (_hero == null)
|
{
|
return;
|
}
|
|
_hero.LockTarget = this;
|
_hero.SelectTarget = this;
|
|
float _chkDistSqrt = MathUtility.DistanceSqrtXZ(_hero.Pos, Pos);
|
|
// 这里判断是否要走向此对象
|
if (_chkDistSqrt > 2f)
|
{
|
_hero.MoveToPosition(Pos, 1f);
|
}
|
|
if (_chkDistSqrt < 4)
|
{
|
var _model = ModelCenter.Instance.GetModel<HazyGrassModel>();
|
if (_model.CanCollectClientNpc(NpcConfig.NPCID))
|
{
|
StartCollect();
|
}
|
else
|
{
|
_model.DisplayCollectErrorTip();
|
}
|
}
|
}
|
|
private bool m_StartCollect;
|
private float m_CollectTime;
|
|
public void StartCollect()
|
{
|
if (m_StartCollect)
|
{
|
return;
|
}
|
m_CollectTime = 0;
|
NormalCollectWin.s_CollectInfo = new PrepareHandler.ClientH0812()
|
{
|
PlayerID = PlayerDatas.Instance.PlayerId,
|
PrepareState = (byte)PrepareHandler.E_PrepareType.pstMissionCollecting,
|
MaxTime = 2000,
|
PrepareID = NpcConfig.NPCID,
|
};
|
PrepareHandler.Instance.isPreparing = true;
|
WindowCenter.Instance.Open<NormalCollectWin>();
|
m_StartCollect = true;
|
}
|
|
public void StopCollect()
|
{
|
PrepareHandler.Instance.isPreparing = false;
|
WindowCenter.Instance.Close<NormalCollectWin>();
|
m_StartCollect = false;
|
}
|
|
protected override void OnUpdate()
|
{
|
if (m_StartCollect)
|
{
|
GA_Hero m_Hero = PlayerDatas.Instance.hero;
|
|
m_CollectTime += Time.deltaTime;
|
|
if (m_CollectTime > 2f)
|
{
|
StopCollect();
|
|
if (m_Hero != null)
|
{
|
if (m_Hero.LockTarget == this)
|
{
|
m_Hero.LockTarget = null;
|
}
|
if (m_Hero.SelectTarget == this)
|
{
|
m_Hero.SelectTarget = null;
|
}
|
}
|
|
if (belongEventID != -1)
|
{
|
ClientSceneManager.Instance.NpcDead(belongEventID, this, NpcConfig.NPCID);
|
}
|
else
|
{
|
if (OnCollectFinished != null)
|
{
|
OnCollectFinished(ServerInstID);
|
}
|
}
|
|
GAMgr.Instance.Release(this);
|
}
|
|
if (!m_Hero.IsIdle() && !m_Hero.IsCollect())
|
{
|
StopCollect();
|
}
|
}
|
}
|
|
public override void OnSelect()
|
{
|
if (NpcConfig.NPCID != 10204200)
|
{
|
SelectionManager.Request(SelectionManager.E_Type.Green, this, NpcConfig.ModelRadius * 2);
|
}
|
|
GA_Hero _hero = PlayerDatas.Instance.hero;
|
Vector3 _forward = MathUtility.ForwardXZ(Pos, _hero.Pos);
|
|
_hero.Forward = _forward;
|
|
if (NpcConfig.AutomaticFace == 1)
|
{
|
_forward = MathUtility.ForwardXZ(_hero.Pos, Pos);
|
Forward = _forward;
|
}
|
}
|
public override void OnUnSelect()
|
{
|
base.OnUnSelect();
|
if (OnLeave != null)
|
{
|
OnLeave(ServerInstID, NpcConfig.NPCID);
|
}
|
}
|
|
protected override void OnFixedUpdate()
|
{
|
base.OnFixedUpdate();
|
|
if (!m_IsMainWinOpen)
|
{
|
m_IsMainWinOpen = WindowCenter.Instance.IsOpen<MainInterfaceWin>();
|
return;
|
}
|
|
GA_Hero _hero = PlayerDatas.Instance.hero;
|
|
if (_hero != null)
|
{
|
float _distSqrt = MathUtility.DistanceSqrtXZ(_hero.Pos, Pos);
|
|
if (_distSqrt < Mathf.Pow(NpcConfig.ModelRadius + 0.4f + GeneralDefine.CloseNpcDist, 2))
|
{
|
Arrive();
|
}
|
else
|
{
|
if (m_ArrivedList.Contains(ServerInstID))
|
{
|
if (OnLeave != null)
|
{
|
OnLeave(ServerInstID, NpcConfig.NPCID);
|
}
|
m_ArrivedList.Remove(ServerInstID);
|
}
|
}
|
}
|
}
|
|
public void Arrive()
|
{
|
if (OnArrive != null)
|
{
|
OnArrive(ServerInstID, NpcConfig.NPCID);
|
}
|
}
|
|
public void Leave()
|
{
|
if (OnLeave != null)
|
{
|
OnLeave(ServerInstID, NpcConfig.NPCID);
|
}
|
}
|
}
|