refactor: update turn start triggers in desert sample
Fixes logic in `defendNext` to ensure effects are applied after buff updates by calling `next()` before mutation. Also updates indentation and formatting to match project style guidelines.
This commit is contained in:
@@ -4,85 +4,89 @@ import { EffectData } from "@/samples/slay-the-spire-like/system/types";
|
||||
import getEffects from "../effect.csv";
|
||||
|
||||
export function addTurnStartTriggers(triggers: Triggers) {
|
||||
const effects = getEffects();
|
||||
const effects = getEffects();
|
||||
|
||||
function findEffect(id: string): EffectData {
|
||||
const found = effects.find(e => e.id === id);
|
||||
if (found) return found;
|
||||
return { id, name: id, description: "", lifecycle: "instant" } as EffectData;
|
||||
function findEffect(id: string): EffectData {
|
||||
const found = effects.find((e) => e.id === id);
|
||||
if (found) return found;
|
||||
return {
|
||||
id,
|
||||
name: id,
|
||||
description: "",
|
||||
lifecycle: "instant",
|
||||
} as EffectData;
|
||||
}
|
||||
|
||||
// discard: random discard at turn start
|
||||
triggers.onTurnStart.use(async (ctx, next) => {
|
||||
if (ctx.entityKey !== "player") {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
// discard: random discard at turn start
|
||||
triggers.onTurnStart.use(async (ctx, next) => {
|
||||
if (ctx.entityKey !== "player") {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
const discard = ctx.game.value.player.effects.discard;
|
||||
if (discard && discard.stacks > 0) {
|
||||
const handIds = [...ctx.game.value.player.deck.regions.hand.childIds];
|
||||
if (handIds.length > 0) {
|
||||
const randomIndex = ctx.game.rng.nextInt(handIds.length);
|
||||
const randomCardId = handIds[randomIndex];
|
||||
await triggers.onCardDiscarded.execute(ctx.game, {
|
||||
cardId: randomCardId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const discard = ctx.game.value.player.effects.discard;
|
||||
if (discard && discard.stacks > 0) {
|
||||
const handIds = [...ctx.game.value.player.deck.regions.hand.childIds];
|
||||
if (handIds.length > 0) {
|
||||
const randomIndex = ctx.game.rng.nextInt(handIds.length);
|
||||
const randomCardId = handIds[randomIndex];
|
||||
await triggers.onCardDiscarded.execute(ctx.game, { cardId: randomCardId });
|
||||
}
|
||||
}
|
||||
await next();
|
||||
});
|
||||
|
||||
await next();
|
||||
// defendNext: gain block next turn
|
||||
// record how many defendNext there is, before buff update
|
||||
// then apply after buff update
|
||||
triggers.onTurnStart.use(async (ctx, next) => {
|
||||
const defendNext = ctx.game.value.player.effects.defendNext;
|
||||
if (!defendNext || defendNext.stacks <= 0) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
await next();
|
||||
await ctx.game.produceAsync((draft) => {
|
||||
addEntityEffect(draft.player, findEffect("defend"), defendNext.stacks);
|
||||
});
|
||||
});
|
||||
|
||||
// defendNext: gain block next turn
|
||||
triggers.onTurnStart.use(async (ctx, next) => {
|
||||
if (ctx.entityKey !== "player") {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
// energyNext: gain energy next turn
|
||||
triggers.onTurnStart.use(async (ctx, next) => {
|
||||
if (ctx.entityKey !== "player") {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
const defendNext = ctx.game.value.player.effects.defendNext;
|
||||
if (defendNext && defendNext.stacks > 0) {
|
||||
await ctx.game.produceAsync(draft => {
|
||||
addEntityEffect(draft.player, findEffect("defend"), defendNext.stacks);
|
||||
addEntityEffect(draft.player, defendNext.data, -defendNext.stacks);
|
||||
});
|
||||
}
|
||||
const energyNext = ctx.game.value.player.effects.energyNext;
|
||||
if (energyNext && energyNext.stacks > 0) {
|
||||
await ctx.game.produceAsync((draft) => {
|
||||
draft.player.energy += energyNext.stacks;
|
||||
addEntityEffect(draft.player, energyNext.data, -energyNext.stacks);
|
||||
});
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
await next();
|
||||
});
|
||||
|
||||
// energyNext: gain energy next turn
|
||||
triggers.onTurnStart.use(async (ctx, next) => {
|
||||
if (ctx.entityKey !== "player") {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
// drawNext: draw extra cards next turn
|
||||
triggers.onTurnStart.use(async (ctx, next) => {
|
||||
if (ctx.entityKey !== "player") {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
const energyNext = ctx.game.value.player.effects.energyNext;
|
||||
if (energyNext && energyNext.stacks > 0) {
|
||||
await ctx.game.produceAsync(draft => {
|
||||
draft.player.energy += energyNext.stacks;
|
||||
addEntityEffect(draft.player, energyNext.data, -energyNext.stacks);
|
||||
});
|
||||
}
|
||||
const drawNext = ctx.game.value.player.effects.drawNext;
|
||||
if (drawNext && drawNext.stacks > 0) {
|
||||
await ctx.game.produceAsync((draft) => {
|
||||
addEntityEffect(draft.player, drawNext.data, -drawNext.stacks);
|
||||
});
|
||||
await triggers.onDraw.execute(ctx.game, { count: drawNext.stacks });
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
|
||||
// drawNext: draw extra cards next turn
|
||||
triggers.onTurnStart.use(async (ctx, next) => {
|
||||
if (ctx.entityKey !== "player") {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
const drawNext = ctx.game.value.player.effects.drawNext;
|
||||
if (drawNext && drawNext.stacks > 0) {
|
||||
await ctx.game.produceAsync(draft => {
|
||||
addEntityEffect(draft.player, drawNext.data, -drawNext.stacks);
|
||||
});
|
||||
await triggers.onDraw.execute(ctx.game, { count: drawNext.stacks });
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
await next();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user