forked from noxious/client
#192: Update light and other effects based on server date / weather state
This commit is contained in:
parent
58929290ab
commit
d71f4e7b59
@ -9,6 +9,12 @@ import { useGameStore } from '@/stores/gameStore'
|
|||||||
import { onBeforeUnmount, ref, watch } from 'vue'
|
import { onBeforeUnmount, ref, watch } from 'vue'
|
||||||
import type { WeatherState } from '@/types'
|
import type { WeatherState } from '@/types'
|
||||||
|
|
||||||
|
// Constants
|
||||||
|
const SUNRISE_HOUR = 6
|
||||||
|
const SUNSET_HOUR = 20
|
||||||
|
const DAY_STRENGTH = 100
|
||||||
|
const NIGHT_STRENGTH = 30
|
||||||
|
|
||||||
// Stores
|
// Stores
|
||||||
const gameStore = useGameStore()
|
const gameStore = useGameStore()
|
||||||
const zoneStore = useZoneStore()
|
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 rainEmitter = ref<Phaser.GameObjects.Particles.ParticleEmitter | null>(null)
|
||||||
const fogSprite = ref<Phaser.GameObjects.Sprite | null>(null)
|
const fogSprite = ref<Phaser.GameObjects.Sprite | null>(null)
|
||||||
|
|
||||||
// Weather state
|
// State refs
|
||||||
const weatherState = ref<WeatherState>({
|
const weatherState = ref<WeatherState>({
|
||||||
isRainEnabled: false,
|
isRainEnabled: false,
|
||||||
rainPercentage: 0,
|
rainPercentage: 0,
|
||||||
@ -38,7 +44,7 @@ const preloadScene = async (scene: Phaser.Scene) => {
|
|||||||
const createScene = async (scene: Phaser.Scene) => {
|
const createScene = async (scene: Phaser.Scene) => {
|
||||||
sceneRef.value = scene
|
sceneRef.value = scene
|
||||||
setupEffects(scene)
|
setupEffects(scene)
|
||||||
setupWeatherListeners()
|
setupSocketListeners()
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateScene = () => {
|
const updateScene = () => {
|
||||||
@ -79,6 +85,35 @@ const createFogEffect = (scene: Phaser.Scene) => {
|
|||||||
fogSprite.value.setDepth(950)
|
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
|
// Effect updates
|
||||||
const updateEffects = () => {
|
const updateEffects = () => {
|
||||||
const effects = zoneStore.zone?.zoneEffects || []
|
const effects = zoneStore.zone?.zoneEffects || []
|
||||||
@ -86,16 +121,19 @@ const updateEffects = () => {
|
|||||||
if (effects.length > 0) {
|
if (effects.length > 0) {
|
||||||
updateZoneEffects(effects)
|
updateZoneEffects(effects)
|
||||||
} else {
|
} else {
|
||||||
|
// Make sure we're getting the current time
|
||||||
|
const lightStrength = calculateLightStrength(gameStore.world.date)
|
||||||
|
updateLightEffect(lightStrength)
|
||||||
updateWeatherEffects()
|
updateWeatherEffects()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateZoneEffects = (effects: any[]) => {
|
const updateZoneEffects = (effects: any[]) => {
|
||||||
|
// Always update light based on time when zone effects are present
|
||||||
|
updateLightEffect(calculateLightStrength(gameStore.world.date))
|
||||||
|
|
||||||
effects.forEach((effect) => {
|
effects.forEach((effect) => {
|
||||||
switch (effect.effect) {
|
switch (effect.effect) {
|
||||||
case 'light':
|
|
||||||
updateLightEffect(effect.strength)
|
|
||||||
break
|
|
||||||
case 'rain':
|
case 'rain':
|
||||||
updateRainEffect(effect.strength)
|
updateRainEffect(effect.strength)
|
||||||
break
|
break
|
||||||
@ -134,11 +172,11 @@ const updateFogEffect = (strength: number) => {
|
|||||||
fogSprite.value.setAlpha(strength / 100)
|
fogSprite.value.setAlpha(strength / 100)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Weather socket handlers
|
// Socket handlers
|
||||||
const setupWeatherListeners = () => {
|
const setupSocketListeners = () => {
|
||||||
// Initial weather state
|
// Initial weather state
|
||||||
gameStore.connection?.emit('weather', (response: WeatherState) => {
|
gameStore.connection?.emit('weather', (response: WeatherState) => {
|
||||||
if (!zoneStore.zone) return
|
if (zoneStore.zone?.zoneEffects) return
|
||||||
weatherState.value = response
|
weatherState.value = response
|
||||||
updateEffects()
|
updateEffects()
|
||||||
})
|
})
|
||||||
@ -148,6 +186,12 @@ const setupWeatherListeners = () => {
|
|||||||
weatherState.value = data
|
weatherState.value = data
|
||||||
updateEffects()
|
updateEffects()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Time updates
|
||||||
|
gameStore.connection?.on('date', () => {
|
||||||
|
if (zoneStore.zone?.zoneEffects) return
|
||||||
|
updateEffects()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watchers
|
// Watchers
|
||||||
|
Loading…
x
Reference in New Issue
Block a user