using ET; using Cal.DataTable; using System; using System.Collections.Generic; using UnityEngine; namespace ET { public class NPCComponentAwakeSystem : AwakeSystem { public override void Awake(NPCComponent self) { self.Awake(); } } public class NPCComponent : Entity { private UnOrderMultiMap<(int, int), NPCBase> _NPCInfoDic = new UnOrderMultiMap<(int, int), NPCBase>(); private Dictionary _NPCDic = new Dictionary(); public void Awake() { _NPCInfoDic.Clear(); var arr = DataTableHelper.GetAll(); foreach (NPCBase item in arr) { _NPCInfoDic.Add((item.SceneId, item.MapLayer), item); } } public List GetList(int sceneId, int mapLayer) { return _NPCInfoDic[(sceneId, mapLayer)]; } public async ETVoid ShowNPC(int sceneId, int mapLayer) { var nPCBaseList = GetList(sceneId, mapLayer); if (nPCBaseList != null && nPCBaseList.Count > 0) { foreach (NPCBase npcBase in nPCBaseList) { await CreateNPC(npcBase); } } M2C_GetTaskState ret = await SessionComponent.Instance.Call(new C2M_GetTaskState()); if (!ret.Message.IsNullOrEmpty()) { Game.EventSystem.Publish(new ET.EventType.ShowTipUI { tip = ret.Message }).Coroutine(); return; } ChangeNPCTaskState(ret.NPCStateList); } private async ETTask CreateNPC(NPCBase npcBase) { //!创建NPC int id = (int)npcBase.Id; int prefabId = npcBase.PrefabId; Unit npc = await UnitFactory.CreateNPC(prefabId, id); npc.Position = new Vector3(npcBase.PosX, npcBase.PosY, PosHelper.NPCPos_Z); _NPCDic.Add(id, npc); } public void ChangeNPCTaskState(List nPCStateList) { foreach (NPCTask item in nPCStateList) { ChangeNPCTaskState(item); } } public void ChangeNPCTaskState(NPCTask npcTask) { if (npcTask == null) { Log.Info($"nPCState == null"); return; } if (!_NPCDic.TryGetValue(npcTask.Id, out Unit unit)) { return; } Game.EventSystem.Publish(new ET.EventType.UpdateNPCTaskState { unit = unit, taskState = npcTask.TaskState }).Coroutine(); } public void RemoveNPC(Unit unit) { UnitComponent.Instance.Remove(unit); } public void RemoveAll() { _NPCDic.Clear(); } } }