feat: add effect applier with all core effect types

This commit is contained in:
2026-05-13 03:29:32 +00:00
parent 8ff8e8f522
commit b09683e7f4
2 changed files with 326 additions and 0 deletions
+126
View File
@@ -0,0 +1,126 @@
// ==================== 效果执行器 ====================
export function applyEffect(effect, state) {
if (!effect || typeof effect !== 'object') return;
const { type } = effect;
switch (type) {
case 'addMoney': {
if (typeof effect.value === 'number') {
state.money = (state.money || 0) + effect.value;
}
break;
}
case 'addStat': {
const stat = effect.stat;
if (stat && typeof effect.value === 'number') {
if (!state.stats) state.stats = {};
state.stats[stat] = (state.stats[stat] || 0) + effect.value;
}
break;
}
case 'setStat': {
const stat = effect.stat;
if (stat && typeof effect.value === 'number') {
if (!state.stats) state.stats = {};
state.stats[stat] = effect.value;
}
break;
}
case 'setFlag': {
const flag = effect.flag;
if (flag !== undefined) {
if (!state.worldFlags) state.worldFlags = {};
state.worldFlags[flag] = effect.value;
}
break;
}
case 'unlockCareer': {
const careerId = effect.careerId;
if (careerId) {
if (!state.careers) state.careers = {};
if (!state.careers[careerId]) {
state.careers[careerId] = { level: 0, exp: 0 };
}
}
break;
}
case 'addExp': {
const careerId = effect.careerId;
if (careerId && typeof effect.value === 'number') {
if (!state.careers) state.careers = {};
if (!state.careers[careerId]) {
state.careers[careerId] = { level: 0, exp: 0 };
}
state.careers[careerId].exp += effect.value;
}
break;
}
case 'addItem': {
const itemId = effect.itemId;
if (itemId) {
if (!state.shopItems) state.shopItems = {};
state.shopItems[itemId] = (state.shopItems[itemId] || 0) + 1;
}
break;
}
case 'addBuff': {
const buffId = effect.buffId;
if (buffId) {
if (!state.activeBuffs) state.activeBuffs = {};
const duration = effect.duration;
const expiresDay = duration === -1 ? -1 : (state.day || 0) + duration;
state.activeBuffs[buffId] = { expiresDay };
}
break;
}
case 'upgradeArtifact': {
const artifactId = effect.artifactId;
if (artifactId) {
if (!state.artifacts) state.artifacts = {};
state.artifacts[artifactId] = (state.artifacts[artifactId] || 0) + 1;
}
break;
}
case 'addLog': {
if (effect.text) {
if (!state.logs) state.logs = [];
const entry = {
day: state.day,
year: state.year,
age: state.age,
text: effect.text,
type: effect.logType || 'normal',
timestamp: Date.now(),
};
state.logs.unshift(entry);
if (state.logs.length > 500) {
state.logs.pop();
}
}
break;
}
case 'die': {
state.alive = false;
if (effect.reason) {
state.deathReason = effect.reason;
}
break;
}
default:
// Unknown effect type, ignore
break;
}
}