Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clamping for character initial health values. #4

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading