1
0
Fork 0
spacetac/src/core/LootGenerator.ts

68 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-02-09 00:00:35 +00:00
module TS.SpaceTac {
2015-01-14 00:00:00 +00:00
// Equipment generator from loot templates
export class LootGenerator {
// List of available templates
templates: LootTemplate[];
// Random generator that will be used
random: RandomGenerator;
// Construct a basic loot generator
// The list of templates will be automatically populated
2017-02-26 17:44:15 +00:00
constructor(random = RandomGenerator.global, populate: boolean = true) {
2015-01-14 00:00:00 +00:00
this.templates = [];
this.random = random;
2015-01-14 00:00:00 +00:00
if (populate) {
this.populate();
}
2015-01-14 00:00:00 +00:00
}
// Fill the list of templates
populate(): void {
var templates: LootTemplate[] = [];
2017-02-09 00:00:35 +00:00
for (var template_name in TS.SpaceTac.Equipments) {
2017-02-07 00:08:07 +00:00
if (template_name && template_name.indexOf("Abstract") != 0) {
2017-02-09 00:00:35 +00:00
var template_class = TS.SpaceTac.Equipments[template_name];
var template: LootTemplate = new template_class();
templates.push(template);
}
}
this.templates = templates;
2015-01-14 00:00:00 +00:00
}
// Generate a random equipment
// If slot is specified, it will generate an equipment for this slot type specifically
// If level is specified, it will generate an equipment with level requirement inside this range
// If no equipment could be generated from available templates, null is returned
2017-03-09 17:11:00 +00:00
generate(level: IntegerRange | null = null, slot: SlotType | null = null): Equipment | null {
2015-01-14 00:00:00 +00:00
// Generate equipments matching conditions, with each template
var equipments: Equipment[] = [];
this.templates.forEach((template: LootTemplate) => {
2017-03-09 17:11:00 +00:00
if (slot && slot != template.slot) {
2015-01-14 00:00:00 +00:00
return;
}
2017-03-09 17:11:00 +00:00
var equipment: Equipment | null;
if (level) {
2015-01-14 00:00:00 +00:00
equipment = template.generateInLevelRange(level, this.random);
2017-03-09 17:11:00 +00:00
} else {
equipment = template.generate(this.random);
2015-01-14 00:00:00 +00:00
}
2017-03-09 17:11:00 +00:00
if (equipment) {
2015-01-14 00:00:00 +00:00
equipments.push(equipment);
}
});
// No equipment could be generated with given conditions
if (equipments.length === 0) {
return null;
}
// Pick a random equipment
return this.random.choice(equipments);
}
}
}