diff --git a/src/components/Effects.vue b/src/components/Effects.vue index bb88257..92c8ab0 100644 --- a/src/components/Effects.vue +++ b/src/components/Effects.vue @@ -9,6 +9,12 @@ import { useGameStore } from '@/stores/gameStore' import { onBeforeUnmount, ref, watch } from 'vue' import type { WeatherState } from '@/types' +// Constants +const SUNRISE_HOUR = 6 +const SUNSET_HOUR = 20 +const DAY_STRENGTH = 100 +const NIGHT_STRENGTH = 30 + // Stores const gameStore = useGameStore() const zoneStore = useZoneStore() @@ -21,7 +27,7 @@ const lightEffect = ref(null) const rainEmitter = ref(null) const fogSprite = ref(null) -// Weather state +// State refs const weatherState = ref({ isRainEnabled: false, rainPercentage: 0, @@ -38,7 +44,7 @@ const preloadScene = async (scene: Phaser.Scene) => { const createScene = async (scene: Phaser.Scene) => { sceneRef.value = scene setupEffects(scene) - setupWeatherListeners() + setupSocketListeners() } const updateScene = () => { @@ -79,6 +85,35 @@ const createFogEffect = (scene: Phaser.Scene) => { fogSprite.value.setDepth(950) } +// Lighting calculations +const calculateLightStrength = (time: Date): number => { + const hour = time.getHours() + const minute = time.getMinutes() + + let strength = DAY_STRENGTH + + // Night time (10 PM - 6 AM) + if (hour >= SUNSET_HOUR || hour < SUNRISE_HOUR) { + strength = NIGHT_STRENGTH + } + // Full daylight (7 AM - 7 PM) + else if (hour > SUNRISE_HOUR && hour < SUNSET_HOUR - 2) { + strength = DAY_STRENGTH + } + // Sunrise transition (6 AM - 7 AM) + else if (hour === SUNRISE_HOUR) { + strength = NIGHT_STRENGTH + ((DAY_STRENGTH - NIGHT_STRENGTH) * minute / 60) + } + // Sunset transition (8 PM - 10 PM) + else if (hour >= SUNSET_HOUR - 2 && hour < SUNSET_HOUR) { + const totalMinutes = ((hour - (SUNSET_HOUR - 2)) * 60) + minute + const transitionProgress = totalMinutes / 120 // 2 hours = 120 minutes + strength = DAY_STRENGTH - ((DAY_STRENGTH - NIGHT_STRENGTH) * transitionProgress) + } + + return strength +} + // Effect updates const updateEffects = () => { const effects = zoneStore.zone?.zoneEffects || [] @@ -86,16 +121,19 @@ const updateEffects = () => { if (effects.length > 0) { updateZoneEffects(effects) } else { + // Make sure we're getting the current time + const lightStrength = calculateLightStrength(gameStore.world.date) + updateLightEffect(lightStrength) updateWeatherEffects() } } const updateZoneEffects = (effects: any[]) => { + // Always update light based on time when zone effects are present + updateLightEffect(calculateLightStrength(gameStore.world.date)) + effects.forEach((effect) => { switch (effect.effect) { - case 'light': - updateLightEffect(effect.strength) - break case 'rain': updateRainEffect(effect.strength) break @@ -134,11 +172,11 @@ const updateFogEffect = (strength: number) => { fogSprite.value.setAlpha(strength / 100) } -// Weather socket handlers -const setupWeatherListeners = () => { +// Socket handlers +const setupSocketListeners = () => { // Initial weather state gameStore.connection?.emit('weather', (response: WeatherState) => { - if (!zoneStore.zone) return + if (zoneStore.zone?.zoneEffects) return weatherState.value = response updateEffects() }) @@ -148,6 +186,12 @@ const setupWeatherListeners = () => { weatherState.value = data updateEffects() }) + + // Time updates + gameStore.connection?.on('date', () => { + if (zoneStore.zone?.zoneEffects) return + updateEffects() + }) } // Watchers