using vnxbqy.UI; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class ClientHazyGrassStage : DungeonStage { static readonly Vector3 PlayerBornPosition1 = new Vector3(19.401f, 4.985f, 5.494f); static List s_NpcPositions = new List(); static List s_RandomIndexs = new List(); static readonly int s_EnemyNpcId = 40208005; static Vector3 s_EnemyPosition = Vector3.zero; bool hasInitializedNpc = false; public bool hasCompleted { get; private set; } static Clock s_CollectClock = null; static Dictionary s_CollectNpcs = new Dictionary(); static Dictionary s_Sid2NpcIds = new Dictionary(); static Dictionary s_Sid2NpcIndexs = new Dictionary(); static Dictionary s_NpcCounts = new Dictionary(); static GA_NpcClientCollect autoNpc = null; HazyRegionIncidentType incidentType; HazyGrassModel model { get { return ModelCenter.Instance.GetModel(); } } HazyRegionModel hazyRegionModel { get { return ModelCenter.Instance.GetModel(); } } DungeonModel dungeonModel { get { return ModelCenter.Instance.GetModel(); } } PackModel packModel { get { return ModelCenter.Instance.GetModel(); } } public override void Initialize() { base.Initialize(); s_RandomIndexs.Clear(); s_Sid2NpcIds.Clear(); s_CollectNpcs.Clear(); s_Sid2NpcIndexs.Clear(); s_NpcCounts.Clear(); localNpcJsons.Clear(); UnloadAllNpc(); hasInitializedNpc = false; hasCompleted = false; ClientCollectUtility.OnCollectFinished += OnCollectFinished; dungeonModel.onDungeonResultEvent += OnDungeonResultEvent; } protected override void OnStageLoadFinish() { base.OnStageLoadFinish(); InitialPlayer(); InitializeNpc(); var npcId = TryGetScarceNpc(); TryGotoCloserNpc(npcId); model.onMapNpcCountRefresh += OnMapNpcCountRefresh; } public override void UnInitialize() { base.UnInitialize(); if (s_CollectClock != null) { Clock.Stop(s_CollectClock); s_CollectClock = null; } UnloadAllNpc(); ClientCollectUtility.OnCollectFinished -= OnCollectFinished; dungeonModel.onDungeonResultEvent -= OnDungeonResultEvent; model.onMapNpcCountRefresh -= OnMapNpcCountRefresh; } private void OnMapNpcCountRefresh() { if (!hasInitializedNpc) { InitializeNpc(); var npcId = TryGetScarceNpc(); TryGotoCloserNpc(npcId); } } private void OnDungeonResultEvent() { dungeonModel.UpdateCoolDown(DungeonCoolDownType.LeaveMap, 10 * 1000); hasCompleted = true; } private float m_TimeCalc = 0; protected override void OnUpdate() { base.OnUpdate(); if (hazyRegionModel.auto) { var _hero = PlayerDatas.Instance.hero; if (_hero.IsIdle()) { if (_hero != null) { m_TimeCalc += Time.deltaTime; if (m_TimeCalc > 1) { var dungeon = ClientDungeonStageUtility.dungeonInfo; var hintId = dungeonModel.GetDungeonHintId(dungeon.mapId, dungeon.lineId); var dungeonHintConfig = DungeonHintConfig.Get(hintId); if (dungeonHintConfig != null) { var collectCount = 0; for (int i = 0; i < dungeonHintConfig.NPC1ID.Length; i++) { collectCount += dungeonModel.GetDugneonNpcCollectCount(dungeonHintConfig.NPC1ID[i]); } if (dungeonHintConfig.targetValue1.Length > 0 && dungeonHintConfig.targetValue1[0] > 0) { if (collectCount < dungeonHintConfig.targetValue1[0]) { var npcId = TryGetScarceNpc(); TryGotoCloserNpc(npcId); m_TimeCalc = 0; } } } } } } else { m_TimeCalc = 0; } } } private void OnCollectFinished(uint _sid) { if (s_Sid2NpcIds.ContainsKey(_sid)) { var npcId = s_Sid2NpcIds[_sid]; var pak = new CA234_tagCMGetCustomSceneCollectAward(); pak.NPCID = (uint)npcId; GameNetSystem.Instance.SendInfo(pak); s_Sid2NpcIds.Remove(_sid); if (s_Sid2NpcIndexs.ContainsKey(_sid)) { var index = s_Sid2NpcIndexs[_sid]; localNpcJsons.RemoveAll((x) => { return x.npcId == npcId && x.index == index; }); s_Sid2NpcIndexs.Remove(_sid); SaveNpcData(); } if (s_NpcCounts.ContainsKey(npcId)) { s_NpcCounts[npcId] = Mathf.Max(0, s_NpcCounts[npcId] - 1); } } if (s_CollectNpcs.ContainsKey(_sid)) { s_CollectNpcs.Remove(_sid); } if (s_CollectClock != null) { Clock.Stop(s_CollectClock); s_CollectClock = null; } s_CollectClock = Clock.AlarmAfter(0.5f, () => { var dungeon = ClientDungeonStageUtility.dungeonInfo; var hintId = dungeonModel.GetDungeonHintId(dungeon.mapId, dungeon.lineId); var dungeonHintConfig = DungeonHintConfig.Get(hintId); if (dungeonHintConfig != null) { var collectCount = 0; for (int i = 0; i < dungeonHintConfig.NPC1ID.Length; i++) { collectCount += dungeonModel.GetDugneonNpcCollectCount(dungeonHintConfig.NPC1ID[i]); } if (dungeonHintConfig.targetValue1.Length > 0 && dungeonHintConfig.targetValue1[0] > 0) { if (collectCount < dungeonHintConfig.targetValue1[0]) { var npcId = TryGetScarceNpc(); TryGotoCloserNpc(npcId); } } } }); } void InitialPlayer() { var hero = PlayerDatas.Instance.hero; if (hero != null) { hero.Pos = PlayerBornPosition1; hero.Rotation = Quaternion.Euler(0, -9.537001f, 0); CameraController.Instance.Apply(); } } /// /// /// /// 指定NpcId void TryGotoCloserNpc(int npcId = 0) { var hero = PlayerDatas.Instance.hero; if (hero == null) { return; } if (s_CollectNpcs.Count == 0) { return; } var minDis = float.MaxValue; autoNpc = null; foreach (var npc in s_CollectNpcs.Values) { if (npcId != 0 && npc.NpcConfig.NPCID != npcId) { continue; } var dis = MathUtility.CalDistance(hero.Pos, npc.Pos); if (minDis > dis) { minDis = dis; autoNpc = npc; } } if (autoNpc != null) { hero.OnPathFindStop -= OnHeroFindStop; hero.OnPathFindStop += OnHeroFindStop; MapTransferUtility.Instance.MoveToNPC(autoNpc.NpcConfig.NPCID, (int)autoNpc.ServerInstID); } } int TryGetScarceNpc() { if (ClientDungeonStageUtility.clientMapId == HazyGrassModel.REIKI_CLIENTDATAMAP) { return 0; } List npcIds = new List(); var dungeon = ClientDungeonStageUtility.dungeonInfo; var hintId = dungeonModel.GetDungeonHintId(dungeon.mapId, dungeon.lineId); var dungeonHintConfig = DungeonHintConfig.Get(hintId); if (dungeonHintConfig != null) { npcIds.AddRange(dungeonHintConfig.NPC1ID); } if (npcIds.Count > 0) { npcIds.Sort((lhs, rhs) => { var lhs_item = model.GetGrassNpcItemId(lhs); var rhs_item = model.GetGrassNpcItemId(rhs); var lhs_npcCount = model.GetMapNpcCount(lhs); var rhs_npcCount = model.GetMapNpcCount(rhs); var lhs_empty = lhs_npcCount == 0; var rhs_empty = rhs_npcCount == 0; if (lhs_empty != rhs_empty) { return lhs_empty.CompareTo(rhs_empty); } var lhs_itemCount = packModel.GetItemCountByID(PackType.Item, lhs_item); var rhs_itemCount = packModel.GetItemCountByID(PackType.Item, rhs_item); if (lhs_itemCount != rhs_itemCount) { return lhs_itemCount.CompareTo(rhs_itemCount); } return lhs_item.CompareTo(rhs_item); }); return npcIds[0]; } return 0; } private void OnHeroFindStop() { var hero = PlayerDatas.Instance.hero; if (hero == null) { return; } hero.OnPathFindStop -= OnHeroFindStop; if (PlayerDatas.Instance.baseData.LV >= model.autoCollectLevel) { if (autoNpc != null) { float _chkDistSqrt = MathUtility.DistanceSqrtXZ(hero.Pos, autoNpc.Pos); float _chkDist = GeneralDefine.CloseNpcDist + autoNpc.NpcConfig.ModelRadius + 0.3f; if (_chkDistSqrt <= Mathf.Pow(_chkDist, 2)) { hero.SelectTarget = autoNpc; ClientCollectUtility.HandleCallback(E_NpcType.Collect, autoNpc.NpcConfig.NPCID, autoNpc.ServerInstID); } } } } void InitializeNpc() { var config = ScriptableObjectLoader.LoadSoHazyMapNpc(ClientDungeonStageUtility.clientMapId); s_NpcPositions = config.GetNpcPositions(hazyRegionModel.hazyRegionOpenTimes); s_RandomIndexs.Clear(); for (int i = 0; i < s_NpcPositions.Count; i++) { s_RandomIndexs.Add(i); } var incidentId = hazyRegionModel.GetIncidentId(ClientDungeonStageUtility.dungeonInfo.mapId, ClientDungeonStageUtility.dungeonInfo.lineId); var mapId = model.GetGrassMapId(incidentId); var npcInfos = model.GetGrassNpcInfos(mapId); var npcJsonDatas = GetLocalNpcData(); if (npcJsonDatas != null && npcJsonDatas.Count > 0) { foreach (var npcId in npcInfos) { var count = model.GetMapNpcCount(npcId); if (count == 0) { continue; } for (int j = 0; j < count; j++) { var index = -1; var jsonInfoIndex = npcJsonDatas.FindIndex((x) => { return x.npcId == npcId; }); if (jsonInfoIndex != -1) { index = npcJsonDatas[jsonInfoIndex].index; npcJsonDatas.RemoveAt(jsonInfoIndex); } if (index == -1) { break; } InitializeNpc(npcId, index); s_RandomIndexs.Remove(index); } } } foreach (var npcId in npcInfos) { var count = model.GetMapNpcCount(npcId) - (s_NpcCounts.ContainsKey(npcId) ? s_NpcCounts[npcId] : 0); for (int j = 0; j < count; j++) { if (s_RandomIndexs.Count == 0) { DebugEx.LogError("配置的Npc位置数量与剩余Npc数量不一致"); break; } var index = UnityEngine.Random.Range(0, s_RandomIndexs.Count); InitializeNpc(npcId, s_RandomIndexs[index]); s_RandomIndexs.RemoveAt(index); } } if (hasInitializedNpc) { //s_EnemyPosition = s_NpcPositions[UnityEngine.Random.Range(0, s_NpcPositions.Count)]; } if (hasInitializedNpc) { SaveNpcData(); } } void InitializeNpc(int npcId, int index) { hasInitializedNpc = true; var position = s_NpcPositions[index]; GA_NpcClientCollect _npc = GAMgr.Instance.ReqClntNoFightNpc((uint)npcId, E_ActorGroup.FuncNpc); _npc.Pos = position; s_CollectNpcs[_npc.ServerInstID] = _npc; s_Sid2NpcIds[_npc.ServerInstID] = npcId; s_Sid2NpcIndexs[_npc.ServerInstID] = index; if (!s_NpcCounts.ContainsKey(npcId)) { s_NpcCounts.Add(npcId, 1); } else { s_NpcCounts[npcId] += 1; } localNpcJsons.Add(new NpcJsonInfo() { npcId = npcId, index = index, }); } void UnloadAllNpc() { foreach (var _npc in s_CollectNpcs.Values) { if (_npc != null) { _npc.KillerServerInstID = PlayerDatas.Instance.PlayerId; _npc.ActorInfo.serverDie = true; GAMgr.Instance.ServerDie(_npc.ServerInstID); GAMgr.Instance.Release(_npc); } } s_CollectNpcs.Clear(); } #region 本地Npc数据 List localNpcJsons = new List(); public struct NpcJsonInfo { public int npcId; public int index; } void SaveNpcData() { LocalSave.SetString(model.GetLocalSaveKey(ClientDungeonStageUtility.clientMapId), LitJson.JsonMapper.ToJson(localNpcJsons)); } List GetLocalNpcData() { var key = model.GetLocalSaveKey(ClientDungeonStageUtility.clientMapId); if (LocalSave.HasKey(key)) { return LitJson.JsonMapper.ToObject>(LocalSave.GetString(key)); } return null; } #endregion #if UNITY_EDITOR private void OnGUI() { if (GUILayout.Button("Exit")) { ClientDungeonStageUtility.ExitNormalClientDungeon(); } } #endif }