life.component.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 maxHitPoints: number = 0;
  12. public currentHitPoints: number = 0;
  13. public temporaryHitPoints: number = 0;
  14. public currentHitPointsPercentage: number = 0;
  15. public temporaryHitPointsPercentage: number = 0;
  16. public missingHitPointsPercentage: number = 0;
  17. private hitDice: any;
  18. public constructor(
  19. public dataAccessor: DataService,
  20. public detailsAccessor: DetailsService
  21. ) {}
  22. ngOnInit(): void {
  23. const hitPointsData = this.dataAccessor.hitPoints;
  24. this.maxHitPoints = hitPointsData.maxHitPoints;
  25. this.currentHitPoints = hitPointsData.currentHitPoints;
  26. this.temporaryHitPoints = hitPointsData.temporaryHitPoints;
  27. this.hitDice = this.dataAccessor.hitDice;
  28. this.calculatePercentages();
  29. }
  30. public addHitPoints(): void {
  31. if (this.currentHitPoints < this.maxHitPoints) {
  32. this.currentHitPoints = this.currentHitPoints + 1;
  33. this.updateDatabase();
  34. this.calculatePercentages();
  35. }
  36. }
  37. public removeHitPoints(): void {
  38. if (this.temporaryHitPoints > 0) {
  39. this.temporaryHitPoints = this.temporaryHitPoints - 1;
  40. this.updateDatabase();
  41. this.calculatePercentages();
  42. } else if (this.currentHitPoints > 0) {
  43. this.currentHitPoints = this.currentHitPoints - 1;
  44. this.updateDatabase();
  45. this.calculatePercentages();
  46. }
  47. }
  48. public addTemporaryHitPoints(): void {
  49. if (this.currentHitPoints > 0) {
  50. this.temporaryHitPoints = this.temporaryHitPoints + 1;
  51. this.updateDatabase();
  52. this.calculatePercentages();
  53. }
  54. }
  55. private calculatePercentages() {
  56. this.currentHitPointsPercentage =
  57. (this.currentHitPoints / this.maxHitPoints) * 100;
  58. this.temporaryHitPointsPercentage =
  59. (this.temporaryHitPoints / this.maxHitPoints) * 100;
  60. this.missingHitPointsPercentage =
  61. ((this.maxHitPoints - (this.currentHitPoints + this.temporaryHitPoints)) *
  62. 100) /
  63. this.maxHitPoints;
  64. }
  65. public openDetailsPanel(): void {
  66. this.detailsAccessor.openPanel(LifeDetailsComponent, {
  67. lifeData: {
  68. maxHitPoints: this.maxHitPoints,
  69. currentHitPoints: this.currentHitPoints,
  70. temporaryHitPoints: this.temporaryHitPoints,
  71. },
  72. });
  73. // The result from the details panel with return values
  74. const resultSubscription = this.detailsAccessor.result$.subscribe(
  75. (result) => {
  76. if (result.state === 'update') {
  77. this.maxHitPoints = result.data.maxHitPoints;
  78. this.currentHitPoints = result.data.currentHitPoints;
  79. this.temporaryHitPoints = result.data.temporaryHitPoints;
  80. this.hitDice = result.data.hitDice;
  81. console.log('HITDICE IN LIFE-RESPONSE: ', this.hitDice);
  82. this.calculatePercentages();
  83. this.updateDatabase();
  84. } else if (result.state === 'cancel') {
  85. // Do nothing
  86. }
  87. resultSubscription.unsubscribe();
  88. }
  89. );
  90. }
  91. public updateDatabase() {
  92. console.warn(this.hitDice.diceNumber);
  93. this.dataAccessor.hitPoints = {
  94. maxHitPoints: this.maxHitPoints,
  95. currentHitPoints: this.currentHitPoints,
  96. temporaryHitPoints: this.temporaryHitPoints,
  97. };
  98. this.dataAccessor.hitDice = this.hitDice;
  99. }
  100. }