56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { EventEmitter, Injectable } from "@angular/core";
|
|
import { Planet } from "../model/planet.model";
|
|
import { Ship, ShipConfig } from "../model/ships/ship.model";
|
|
import { TradeRoute } from "../model/routes/trade-route.model";
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class GameService {
|
|
public showPlanetInfo: Planet | undefined;
|
|
public showShipInfo: Ship | undefined;
|
|
public showBuyShip = false;
|
|
|
|
public tradeRoutes: TradeRoute[] = [];
|
|
public ships: Ship[] = [];
|
|
public planets: Planet[] = [];
|
|
|
|
showTutorial = true;
|
|
|
|
public money = 100000;
|
|
|
|
onShipCreate: EventEmitter<Ship> = new EventEmitter();
|
|
onShipDestroy: EventEmitter<Ship> = new EventEmitter();
|
|
|
|
constructor() {}
|
|
|
|
showDialog(planet: Planet) {
|
|
this.showPlanetInfo = planet;
|
|
}
|
|
|
|
showShip(ship: Ship) {
|
|
this.showShipInfo = ship;
|
|
}
|
|
|
|
get canDrag(): boolean {
|
|
return this.showShipInfo == undefined && this.showPlanetInfo == undefined && !this.showBuyShip;
|
|
}
|
|
|
|
|
|
createShip(config: ShipConfig) {
|
|
if (this.money < config.buyCost) {
|
|
return;
|
|
}
|
|
this.money -= config.buyCost;
|
|
const ship = new Ship(this, config);
|
|
this.ships.push(ship);
|
|
this.onShipCreate.emit(ship);
|
|
}
|
|
|
|
sellShip(ship: Ship) {
|
|
this.ships = this.ships.filter(s => s != ship);
|
|
ship.sell();
|
|
this.onShipDestroy.emit(ship);
|
|
this.money += ship.buyCost;
|
|
}
|
|
} |