1
0
Fork 0
spacetac/src/scripts/game/LootGenerator.ts

60 lines
1.9 KiB
TypeScript

module SpaceTac.Game {
"use strict";
// 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
constructor() {
this.templates = [];
this.random = new RandomGenerator();
this.populate();
}
// Fill the list of templates
populate(): void {
}
// 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
generate(level: IntegerRange = null, slot: SlotType = null): Equipment {
// Generate equipments matching conditions, with each template
var equipments: Equipment[] = [];
this.templates.forEach((template: LootTemplate) => {
if (slot !== null && slot !== template.slot) {
return;
}
var equipment: Equipment;
if (level === null) {
equipment = template.generate(this.random);
} else {
equipment = template.generateInLevelRange(level, this.random);
}
if (equipment !== null) {
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);
}
}
}