2025-04-24 17:03:28 +08:00

166 lines
4.5 KiB
JavaScript

import GameModel from "../ts/core/GameModel";
import SKDataUtil from "../ts/gear_2.3.4/util/SKDataUtil";
import SKUIUtil from "../ts/gear_2.3.4/util/SKUIUtil";
import { ELivingType } from "../ts/core/EEnum";
let CPubFunction = require('./PubFunction');
class CNpcMgr {
constructor() {
this.mapNpcConfig = {};
this.mapNpcObj = {};
}
LoadNpcJson() {
let conf = GameModel.conf_npc;
for (let key in conf) {
if (key == 'datatype') {
continue;
}
let stData = GameModel.conf_npc[key];
this.mapNpcConfig[key] = { nConfigID: key, nResID: stData.resid, strName: stData.name, stTalk: stData.talk, mapButton: this.GetMapButton(stData.mapButton), nKind: stData.kind, stAutoCreate: this.GetCreate(stData.auto_create) };
}
}
AddNpcObj(nOnlyID, goObj) {
if (SKDataUtil.hasProperty(this.mapNpcObj, nOnlyID)) {
let temp = this.mapNpcObj[nOnlyID];
if (temp) {
let valid = SKUIUtil.isValid(temp);
if (valid) {
temp.destroy();
}
}
}
this.mapNpcObj[nOnlyID] = goObj;
}
CheckNpcRes(onlyid, resid) {
for (let ponlyid in this.mapNpcObj) {
if (ponlyid == onlyid) {
continue;
}
let npcnode = this.mapNpcObj[ponlyid];
if (npcnode.resid == resid) {
return true;
}
}
return false;
}
GetNpcDefaultDir(nConfigID) {
if (!SKDataUtil.hasProperty(this.mapNpcConfig, nConfigID)) {
return 0;
}
let pAutoCreate = this.mapNpcConfig[nConfigID].stAutoCreate;
if (null == pAutoCreate) {
return 0;
}
return pAutoCreate.dir;
}
GetMapButton(strJson) {
if (strJson == "") {
return JSON.parse('{}');
}
let mapButton = SKDataUtil.jsonBy(strJson);
return mapButton;
}
GetCreate(strData) {
if (typeof strData == "undefined") {
return null;
}
let vecTmp = strData.split(";");
if (vecTmp.length != 4) {
return null;
}
return { map: parseInt(vecTmp[0]), x: parseInt(vecTmp[1]), y: parseInt(vecTmp[2]), dir: parseInt(vecTmp[3]) };
}
GetCurSceneNode() {
let nodMap = cc.find('Canvas/MapUI');
return nodMap;
}
GetCurGameMapID() {
let goScene = this.GetCurSceneNode();
if (!goScene) {
return null;
}
return goScene.getComponent('GameMapLogic').mapId;
}
ReloadNpcJson() {
if (CPubFunction.GetMapLen(this.mapNpcConfig) == 0) {
this.LoadNpcJson();
}
SKUIUtil.destroyDict(this.mapNpcObj);
}
GetNpcConfigInfo(nConfigID) {
if (!SKDataUtil.hasProperty(this.mapNpcConfig, nConfigID)) {
return null;
}
return this.mapNpcConfig[nConfigID];
}
FindNpcByConfigID(nConfigID) {
let mapPlayer = this.mapNpcObj;
for (let key in mapPlayer) {
let node = mapPlayer[key];
if (!node) {
continue;
}
let valid = SKUIUtil.isValid(node);
if (!valid) {
continue;
}
if (node.livingtype != ELivingType.NPC) {
continue;
}
let npc = node.getComponent('Npc');
if (npc.nConfigID != nConfigID) {
continue;
}
return node;
}
return null;
}
GetNpcPos(nConfigID) {
for (let key in this.mapNpcConfig) {
if (this.mapNpcConfig[key].nConfigID == nConfigID) {
let stCreate = this.mapNpcConfig[key].stAutoCreate;
if (!stCreate) {
continue;
}
return { nNpc: nConfigID, nMap: stCreate.map, nX: stCreate.x, nY: stCreate.y, nDir: stCreate.dir };
}
}
return null;
}
DestroyNpc(onlyid) {
let node = this.mapNpcObj[onlyid];
if (node) {
let valid = SKUIUtil.isValid(node);
if (valid) {
let npc = node.getComponent('Npc');
if (npc) {
npc.clear();
}
node.destroy();
}
}
delete this.mapNpcObj[onlyid];
}
}
let g_NpcMgr = null;
module.exports = (() => {
if (g_NpcMgr == null) {
g_NpcMgr = new CNpcMgr();
}
return g_NpcMgr;
})();