using Snxxz.UI;
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
public class ClientHazyGrassStage : DungeonStage
|
{
|
static readonly Vector3 PlayerBornPosition1 = new Vector3(17.25f, 5.12f, 3.70f);
|
static readonly Vector3 PlayerBornPosition2 = new Vector3(17.25f, 5.12f, 3.70f);
|
|
static List<HazyMapNpcScriptableObject.NpcInfo> s_NpcInfos = new List<HazyMapNpcScriptableObject.NpcInfo>();
|
static Dictionary<Vector3, GA_NpcClientCollect> s_CollectNpcs = new Dictionary<Vector3, GA_NpcClientCollect>();
|
static Dictionary<uint, Vector3> s_Sid2NpcPos = new Dictionary<uint, Vector3>();
|
static Dictionary<uint, int> s_Sid2NpcIds = new Dictionary<uint, int>();
|
//static List<GA_NpcClientFightNorm> s_ClientFightNpcs = new List<GA_NpcClientFightNorm>();
|
static List<HazyGrassNpcInfo> s_NpcRefreshInfos = new List<HazyGrassNpcInfo>();
|
|
static Dictionary<int, int> s_NpcRefreshTimes = new Dictionary<int, int>();
|
|
bool mapLoadFinish = false;
|
bool initedFightNpc = false;
|
|
HazyRegionIncidentType incidentType;
|
|
HazyGrassModel model { get { return ModelCenter.Instance.GetModel<HazyGrassModel>(); } }
|
HazyRegionModel hazyRegionModel { get { return ModelCenter.Instance.GetModel<HazyRegionModel>(); } }
|
|
public override void Initialize()
|
{
|
base.Initialize();
|
|
s_NpcInfos.Clear();
|
s_Sid2NpcIds.Clear();
|
s_Sid2NpcPos.Clear();
|
s_NpcRefreshTimes.Clear();
|
|
mapLoadFinish = false;
|
initedFightNpc = false;
|
|
var config = HazyRegionConfig.Get(hazyRegionModel.processingIncidentId);
|
if (config != null)
|
{
|
incidentType = (HazyRegionIncidentType)config.incidentType;
|
s_NpcRefreshInfos = model.GetGrassNpcInfos(config.dungeonId);
|
foreach (var npcInfo in s_NpcRefreshInfos)
|
{
|
s_NpcRefreshTimes.Add(npcInfo.npcId, GetNpcRefreshCount(npcInfo));
|
}
|
}
|
|
UnloadAllNpc();
|
|
GA_NpcClientCollect.OnCollectFinished += OnCollectFinished;
|
}
|
|
protected override void OnStageLoadFinish()
|
{
|
base.OnStageLoadFinish();
|
|
mapLoadFinish = true;
|
|
InitialPlayer();
|
InitializeNpc();
|
|
initedFightNpc = true;
|
}
|
|
protected override void OnUpdate()
|
{
|
base.OnUpdate();
|
|
if (mapLoadFinish)
|
{
|
foreach (var npcInfo in s_NpcRefreshInfos)
|
{
|
var _lastCount = s_NpcRefreshTimes[npcInfo.npcId];
|
var count = GetNpcRefreshCount(npcInfo);
|
if (_lastCount != count)
|
{
|
RebornCollectedNpc(npcInfo.npcId);
|
s_NpcRefreshTimes[npcInfo.npcId] = count;
|
}
|
}
|
}
|
}
|
|
public override void UnInitialize()
|
{
|
base.UnInitialize();
|
|
UnloadAllNpc();
|
|
GA_NpcClientCollect.OnCollectFinished -= OnCollectFinished;
|
}
|
|
int GetNpcRefreshCount(HazyGrassNpcInfo npcInfo)
|
{
|
var used = TimeUtility.Minute * 60 + TimeUtility.Second;
|
var refreshSeconds = npcInfo.refreshMinute * 60;
|
return used / refreshSeconds;
|
}
|
|
private void OnCollectFinished(uint _sid)
|
{
|
if (s_Sid2NpcIds.ContainsKey(_sid))
|
{
|
var npcId = s_Sid2NpcIds[_sid];
|
Debug.Log("采集了Npc:--" + npcId);
|
var pak = new CA234_tagCMGetCustomSceneCollectAward();
|
pak.NPCID = (uint)npcId;
|
GameNetSystem.Instance.SendInfo(pak);
|
|
s_Sid2NpcIds.Remove(_sid);
|
}
|
|
if (s_Sid2NpcPos.ContainsKey(_sid))
|
{
|
var pos = s_Sid2NpcPos[_sid];
|
if (s_CollectNpcs.ContainsKey(pos))
|
{
|
s_CollectNpcs.Remove(pos);
|
}
|
}
|
|
RefreshMapNpcCount();
|
}
|
|
void InitializeNpc()
|
{
|
s_NpcInfos.Clear();
|
|
var config = ScriptableObjectLoader.LoadSoHazyMapNpc(ClientDungeonStageUtility.clientMapId);
|
var npcInfos = config.GetAllNpcInfos();
|
if (npcInfos != null)
|
{
|
s_NpcInfos.AddRange(npcInfos);
|
RebornCollectedNpc();
|
}
|
}
|
|
void InitialPlayer()
|
{
|
var hero = PlayerDatas.Instance.hero;
|
if (incidentType == HazyRegionIncidentType.ReikiGrass)
|
{
|
hero.Pos = PlayerBornPosition1;
|
}
|
else if (incidentType == HazyRegionIncidentType.FairyGrass)
|
{
|
hero.Pos = PlayerBornPosition2;
|
}
|
CameraController.Instance.Apply();
|
}
|
|
void RebornCollectedNpc(int npcId = 0)
|
{
|
foreach (var npcInfo in s_NpcInfos)
|
{
|
if (npcId != 0 && npcInfo.npcId != npcId)
|
{
|
continue;
|
}
|
switch (npcInfo.npcType)
|
{
|
case E_NpcType.Collect:
|
GA_NpcClientCollect _npc = null;
|
if (!s_CollectNpcs.TryGetValue(npcInfo.position, out _npc)
|
|| _npc == null || _npc.ActorInfo.serverDie)
|
{
|
_npc = GAMgr.Instance.ReqClntNoFightNpc<GA_NpcClientCollect>((uint)npcInfo.npcId,
|
E_ActorGroup.FuncNpc);
|
if (_npc != null)
|
{
|
_npc.Pos = npcInfo.position;
|
s_CollectNpcs[npcInfo.position] = _npc;
|
|
s_Sid2NpcIds[_npc.ServerInstID] = npcInfo.npcId;
|
s_Sid2NpcPos[_npc.ServerInstID] = npcInfo.position;
|
}
|
}
|
break;
|
case E_NpcType.Fight:
|
//if (!initedFightNpc)
|
//{
|
// var fightNpc = GAMgr.Instance.ReqClntFightNpc<GA_NpcClientFightNorm>((uint)npcInfo.npcId,
|
// E_ActorGroup.Enemy);
|
// if (fightNpc != null)
|
// {
|
// fightNpc.Pos = npcInfo.position;
|
// fightNpc.OnAttacked -= OnAttackNpc;
|
// fightNpc.OnAttacked += OnAttackNpc;
|
// }
|
// s_ClientFightNpcs.Add(fightNpc);
|
//}
|
break;
|
}
|
}
|
|
RefreshMapNpcCount();
|
}
|
|
void RefreshMapNpcCount()
|
{
|
foreach (var npcInfo in s_NpcRefreshInfos)
|
{
|
var count = 0;
|
foreach (var npcId in s_Sid2NpcIds.Values)
|
{
|
if (npcId == npcInfo.npcId)
|
{
|
count++;
|
}
|
}
|
model.RefreshMapNpcCount(npcInfo.npcId, count);
|
}
|
}
|
|
private void OnAttackNpc()
|
{
|
Debug.Log("攻击了宝箱怪");
|
}
|
|
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);
|
}
|
}
|
|
//foreach (var _npc in s_ClientFightNpcs)
|
//{
|
// if (_npc != null)
|
// {
|
// _npc.OnAttacked -= OnAttackNpc;
|
// _npc.ActorInfo.serverDie = true;
|
// GAMgr.Instance.ServerDie(_npc.ServerInstID);
|
// GAMgr.Instance.Release(_npc);
|
// }
|
//}
|
//
|
//s_ClientFightNpcs.Clear();
|
s_CollectNpcs.Clear();
|
}
|
|
#if UNITY_EDITOR
|
private void OnGUI()
|
{
|
if (GUILayout.Button("Exit"))
|
{
|
model.RequestExitClientDungeon();
|
}
|
}
|
#endif
|
}
|