2017-02-09 00:00:35 +00:00
|
|
|
module TS.SpaceTac {
|
2015-01-14 00:00:00 +00:00
|
|
|
// Generator of random ship
|
|
|
|
export class ShipGenerator {
|
|
|
|
// Random number generator used
|
|
|
|
random: RandomGenerator;
|
|
|
|
|
2017-03-09 17:11:00 +00:00
|
|
|
constructor(random = RandomGenerator.global) {
|
|
|
|
this.random = random;
|
2015-01-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a ship of a given level
|
|
|
|
// The ship will not be named, nor will be a member of any fleet
|
2017-03-09 17:11:00 +00:00
|
|
|
generate(level: number, model: ShipModel | null = null): Ship {
|
2015-01-14 00:00:00 +00:00
|
|
|
var result = new Ship();
|
|
|
|
var loot = new LootGenerator(this.random);
|
|
|
|
|
2015-05-05 19:49:33 +00:00
|
|
|
if (!model) {
|
|
|
|
// Get a random model
|
|
|
|
model = ShipModel.getRandomModel(level, this.random);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply model
|
|
|
|
result.model = model.code;
|
2017-03-05 17:48:13 +00:00
|
|
|
result.setCargoSpace(model.cargo);
|
2015-05-05 19:49:33 +00:00
|
|
|
model.slots.forEach((slot: SlotType) => {
|
|
|
|
result.addSlot(slot);
|
|
|
|
});
|
2015-01-14 00:00:00 +00:00
|
|
|
|
2017-04-18 19:51:23 +00:00
|
|
|
// Set all skills to 1 (to be able to use at least basic equipment)
|
|
|
|
keys(result.skills).forEach(skill => result.upgradeSkill(skill));
|
|
|
|
|
2015-01-14 00:00:00 +00:00
|
|
|
// Fill equipment slots
|
|
|
|
result.slots.forEach((slot: Slot) => {
|
2017-04-18 19:51:23 +00:00
|
|
|
var equipment = loot.generate(level, slot.type);
|
2017-03-09 17:11:00 +00:00
|
|
|
if (equipment) {
|
2017-03-09 19:11:19 +00:00
|
|
|
slot.attach(equipment)
|
|
|
|
if (slot.attached !== equipment) {
|
|
|
|
console.error("Cannot attach generated equipment to slot", equipment, slot);
|
|
|
|
}
|
2017-03-09 17:11:00 +00:00
|
|
|
}
|
2015-01-14 00:00:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|