Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. import { LoggerService } from './../services/logger.service';
  2. import { Pokemon } from './../models/Pokemon';
  3. import { FightService, PokemonFightListener } from './../services/fight.service';
  4. import { Component, OnInit } from '@angular/core';
  5. import { Subscription, timer } from 'rxjs';
  6. import { tap, mergeMap } from 'rxjs/operators';
  7. import { ActivatedRoute, Params } from '@angular/router';
  8. import { Log, LogType } from '../models/Log';
  9. import * as $ from "jquery";
  10. import pokemonGif from 'pokemon-gif';
  11.  
  12.  
  13. @Component({
  14. selector: 'app-fight',
  15. templateUrl: './fight.component.html',
  16. styleUrls: ['./fight.component.css']
  17. })
  18.  
  19.  
  20. export class FightComponent implements OnInit, PokemonFightListener {
  21.  
  22. isFinished: boolean;
  23. isRunning: boolean;
  24. pokemon1: Pokemon;
  25. pokemon2: Pokemon;
  26. subscriber: Subscription;
  27. title: string;
  28. loggerService: LoggerService;
  29. LogType = LogType;
  30.  
  31. stateScale : string = "inactive";
  32.  
  33. constructor(private route: ActivatedRoute, private fightService: FightService, logger: LoggerService) {
  34. this.loggerService = logger;
  35. }
  36.  
  37. ngOnInit(): void {
  38. this.fightService.subscribe(this);
  39. this.initiateFight();
  40. }
  41.  
  42. initiateFight() {
  43. let p1, p2;
  44.  
  45. this.subscriber = this.route.params.pipe(
  46. tap( (params: Params) => {
  47. p1 = params['pokemon1'];
  48. p2 = params['pokemon2'];
  49. this.title = p1.toUpperCase() + ' vs ' + p2.toUpperCase();
  50. }),
  51. mergeMap(() => this.fightService.getPokemons(p1, p2)),
  52. tap((pokemons: Pokemon[]) => {
  53. console.log(pokemons);
  54. [this.pokemon1, this.pokemon2] = pokemons;
  55. }),
  56. mergeMap(() => {
  57. return this.fightService.attack();
  58. })
  59. ).subscribe();
  60.  
  61. }
  62.  
  63. handleMainButton() {
  64. if (this.isFinished) {
  65. this.isRunning = false;
  66. this.isFinished = false;
  67. this.subscriber.unsubscribe();
  68. this.initiateFight();
  69. } else {
  70. this.isRunning = !this.isRunning;
  71. this.fightService.setPause(!this.isRunning);
  72. }
  73. }
  74.  
  75. onPokemonAttack(attacker: Pokemon, defender: Pokemon) {
  76. $('#log-screen').animate( { scrollTop: $('#log-screen').offset().top }, 1000 );
  77. let attackr, defendr;
  78. if (attacker.name == this.pokemon1.name) {
  79. attackr = '#pokemon1';
  80. defendr = '#pokemon2';
  81. $(attackr).animate( { left: '+=100' }, 250, function() {
  82. $(attackr).animate( { left: '-=100' }, 250);
  83. });
  84. } else {
  85. attackr = '#pokemon2';
  86. defendr = '#pokemon1';
  87. $(attackr).animate( { left: '-=100' }, 250, function() {
  88. $(attackr).animate( { left: '+=100' }, 250);
  89. });
  90. }
  91. let timr = timer(0, 100);
  92. let sub = timr.subscribe(val => {
  93. let opac = $(defendr).css('opacity') == '0' ? '1' : '0';
  94. $(defendr).css('opacity', opac);
  95. });
  96. setTimeout(() => {
  97. sub.unsubscribe();
  98. $(defendr).css('opacity', '1');
  99. }, 500);
  100. if (attacker.hp <= 0 || defender.hp <= 0) {
  101. this.isFinished = true;
  102. }
  103. }
  104.  
  105. pokemonGif(name: string) {
  106. return pokemonGif(name);
  107. }
  108.  
  109. scrollToContent() {
  110. $('html, body').animate( { scrollTop: $('#fight-content').offset().top + 80 }, 1000 );
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement