CTT/Unity/Assets/Hotfix/Logic/Model/Game/Entity/NPC/NPCComponent.cs

100 lines
2.9 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using ET;
using Cal.DataTable;
using System;
using System.Collections.Generic;
2021-04-08 20:09:59 +08:00
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)
2021-04-08 20:09:59 +08:00
{
var nPCBaseList = GetList(sceneId, mapLayer);
if (nPCBaseList != null && nPCBaseList.Count > 0)
{
2021-04-11 19:50:39 +08:00
foreach (NPCBase npcBase in nPCBaseList)
2021-04-08 20:09:59 +08:00
{
await CreateNPC(zoneScene,npcBase);
2021-04-08 20:09:59 +08:00
}
}
M2C_GetTaskState ret = await zoneScene.GetComponent<SessionComponent>().Call<M2C_GetTaskState>(new C2M_GetTaskState());
2021-04-08 20:09:59 +08:00
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)
2021-04-08 20:09:59 +08:00
{
//!创建NPC
int id = (int)npcBase.Id;
int prefabId = npcBase.PrefabId;
Unit npc = await UnitFactory.CreateNPC(zoneScene,prefabId, id);
2021-04-08 20:09:59 +08:00
npc.Position = new Vector3(npcBase.PosX, npcBase.PosY, PosHelper.NPCPos_Z);
_NPCDic.Add(id, npc);
}
public void ChangeNPCTaskState(List<NPCTask> nPCStateList)
{
2021-04-11 19:50:39 +08:00
foreach (NPCTask item in nPCStateList)
2021-04-08 20:09:59 +08:00
{
ChangeNPCTaskState(item);
}
}
public void ChangeNPCTaskState(NPCTask npcTask)
{
if (npcTask == null)
{
Log.Info($"nPCState == null");
return;
}
2021-04-11 19:50:39 +08:00
if (!_NPCDic.TryGetValue(npcTask.Id, out Unit unit))
2021-04-08 20:09:59 +08:00
{
return;
}
Game.EventSystem.Publish(new ET.EventType.UpdateNPCTaskState
{
unit = unit,
taskState = npcTask.TaskState
}).Coroutine();
}
public void RemoveAll()
{
_NPCDic.Clear();
}
}
}