1
0
forked from noxious/client

#192: Update light and other effects based on server date / weather state

This commit is contained in:
Dennis Postma 2024-11-05 23:07:05 +01:00
parent 58929290ab
commit d71f4e7b59

View File

@ -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<Phaser.GameObjects.Graphics | null>(null)
const rainEmitter = ref<Phaser.GameObjects.Particles.ParticleEmitter | null>(null)
const fogSprite = ref<Phaser.GameObjects.Sprite | null>(null)
// Weather state
// State refs
const weatherState = ref<WeatherState>({
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