public void initNPC() { List<NpcConfig> list = (List<NpcConfig>) baseService.listAll(NpcConfig.class); if (list == null || list.isEmpty()) { LOGGER.error("NPC数据为空"); return; } if (npcs == null) { npcs = new HashMap<Integer, Npc>(list.size()); handleNpcSet = Collections.synchronizedSet(new HashSet<Npc>(5)); } CopyOnWriteArrayList<Integer> branchs = channelFacade.getCurrentBranching(); for (NpcConfig config : list) { if (config.getMapId() == null) { LOGGER.error("构建NPC数据,基础NPC:{},地图ID为空,请检查配置", config.getId()); continue; } Npc npc = Npc.valueOf(config); Npc exist = this.npcs.put(npc.getId(), npc); if (exist != null) { LOGGER.error( "重复npc1[id:{}, mapId:{}],npc2[id:{}, mapId:{}],请检查配置", new Object[] {config.getId(), config.getMapId(), exist.getId(), exist.getMapId()}); } for (int branching : branchs) { GameMap gameMap = gameMapManager.getGameMapById(npc.getMapId(), branching); if (gameMap == null) { continue; } UnitId unitId = UnitId.valueOf(config.getId(), ElementType.NPC); gameMap.enterMap(unitId, config.getBornX(), config.getBornY()); } } if (LOGGER.isDebugEnabled()) { LOGGER.debug("NPC数据构建完毕,一共存在[{}]个NPC", this.npcs.size()); } }
/** * 处理采集显示和隐藏 * * @param gameMap * @param npc * @param view * @param hideTime, 隐藏时间 (毫秒) */ public void handleCollect(GameMap gameMap, Npc npc, boolean view, int hideTime) { if (npc == null || gameMap == null) { return; } int x = npc.getBornX(); int y = npc.getBornY(); GameScreen gameScreen = gameMap.getGameScreen(x, y); if (gameScreen == null) { return; } Collection<ISpire> spireCollection = gameScreen.getSpireCollection(ElementType.NPC); ISpire target = null; int npcId = npc.getId(); for (ISpire npcSpire : spireCollection) { if (npcSpire.getId() == npcId) { target = npcSpire; break; } } if (target == null) { return; } Set<ISpire> spires = gameMap.getCanViewsSpireCollection(x, y, ElementType.PLAYER); if (spires != null) { for (ISpire spire : spires) { UserDomain userDomain = (UserDomain) spire; if (view) { npc.view(gameMap); userDomain.putCanViewSpire(target); } else if (hideTime > 0) { npc.hide(gameMap, hideTime); synchronized (handleNpcSet) { handleNpcSet.add(npc); } userDomain.putHideSpire(target); } } worldPusherHelper.putMessage2Queue(spires); } }