zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Hotfix/Logic/Model/Game/Entity/NPC/NPCComponent.cs

100 lines
2.9 KiB
C#

using ET;
using Cal.DataTable;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace ET
{
public class NPCComponentAwakeSystem : AwakeSystem<NPCComponent>
{
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<int, Unit> _NPCDic = new Dictionary<int, Unit>();
public void Awake()
{
_NPCInfoDic.Clear();
var arr = DataTableHelper.GetAll<NPCBase>();
foreach (NPCBase item in arr)
{
_NPCInfoDic.Add((item.SceneId, item.MapLayer), item);
}
}
public List<NPCBase> GetList(int sceneId, int mapLayer)
{
return _NPCInfoDic[(sceneId, mapLayer)];
}
public async ETVoid ShowNPC(Scene zoneScene, int sceneId, int mapLayer)
{
var nPCBaseList = GetList(sceneId, mapLayer);
if (nPCBaseList != null && nPCBaseList.Count > 0)
{
foreach (NPCBase npcBase in nPCBaseList)
{
await CreateNPC(zoneScene,npcBase);
}
}
M2C_GetTaskState ret = await zoneScene.GetComponent<SessionComponent>().Call<M2C_GetTaskState>(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(Scene zoneScene, NPCBase npcBase)
{
//!创建NPC
int id = (int)npcBase.Id;
int prefabId = npcBase.PrefabId;
Unit npc = await UnitFactory.CreateNPC(zoneScene,prefabId, id);
npc.Position = new Vector3(npcBase.PosX, npcBase.PosY, PosHelper.NPCPos_Z);
_NPCDic.Add(id, npc);
}
public void ChangeNPCTaskState(List<NPCTask> 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 RemoveAll()
{
_NPCDic.Clear();
}
}
}