xy-server/game/core/NpcMgr.ts
2025-04-23 09:34:08 +08:00

143 lines
3.9 KiB
TypeScript

import NpcConfigMgr from "./NpcConfigMgr";
import Npc from "./Npc";
import { ENpcCreater } from "../role/EEnum";
export default class NpcMgr {
static shared = new NpcMgr();
mapNpc: { [key: number]: Npc };
constructor() {
this.mapNpc = {};
}
InitNpcByMapId(mapid: any, nFuBenID: any) {
for (let it in NpcConfigMgr.shared.mapConfig) {
let stData = NpcConfigMgr.shared.mapConfig[it];
if (stData.auto_create == null)
continue;
if (stData.auto_create.map == mapid) {
this.CreateNpc(it, stData.auto_create.map, stData.auto_create.x, stData.auto_create.y, {
nKind: 0,
nID: 0
}, nFuBenID);
}
}
}
launch() {
for (let it in NpcConfigMgr.shared.mapConfig) {
let stData = NpcConfigMgr.shared.mapConfig[it];
if (stData.auto_create == null)
continue;
this.CreateNpc(it, stData.auto_create.map, stData.auto_create.x, stData.auto_create.y, {
nKind: 0,
nID: 0
}, 0)
}
}
deletePlayersNpc(roleId: number) {
for (let it in this.mapNpc) {
let stCreater = this.mapNpc[it].stCreater;
if (stCreater.nKind != ENpcCreater.PLAYER || stCreater.nID != roleId) {
continue;
}
this.mapNpc[it].destroy();
delete this.mapNpc[it];
}
}
deleteTeamsNpc(nTeamID: any) {
for (let it in this.mapNpc) {
let stCreater = this.mapNpc[it].stCreater;
if (stCreater.nKind != ENpcCreater.TEAM || stCreater.nID != nTeamID) {
continue;
}
this.mapNpc[it].destroy();
delete this.mapNpc[it];
}
}
GetMaxID(): number {
let nCurMax: any = 0;
for (let it in this.mapNpc) {
if (Number(it) > nCurMax)
nCurMax = Number(it);
}
return Math.max(nCurMax, 1000000);
}
CreateNpc(nNpc: any, nMap: any, nX: number, nY: number, stCreater = { nKind: 0, nID: 0 }, nFuBenID = 0, nlevel: number = 0, type: number = 0): any {
let npc = new Npc(nNpc);
npc.x = nX;
npc.y = nY;
npc.mapid = nMap;
npc.stCreater = stCreater;
npc.nFuBenID = nFuBenID;
npc.level = nlevel;
npc.type = type;
this.mapNpc[npc.onlyid] = npc;
npc.onEnterGame();
return npc.onlyid;
}
// 查找NPC
findNpc(onlyId: any): Npc {
let result: Npc = this.mapNpc[onlyId];
return result;
}
// 根据配置索引查找NPC
getNpcByConfigID(confId: number): Npc {
for (let key in this.mapNpc) {
let item: Npc = this.mapNpc[key];
if (item.configid == confId) {
return item;
}
}
return null;
}
CheckAndDeleteNpc(nOnlyID: any, pPlayer: any) {
let pNpc = this.findNpc(nOnlyID);
if (null == pNpc)
return;
if (pNpc.stCreater.nKind == ENpcCreater.SYSTEM)
return;
// if (pPlayer.CanPlayerSeeNpc(pNpc))
if ((pNpc.stCreater.nKind == ENpcCreater.TEAM && pPlayer.isleader) || (pNpc.stCreater.nKind == ENpcCreater.PLAYER && pNpc.stCreater.nID == pPlayer.roleid))
this.DeleteNpc(nOnlyID);
}
DeleteNpc(nOnlyID: any) {
if (this.mapNpc.hasOwnProperty(nOnlyID) == false)
return;
this.mapNpc[nOnlyID].destroy();
delete this.mapNpc[nOnlyID];
}
deleteTeamsNpc2(nTeamID: any) {
let vecNpc = [];
for (var it in this.mapNpc) {
let pNpc = this.mapNpc[it];
if (pNpc.stCreater.nKind == ENpcCreater.TEAM && pNpc.stCreater.nID == nTeamID) {
vecNpc.push(it);
}
}
for (var it2 in vecNpc) {
this.DeleteNpc(vecNpc[it2]);
}
}
}