Files
novafarma/old_logic/src_backup_1768938138/managers/GlobalWeatherManager.js
2026-01-21 01:08:21 +01:00

290 lines
8.5 KiB
JavaScript

/**
* GlobalWeatherManager.js
*
* Global manager for MasterWeatherSystem
* Ensures weather is consistent across all scenes
* Handles scene transitions and weather persistence
*
* Usage: Initialized once in main.js, accessed by all scenes
*/
import MasterWeatherSystem from '../systems/MasterWeatherSystem.js';
export default class GlobalWeatherManager {
constructor(game) {
this.game = game;
// Map of scene key → weather system instance
this.weatherSystems = new Map();
// Global weather state (persists across scenes)
this.globalWeatherState = {
type: 'clear', // Current weather type
intensity: 0.5, // 0.0 - 1.0
windStrength: 1.0, // Wind multiplier
transitionDuration: 3000, // Weather change transition time (ms)
autoChange: true, // Auto weather changes enabled
changeInterval: 300000 // Auto change every 5 minutes
};
// Current biome (affects default weather)
this.currentBiome = 'grassland';
// Auto weather change timer
this.autoWeatherTimer = null;
console.log('🌍 GlobalWeatherManager: Initialized');
}
/**
* Create weather system for a scene
* Called when scene starts
*
* @param {Phaser.Scene} scene - The scene to create weather for
* @returns {MasterWeatherSystem} Weather system instance
*/
createForScene(scene) {
const sceneKey = scene.scene.key;
// Check if already exists
if (this.weatherSystems.has(sceneKey)) {
console.warn(`⚠️ Weather system already exists for ${sceneKey}`);
return this.weatherSystems.get(sceneKey);
}
// Create new weather system
const weather = new MasterWeatherSystem(scene);
weather.init();
// Apply global weather state
weather.setWeather(
this.globalWeatherState.type,
this.globalWeatherState.intensity,
0 // No transition on scene start
);
weather.windSystem.setWindStrength(this.globalWeatherState.windStrength);
// Store reference
this.weatherSystems.set(sceneKey, weather);
console.log(`✅ Weather system created for scene: ${sceneKey}`);
return weather;
}
/**
* Set weather globally (affects all active scenes)
*
* @param {string} type - Weather type: 'clear', 'rain', 'snow', 'storm', 'blizzard'
* @param {number} intensity - Intensity 0.0 - 1.0
* @param {number} transitionDuration - Transition time in ms (optional)
*/
setGlobalWeather(type, intensity = 0.5, transitionDuration = null) {
transitionDuration = transitionDuration || this.globalWeatherState.transitionDuration;
// Update global state
this.globalWeatherState.type = type;
this.globalWeatherState.intensity = intensity;
console.log(`🌦️ Global weather changing to: ${type} (intensity: ${intensity})`);
// Apply to all active scenes
this.weatherSystems.forEach((weather, sceneKey) => {
weather.setWeather(type, intensity, transitionDuration);
});
}
/**
* Set current biome (affects default weather)
*
* @param {string} biomeName - Biome name
*/
setBiome(biomeName) {
this.currentBiome = biomeName.toLowerCase();
console.log(`🌍 Biome changed to: ${biomeName}`);
// Apply biome weather to all active scenes
this.weatherSystems.forEach(weather => {
weather.setBiomeWeather(biomeName);
});
}
/**
* Enable/disable automatic weather changes
*
* @param {boolean} enabled
*/
setAutoWeather(enabled) {
this.globalWeatherState.autoChange = enabled;
if (enabled) {
this.startAutoWeatherChanges();
} else {
this.stopAutoWeatherChanges();
}
}
/**
* Start automatic weather changes
*/
startAutoWeatherChanges() {
// Clear existing timer
if (this.autoWeatherTimer) {
clearInterval(this.autoWeatherTimer);
}
// Create new timer
this.autoWeatherTimer = setInterval(() => {
this.randomWeatherChange();
}, this.globalWeatherState.changeInterval);
console.log('🔄 Auto weather changes: ENABLED');
}
/**
* Stop automatic weather changes
*/
stopAutoWeatherChanges() {
if (this.autoWeatherTimer) {
clearInterval(this.autoWeatherTimer);
this.autoWeatherTimer = null;
}
console.log('⏸️ Auto weather changes: DISABLED');
}
/**
* Randomly change weather (respects biome rules)
*/
randomWeatherChange() {
// Get allowed weather types for current biome
const biomeSettings = this.getBiomeSettings(this.currentBiome);
const allowedWeather = biomeSettings?.allowedWeather || ['clear', 'rain'];
// Pick random weather
const randomType = Phaser.Math.RND.pick(allowedWeather);
const randomIntensity = Phaser.Math.FloatBetween(0.3, 0.8);
console.log(`🎲 Random weather change: ${randomType}`);
this.setGlobalWeather(randomType, randomIntensity);
}
/**
* Get biome settings
*/
getBiomeSettings(biomeName) {
const settings = {
'grassland': {
allowedWeather: ['clear', 'rain', 'storm'],
defaultWind: 1.0
},
'desert': {
allowedWeather: ['clear', 'sandstorm'],
defaultWind: 1.5
},
'snow': {
allowedWeather: ['clear', 'snow', 'blizzard'],
defaultWind: 1.8
},
'tundra': {
allowedWeather: ['clear', 'snow', 'blizzard'],
defaultWind: 1.8
},
'swamp': {
allowedWeather: ['rain', 'fog'],
defaultWind: 0.3
},
'mountains': {
allowedWeather: ['clear', 'snow', 'storm'],
defaultWind: 2.0
},
'forest': {
allowedWeather: ['clear', 'rain'],
defaultWind: 0.8
},
'volcanic': {
allowedWeather: ['clear', 'ash_rain'],
defaultWind: 1.2
}
};
return settings[biomeName.toLowerCase()];
}
/**
* Destroy weather system for a scene
* Called when scene shuts down
*
* @param {string} sceneKey - Scene key
*/
destroyForScene(sceneKey) {
const weather = this.weatherSystems.get(sceneKey);
if (weather) {
weather.destroy();
this.weatherSystems.delete(sceneKey);
console.log(`🗑️ Weather system destroyed for: ${sceneKey}`);
}
}
/**
* Get weather system for a scene
*
* @param {string} sceneKey - Scene key
* @returns {MasterWeatherSystem|null}
*/
getWeatherForScene(sceneKey) {
return this.weatherSystems.get(sceneKey) || null;
}
/**
* Get current global weather state
*/
getGlobalState() {
return { ...this.globalWeatherState };
}
/**
* Update all weather systems (call from global game loop if needed)
*/
updateAll(delta) {
this.weatherSystems.forEach(weather => {
weather.update(delta);
});
}
/**
* Debug: Show all active weather systems
*/
debug() {
console.log('🌦️ === GLOBAL WEATHER DEBUG ===');
console.log(`Current Weather: ${this.globalWeatherState.type}`);
console.log(`Intensity: ${this.globalWeatherState.intensity}`);
console.log(`Wind Strength: ${this.globalWeatherState.windStrength}`);
console.log(`Current Biome: ${this.currentBiome}`);
console.log(`Auto Changes: ${this.globalWeatherState.autoChange ? 'ON' : 'OFF'}`);
console.log(`Active Scenes: ${this.weatherSystems.size}`);
this.weatherSystems.forEach((weather, sceneKey) => {
console.log(` - ${sceneKey}: ${weather.currentWeather}`);
});
}
/**
* Cleanup (call on game shutdown)
*/
destroy() {
this.stopAutoWeatherChanges();
this.weatherSystems.forEach(weather => {
weather.destroy();
});
this.weatherSystems.clear();
console.log('🌍 GlobalWeatherManager: Destroyed');
}
}