life.component.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { Component } from '@angular/core';
  2. import { DataService } from 'src/services/data/data.service';
  3. import { DetailsService } from 'src/services/details/details.service';
  4. import { LifeDetailsComponent } from './life-details/life-details.component';
  5. @Component({
  6. selector: 'life',
  7. templateUrl: './life.component.html',
  8. styleUrls: ['./life.component.scss'],
  9. })
  10. export class LifeComponent {
  11. public constructor(
  12. public dataAccessor: DataService,
  13. public detailsAccessor: DetailsService
  14. ) {}
  15. public maxHitPoints: number = 0;
  16. public currentHitPoints: number = 0;
  17. public temporaryHitPoints: number = 0;
  18. public currentHitPointsPercentage: number = 0;
  19. public temporaryHitPointsPercentage: number = 0;
  20. public missingHitPointsPercentage: number = 0;
  21. ngOnInit(): void {
  22. const hitPointsData = this.dataAccessor.hitPoints;
  23. this.maxHitPoints = hitPointsData.maxHitPoints;
  24. this.currentHitPoints = hitPointsData.currentHitPoints;
  25. this.temporaryHitPoints = hitPointsData.temporaryHitPoints;
  26. this.calculatePercentages();
  27. }
  28. public addHitPoints(): void {
  29. if (this.currentHitPoints < this.maxHitPoints) {
  30. this.currentHitPoints = this.currentHitPoints + 1;
  31. this.updateDatabase();
  32. this.calculatePercentages();
  33. }
  34. }
  35. public removeHitPoints(): void {
  36. if (this.temporaryHitPoints > 0) {
  37. this.temporaryHitPoints = this.temporaryHitPoints - 1;
  38. this.updateDatabase();
  39. this.calculatePercentages();
  40. } else if (this.currentHitPoints > 0) {
  41. this.currentHitPoints = this.currentHitPoints - 1;
  42. this.updateDatabase();
  43. this.calculatePercentages();
  44. }
  45. }
  46. public addTemporaryHitPoints(): void {
  47. if (this.currentHitPoints > 0) {
  48. this.temporaryHitPoints = this.temporaryHitPoints + 1;
  49. this.updateDatabase();
  50. this.calculatePercentages();
  51. }
  52. }
  53. private calculatePercentages() {
  54. this.currentHitPointsPercentage =
  55. (this.currentHitPoints / this.maxHitPoints) * 100;
  56. this.temporaryHitPointsPercentage =
  57. (this.temporaryHitPoints / this.maxHitPoints) * 100;
  58. this.missingHitPointsPercentage =
  59. ((this.maxHitPoints - (this.currentHitPoints + this.temporaryHitPoints)) *
  60. 100) /
  61. this.maxHitPoints;
  62. }
  63. public openDetailsPanel(): void {
  64. this.detailsAccessor.openPanel(LifeDetailsComponent, {
  65. lifeData: {
  66. maxHitPoints: this.maxHitPoints,
  67. currentHitPoints: this.currentHitPoints,
  68. temporaryHitPoints: this.temporaryHitPoints,
  69. },
  70. });
  71. // The result from the details panel with return values
  72. const resultSubscription = this.detailsAccessor.result$.subscribe(
  73. (result) => {
  74. console.log(result);
  75. if (result.state === 'update') {
  76. this.maxHitPoints = result.data.maxHitPoints;
  77. this.currentHitPoints = result.data.currentHitPoints;
  78. this.temporaryHitPoints = result.data.temporaryHitPoints;
  79. this.calculatePercentages();
  80. this.updateDatabase();
  81. }
  82. resultSubscription.unsubscribe();
  83. }
  84. );
  85. }
  86. public updateDatabase() {
  87. this.dataAccessor.hitPoints = {
  88. maxHitPoints: this.maxHitPoints,
  89. currentHitPoints: this.currentHitPoints,
  90. temporaryHitPoints: this.temporaryHitPoints,
  91. };
  92. }
  93. }