Files
novafarma/src/systems/NPCSpawner.js
2025-12-12 10:17:21 +01:00

77 lines
2.2 KiB
JavaScript

// NPCSpawner.js - Sistem za spawnjanje NPCjev
class NPCSpawner {
constructor(scene) {
this.scene = scene;
this.spawnInterval = 30000; // 30 sekund
this.maxNPCs = 3; // 3 NPCji na 100x100 mapo
this.spawnTimer = 0;
console.log('🧑 NPCSpawner: Initialized');
}
spawnInitialNPCs() {
// Spawn 3 NPCs at random locations
for (let i = 0; i < this.maxNPCs; i++) {
this.spawnRandomNPC();
}
console.log(`✅ Spawned ${this.maxNPCs} initial NPCs`);
}
spawnRandomNPC() {
if (!this.scene.terrainSystem || !this.scene.npcs) return;
// Random position (avoid farm area 20,20)
let x, y, attempts = 0;
do {
x = Phaser.Math.Between(5, 95);
y = Phaser.Math.Between(5, 95);
attempts++;
} while (this.isTooCloseToFarm(x, y) && attempts < 50);
// Check if tile is valid
const tile = this.scene.terrainSystem.getTile(x, y);
if (!tile || tile.type === 'water') return;
// Check if decoration exists
const key = `${x},${y}`;
if (this.scene.terrainSystem.decorationsMap.has(key)) return;
// Spawn NPC
const npc = new NPC(
this.scene,
x,
y,
this.scene.terrainOffsetX || 0,
this.scene.terrainOffsetY || 0,
'zombie' // Type
);
// Set to PASSIVE mode (random walk)
npc.state = 'PASSIVE';
npc.isTamed = false;
this.scene.npcs.push(npc);
console.log(`🧟 Spawned NPC at (${x}, ${y})`);
}
isTooCloseToFarm(x, y) {
const farmX = 50; // Farm center (updated from 20,20 to 50,50)
const farmY = 50;
const farmRadius = 15;
const dist = Math.sqrt((x - farmX) ** 2 + (y - farmY) ** 2);
return dist < farmRadius;
}
update(delta) {
// Check if we need to spawn more NPCs
if (this.scene.npcs.length < this.maxNPCs) {
this.spawnTimer += delta;
if (this.spawnTimer >= this.spawnInterval) {
this.spawnTimer = 0;
this.spawnRandomNPC();
}
}
}
}