123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import { Component } from '@angular/core';
- import { DataService } from 'src/services/data/data.service';
- import { DetailsService } from 'src/services/details/details.service';
- import { LifeDetailsComponent } from './life-details/life-details.component';
- @Component({
- selector: 'life',
- templateUrl: './life.component.html',
- styleUrls: ['./life.component.scss'],
- })
- export class LifeComponent {
- public maxHitPoints: number = 0;
- public currentHitPoints: number = 0;
- public temporaryHitPoints: number = 0;
- public currentHitPointsPercentage: number = 0;
- public temporaryHitPointsPercentage: number = 0;
- public missingHitPointsPercentage: number = 0;
- private hitDice: any;
- public constructor(
- public dataAccessor: DataService,
- public detailsAccessor: DetailsService
- ) {}
- ngOnInit(): void {
- const hitPointsData = this.dataAccessor.hitPoints;
- this.maxHitPoints = hitPointsData.maxHitPoints;
- this.currentHitPoints = hitPointsData.currentHitPoints;
- this.temporaryHitPoints = hitPointsData.temporaryHitPoints;
- this.hitDice = this.dataAccessor.hitDice;
- this.calculatePercentages();
- }
- public addHitPoints(): void {
- if (this.currentHitPoints < this.maxHitPoints) {
- this.currentHitPoints = this.currentHitPoints + 1;
- this.updateDatabase();
- this.calculatePercentages();
- }
- }
- public removeHitPoints(): void {
- if (this.temporaryHitPoints > 0) {
- this.temporaryHitPoints = this.temporaryHitPoints - 1;
- this.updateDatabase();
- this.calculatePercentages();
- } else if (this.currentHitPoints > 0) {
- this.currentHitPoints = this.currentHitPoints - 1;
- this.updateDatabase();
- this.calculatePercentages();
- }
- }
- public addTemporaryHitPoints(): void {
- if (this.currentHitPoints > 0) {
- this.temporaryHitPoints = this.temporaryHitPoints + 1;
- this.updateDatabase();
- this.calculatePercentages();
- }
- }
- private calculatePercentages() {
- this.currentHitPointsPercentage =
- (this.currentHitPoints / this.maxHitPoints) * 100;
- this.temporaryHitPointsPercentage =
- (this.temporaryHitPoints / this.maxHitPoints) * 100;
- this.missingHitPointsPercentage =
- ((this.maxHitPoints - (this.currentHitPoints + this.temporaryHitPoints)) *
- 100) /
- this.maxHitPoints;
- }
- public openDetailsPanel(): void {
- this.detailsAccessor.openPanel(LifeDetailsComponent, {
- lifeData: {
- maxHitPoints: this.maxHitPoints,
- currentHitPoints: this.currentHitPoints,
- temporaryHitPoints: this.temporaryHitPoints,
- },
- });
- // The result from the details panel with return values
- const resultSubscription = this.detailsAccessor.result$.subscribe(
- (result) => {
- if (result.state === 'update') {
- this.maxHitPoints = result.data.maxHitPoints;
- this.currentHitPoints = result.data.currentHitPoints;
- this.temporaryHitPoints = result.data.temporaryHitPoints;
- this.hitDice = result.data.hitDice;
- console.log('HITDICE IN LIFE-RESPONSE: ', this.hitDice);
- this.calculatePercentages();
- this.updateDatabase();
- } else if (result.state === 'cancel') {
- // Do nothing
- }
- resultSubscription.unsubscribe();
- }
- );
- }
- public updateDatabase() {
- console.warn(this.hitDice.diceNumber);
- this.dataAccessor.hitPoints = {
- maxHitPoints: this.maxHitPoints,
- currentHitPoints: this.currentHitPoints,
- temporaryHitPoints: this.temporaryHitPoints,
- };
- this.dataAccessor.hitDice = this.hitDice;
- }
- }
|