import {Injectable} from '@angular/core'; import {Observable, Subscription} from 'rxjs'; import {GameActionsService} from './game-actions.service'; import {Collectible} from 'ox-core'; @Injectable({ providedIn: 'root' }) export class CollectiblesService { currentCollectibles: Collectible[]; tempCollectibles: Collectible[]; constructor(private gameActions: GameActionsService) { this.gameActions.restartGame.subscribe(x => { this.cleanLists(); }); this.gameActions.startGame.subscribe(x => { this.cleanLists(); }); } private cleanLists() { this.tempCollectibles = []; this.currentCollectibles = []; } public addCollectible(collectible: Collectible) { this.currentCollectibles.push(collectible); } public addTempCollectible(collectible: Collectible, addEvent: Observable, dropEvent: Observable) { const addSubscription = new Subscription(); this.tempCollectibles.push(collectible); addSubscription.add(addEvent.subscribe(x => { this.addCollectible(collectible); addSubscription.unsubscribe(); })); addSubscription.add(dropEvent.subscribe(x => { this.tempCollectibles = this.tempCollectibles.filter(c => c !== collectible); addSubscription.unsubscribe(); })); } }