From 4199048c7f1067a0481c7842b5aadcbb28a8e771 Mon Sep 17 00:00:00 2001 From: "Daniel H. Alcojor" Date: Fri, 23 Aug 2024 09:16:53 +0200 Subject: [PATCH] Add clamping for character initial health values. Imported and used clamp utility function to ensure character initial health values are within the 1 to 9999 range. This prevents potential issues with out-of-bound health values affecting game logic. --- src/app/fight/fight.component.ts | 12 +++++++----- src/app/utils.ts | 4 ++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/app/fight/fight.component.ts b/src/app/fight/fight.component.ts index 37a6543..d5645e5 100644 --- a/src/app/fight/fight.component.ts +++ b/src/app/fight/fight.component.ts @@ -15,7 +15,7 @@ import { RoundEvent, } from '../components/event/event.component' import { HealthBarComponent } from '../components/health-bar/health-bar.component' -import { getRandomFromRange } from '../utils' +import { clamp, getRandomFromRange } from '../utils' import { HeaderComponent } from '../header/header.component' import { HpEventComponent } from '../components/hp-event/hp-event.component' @@ -53,15 +53,17 @@ export class FightComponent implements OnInit { /** Input parameter read from the URL path */ @Input() set leftCharacterInitialHealth(value: number) { - this.leftCharacterHealth.set(value) - this.leftCharacter.maxHealth = value + const clampedValue = clamp(value, 1, 9999) + this.leftCharacterHealth.set(clampedValue) + this.leftCharacter.maxHealth = clampedValue } /** Input parameter read from the URL path */ @Input() set rightCharacterInitialHealth(value: number) { - this.rightCharacterHealth.set(value) - this.rightCharacter.maxHealth = value + const clampedValue = clamp(value, 1, 9999) + this.rightCharacterHealth.set(clampedValue) + this.rightCharacter.maxHealth = clampedValue } ngOnInit(): void { diff --git a/src/app/utils.ts b/src/app/utils.ts index 19cb515..926347a 100644 --- a/src/app/utils.ts +++ b/src/app/utils.ts @@ -4,3 +4,7 @@ export function getRandomFromRange(min: number, max: number): number { } return Math.floor(Math.random() * (max - min + 1)) + min } + +export function clamp(value: number, min: number, max: number): number { + return Math.min(Math.max(Math.floor(value), min), max) +}