Framework in codice
Ricetta: limitare un valore
Tieni un valore dentro un intervallo con math.max e math.min.
Obiettivo
Impedisci che i PF scendano sotto zero quando un Keeper subisce danno.
La modifica
Leggi il valore attuale, poi scrivi il risultato limitato.
rules/hurt.ts
export function hurt(self: ElementRef, amount: number): void {
const hp: number = element.var(self, "hp") as number;
ledger.set(self, "hp", math.max(0, hp - amount));
}- Riga 2 Leggi gli HP attuali.
- Riga 3 math.max mantiene il risultato a 0 o più. Scrivi il valore finale con ledger.set.
Usa math.min(limit, value) per tenere un valore pari o inferiore a un limite.
Un limite può essere un altro campo. Per tenere Sanity pari o inferiore a un campo max_sanity, leggi entrambi e scrivi math.min(max_sanity, sanity + amount). La regola di cura nell'avvio rapido lo fa con max_hp. Metti alla prova il passo limitato.
Provalo
La regola ora imposta un valore, quindi la scrittura attesa è ledger.set invece di ledger.add. Aggiungi un passaggio che scenderebbe sotto zero.
scenarios/heal.json
"actions": [
{
"action": "hurt",
"parameters": { "amount": 6 },
"expectedWrites": [{ "op": "ledger.set", "key": "hp", "value": 4 }]
},
{
"action": "hurt",
"parameters": { "amount": 20 },
"expectedWrites": [{ "op": "ledger.set", "key": "hp", "value": 0 }]
},
{
"action": "heal",
"parameters": { "amount": 3 },
"expectedWrites": [{ "op": "ledger.set", "key": "hp", "value": 3 }]
}
]- Righe 7-11 Subisci 6 danni: 10 meno 6 fa 4.
- Righe 12-16 Un colpo da 20 scenderebbe sotto zero. La regola scrive 0.
- Righe 17-21 Cura 3 partendo da 0.

