Frameworki kodowe
Przepis: ogranicz wartość
Utrzymaj wartość w zakresie za pomocą math.max i math.min.
Cel
Nie pozwól, by HP spadło poniżej zera, gdy Keeper otrzymuje obrażenia.
Zmiana
Odczytaj bieżącą wartość, a potem zapisz ograniczony wynik.
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));
}- Wiersz 2 Odczytaj bieżące HP.
- Wiersz 3 math.max utrzymuje wynik na poziomie 0 lub wyżej. Zapisz końcową wartość za pomocą ledger.set.
Użyj math.min(limit, value), aby utrzymać wartość na poziomie limitu lub niżej.
Limitem może być inne pole. Aby utrzymać Sanity na poziomie pola max_sanity lub niżej, odczytaj oba i zapisz math.min(max_sanity, sanity + amount). Reguła heal z szybkiego startu robi to z max_hp. Przetestuj krok z ograniczeniem.
Przetestuj to
Reguła teraz ustawia wartość, więc oczekiwany zapis to ledger.set zamiast ledger.add. Dodaj krok, który spadłby poniżej zera.
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 }]
}
]- Wiersze 7-11 Otrzymaj 6 obrażeń: 10 minus 6 to 4.
- Wiersze 12-16 Cios za 20 spadłby poniżej zera. Reguła zapisuje 0.
- Wiersze 17-21 Ulecz 3 od 0.

