Skip to content

Commit

Permalink
Add clamping for character initial health values.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dhAlcojor committed Aug 23, 2024
1 parent 02304c9 commit 4199048
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/app/fight/fight.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 4199048

Please sign in to comment.