|
楼主 |
发表于 2025-9-11 15:40:12
|
显示全部楼层
::DUNGEON_DAMAGE_TABLE <- {
[2] = 1,
//副本ID] = 10, 副本减伤90%
}
// 配置加载函数
function loadDamageConfig() {
if (!("dungeonDamageConfigLoaded" in ::getroottable())) {
::dungeonDamageConfigLoaded <- false;
}
if (::dungeonDamageConfigLoaded) return;
try {
local config = sq_ReadJson(CONFIG_PATH);
if ("table" in config) {
::DUNGEON_DAMAGE_TABLE <- clone config.table;
::dungeonDamageConfigLoaded = true;
} else {
throw "Missing 'table' field";
}
} catch(e) {
::DUNGEON_DAMAGE_TABLE <- {};
::dungeonDamageConfigLoaded = true;
}
}
// 怪物防御应用函数
function applyMonsterDefense(monster) {
if (!monster) return;
loadDamageConfig();
local scriptPath = "shennai_948028091/damage_control/dungeon_damage_control.nut";
if (CNSquirrelAppendage.sq_IsAppendAppendage(monster, scriptPath))
return;
local appendage = CNSquirrelAppendage.sq_AppendAppendage(
monster,
null,
-1,
false,
scriptPath,
true
);
appendage.getVar("isDefenseApp").set_bool(true);
appendage.sq_AddFunctionName("onSetHp", "onMonsterHpChange");
}
// HP变化回调函数
function onMonsterHpChange(appendage, newHp, attacker) {
if (!appendage || !appendage.getVar("isDefenseApp").get_bool())
return newHp;
local monster = appendage.getParent();
if (!monster || newHp >= monster.getHp())
return newHp;
local dungeonId = getCurrentDungeonId();
local damageValue = (dungeonId in ::DUNGEON_DAMAGE_TABLE)
? ::DUNGEON_DAMAGE_TABLE[dungeonId]
: 100;
local damageRate = damageValue.tofloat() / 100.0;
if (damageRate < 0.0) damageRate = 0.0;
if (damageRate > 1.0) damageRate = 1.0;
local currentHp = monster.getHp();
local realDamage = (currentHp - newHp).tofloat() * damageRate;
local finalHp = currentHp - realDamage;
return (finalHp < 1 && currentHp > 1) ? 1 : finalHp.tointeger();
}
// 获取副本ID函数
function getCurrentDungeonId() {
local stage = sq_GetGlobalUdpModuleStage();
if (!stage) return -1;
local dungeon = sq_GetDungeonByStage(stage);
if (!dungeon) return -1;
return dungeon.getDungeonIndex();
}
// 怪物处理器
::monsterProcessor <- {
lastCheckTime = 0,
function process(obj) {
if (!obj || !sq_IsGame()) return;
local now = sq_GetGlobalTime();
if (now - lastCheckTime < 1000) return;
this.lastCheckTime = now;
local monsters = sq_GetMonsterPool(null);
foreach(monster in monsters) {
if (monster && monster.isObjectType(OBJECTTYPE_MONSTER)) {
applyMonsterDefense(monster);
}
}
}
} |
|