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:
parent
a5e2e4888e
commit
f4c96a4a72
|
|
@ -7,9 +7,14 @@ export function addTurnStartTriggers(triggers: Triggers) {
|
|||
const effects = getEffects();
|
||||
|
||||
function findEffect(id: string): EffectData {
|
||||
const found = effects.find(e => e.id === id);
|
||||
const found = effects.find((e) => e.id === id);
|
||||
if (found) return found;
|
||||
return { id, name: id, description: "", lifecycle: "instant" } as EffectData;
|
||||
return {
|
||||
id,
|
||||
name: id,
|
||||
description: "",
|
||||
lifecycle: "instant",
|
||||
} as EffectData;
|
||||
}
|
||||
|
||||
// discard: random discard at turn start
|
||||
|
|
@ -25,7 +30,9 @@ export function addTurnStartTriggers(triggers: Triggers) {
|
|||
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 triggers.onCardDiscarded.execute(ctx.game, {
|
||||
cardId: randomCardId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -33,21 +40,18 @@ export function addTurnStartTriggers(triggers: Triggers) {
|
|||
});
|
||||
|
||||
// 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) => {
|
||||
if (ctx.entityKey !== "player") {
|
||||
const defendNext = ctx.game.value.player.effects.defendNext;
|
||||
if (!defendNext || defendNext.stacks <= 0) {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
await next();
|
||||
await ctx.game.produceAsync((draft) => {
|
||||
addEntityEffect(draft.player, findEffect("defend"), defendNext.stacks);
|
||||
});
|
||||
});
|
||||
|
||||
// energyNext: gain energy next turn
|
||||
|
|
@ -59,7 +63,7 @@ export function addTurnStartTriggers(triggers: Triggers) {
|
|||
|
||||
const energyNext = ctx.game.value.player.effects.energyNext;
|
||||
if (energyNext && energyNext.stacks > 0) {
|
||||
await ctx.game.produceAsync(draft => {
|
||||
await ctx.game.produceAsync((draft) => {
|
||||
draft.player.energy += energyNext.stacks;
|
||||
addEntityEffect(draft.player, energyNext.data, -energyNext.stacks);
|
||||
});
|
||||
|
|
@ -77,7 +81,7 @@ export function addTurnStartTriggers(triggers: Triggers) {
|
|||
|
||||
const drawNext = ctx.game.value.player.effects.drawNext;
|
||||
if (drawNext && drawNext.stacks > 0) {
|
||||
await ctx.game.produceAsync(draft => {
|
||||
await ctx.game.produceAsync((draft) => {
|
||||
addEntityEffect(draft.player, drawNext.data, -drawNext.stacks);
|
||||
});
|
||||
await triggers.onDraw.execute(ctx.game, { count: drawNext.stacks });
|
||||
|
|
|
|||
|
|
@ -26,20 +26,14 @@ import {
|
|||
InventoryItem,
|
||||
} from "@/samples/slay-the-spire-like/system/grid-inventory/types";
|
||||
import { GameItemMeta } from "@/samples/slay-the-spire-like/system/grid-inventory/types";
|
||||
import { ParsedShape } from "@/samples/slay-the-spire-like/system/utils/parse-shape";
|
||||
import { Transform2D } from "@/samples/slay-the-spire-like/system/utils/shape-collision";
|
||||
import {
|
||||
CardEffect,
|
||||
getCards,
|
||||
getEffects,
|
||||
getEncounters,
|
||||
getEnemies,
|
||||
getItems,
|
||||
} from "@/samples/slay-the-spire-like/data/desert";
|
||||
const cards = getCards();
|
||||
const effects = getEffects();
|
||||
const encounters = getEncounters();
|
||||
const items = getItems();
|
||||
const enemies = getEnemies();
|
||||
|
||||
function createEffect(
|
||||
|
|
@ -298,7 +292,7 @@ describe("desert triggers", () => {
|
|||
});
|
||||
|
||||
expect(ctx.value.player.hp).toBe(27);
|
||||
expect(ctx.value.player.effects.defend?.stacks).toBe(2);
|
||||
expect(ctx.value.player.effects.defend?.stacks).toBe(undefined);
|
||||
});
|
||||
|
||||
it("should reduce damage with damageReduce", async () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue