61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import { CommonModule } from '@angular/common';
|
|
import { Component, inject } from '@angular/core';
|
|
import { GameService } from '../../../service/game.service';
|
|
import { CdkDrag, CdkDragDrop, CdkDragHandle, CdkDropList, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
|
|
import { Planet } from '../../../model/planet.model';
|
|
import { ShipConfig } from '../../../model/ships/ship.model';
|
|
|
|
@Component({
|
|
selector: 'app-buy',
|
|
imports: [CommonModule, CdkDropList, CdkDrag, CdkDragHandle],
|
|
templateUrl: './buy.component.html',
|
|
styleUrl: './buy.component.scss'
|
|
})
|
|
export class BuyComponent {
|
|
private gameService: GameService = inject(GameService);
|
|
|
|
availablePlanets: Planet[] = this.gameService.planets.filter(p => p.hasHarbour);
|
|
selectedPlanets: Planet[] = [];
|
|
config: ShipConfig = {
|
|
name: 'Pioneer-01',
|
|
acceleration: 500,
|
|
cargoSize: 10,
|
|
cost: 0.6,
|
|
loadingSpeed: 25,
|
|
maxSpeed: 500,
|
|
planetRoute: [],
|
|
buyCost: 2000,
|
|
desciption: 'Ein kleines, schnelles Schiff. Es hat wenig Platz für Ladung, lädt aber schnell und ist sehr wendig.'
|
|
}
|
|
|
|
canAffordShip() {
|
|
return this.gameService.money >= this.config.buyCost;
|
|
}
|
|
|
|
buyShip() {
|
|
if (!this.canAffordShip()) return;
|
|
this.gameService.createShip({
|
|
...this.config,
|
|
planetRoute: this.selectedPlanets
|
|
})
|
|
this.close();
|
|
}
|
|
|
|
close() {
|
|
this.gameService.showBuyShip = false;
|
|
}
|
|
|
|
drop(event: CdkDragDrop<Planet[]>) {
|
|
if (event.previousContainer === event.container) {
|
|
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
|
|
} else {
|
|
transferArrayItem(
|
|
event.previousContainer.data,
|
|
event.container.data,
|
|
event.previousIndex,
|
|
event.currentIndex,
|
|
);
|
|
}
|
|
}
|
|
}
|