Aethelgard/js/managers/SpawnManager.js

34 lines
934 B
JavaScript

import Enemy from '../objects/Enemy.js';
export default class SpawnManager {
constructor(scene, unitData) {
this.scene = scene;
this.unitDefs = unitData; // object keyed by enemy type
this.timer = null;
}
startSpawning(interval) {
this.timer = this.scene.time.addEvent({
delay: interval,
loop: true,
callback: this.spawnEnemy,
callbackScope: this
});
}
updateSpawnInterval(newInterval) {
if (this.timer) { this.timer.remove(); }
this.startSpawning(newInterval);
}
spawnEnemy() {
const levelCfg = this.scene.levelManager.currentConfig;
const type = Phaser.Utils.Array.GetRandom(levelCfg.enemyTypes);
const def = this.unitDefs[type];
const x = Phaser.Math.Between(50, this.scene.scale.width - 50);
const y = -def.height; // just above screen
const enemy = new Enemy(this.scene, x, y, 'enemy', def);
this.scene.enemies.add(enemy);
}
}