52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import { GoodType } from "../goods/good-type.enum";
|
|
import { Planet, TradeInstance } from "../planet.model";
|
|
import { TradeRoute } from "../routes/trade-route.model";
|
|
|
|
export class Ship {
|
|
public acceleration = 200; // Pixel pro Sekunde²
|
|
public maxSpeed = 4000;
|
|
public slowDownRadius = 2000; // Startet Bremsen, wenn Ziel nahe ist
|
|
public cargoSize = 100;
|
|
cargoSpace: TradeInstance[] = [];
|
|
public route: TradeRoute | undefined = new TradeRoute([])
|
|
|
|
exchangeGoods(planet: Planet) {
|
|
if (!this.route) { return; }
|
|
|
|
planet.deliver(this);
|
|
|
|
console.log(`\n[${planet.name}] Starting new Trade. Free cargospace: ${this.freeCargoSpace.toFixed(2)}`)
|
|
|
|
const demands = this.route.getTradeRouteDemands();
|
|
if (demands.length == 0) { return; }
|
|
console.log(JSON.parse(JSON.stringify(demands)))
|
|
|
|
for (let demand of demands) {
|
|
// requested amount: demand - storage
|
|
const stored = this.cargoSpace.find(c => c.type == demand.type);
|
|
if (stored) { demand.amount -= stored.amount; };
|
|
demand.amount = Math.min(demand.amount, this.freeCargoSpace);
|
|
demand.amount = Math.max(demand.amount, 0)
|
|
if (demand.amount > 0) { console.log(`[${planet.name}] Requesting ${demand.amount.toFixed(2)} ${demand.type}`); }
|
|
if (demand.amount == 0) { continue; }
|
|
const received = planet.request(demand);
|
|
this.addToCargoSpace({type: demand.type, amount: received})
|
|
}
|
|
|
|
console.log(`Cargo: ${this.cargoSpace.map(c => `${c.type}: ${c.amount.toFixed(2)}`).join(', ')}`);
|
|
}
|
|
|
|
get freeCargoSpace(): number {
|
|
return this.cargoSize - this.cargoSpace.reduce((acc, succ) => acc + succ.amount, 0)
|
|
}
|
|
|
|
addToCargoSpace(cargo: TradeInstance) {
|
|
const existing = this.cargoSpace.find(c => c.type == cargo.type);
|
|
|
|
if (existing) {
|
|
existing.amount += cargo.amount;
|
|
} else {
|
|
this.cargoSpace.push(cargo);
|
|
}
|
|
}
|
|
} |