1
0
forked from noxious/client

#184: Listen for weather updates from client

This commit is contained in:
Dennis Postma 2024-11-05 22:46:21 +01:00
parent 63146106c0
commit 58929290ab
2 changed files with 66 additions and 8 deletions

View File

@ -1,21 +1,35 @@
<template> <template>
<Scene name="effects" @preload="preloadScene" @create="createScene" @update="updateScene"> </Scene> <Scene name="effects" @preload="preloadScene" @create="createScene" @update="updateScene" />
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { Scene } from 'phavuer' import { Scene } from 'phavuer'
import { useZoneStore } from '@/stores/zoneStore' import { useZoneStore } from '@/stores/zoneStore'
import { useGameStore } from '@/stores/gameStore'
import { onBeforeUnmount, ref, watch } from 'vue' import { onBeforeUnmount, ref, watch } from 'vue'
import type { WeatherState } from '@/types'
// Stores
const gameStore = useGameStore()
const zoneStore = useZoneStore() const zoneStore = useZoneStore()
// Scene ref
const sceneRef = ref<Phaser.Scene | null>(null) const sceneRef = ref<Phaser.Scene | null>(null)
// Effect-related refs // Effect refs
const lightEffect = ref<Phaser.GameObjects.Graphics | null>(null) 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
const weatherState = ref<WeatherState>({
isRainEnabled: false,
rainPercentage: 0,
isFogEnabled: false,
fogDensity: 0
})
// Scene lifecycle methods
const preloadScene = async (scene: Phaser.Scene) => { const preloadScene = async (scene: Phaser.Scene) => {
scene.load.image('raindrop', 'assets/raindrop.png') scene.load.image('raindrop', 'assets/raindrop.png')
scene.load.image('fog', 'assets/fog.png') scene.load.image('fog', 'assets/fog.png')
@ -23,15 +37,21 @@ const preloadScene = async (scene: Phaser.Scene) => {
const createScene = async (scene: Phaser.Scene) => { const createScene = async (scene: Phaser.Scene) => {
sceneRef.value = scene sceneRef.value = scene
createLightEffect(scene) setupEffects(scene)
createRainEffect(scene) setupWeatherListeners()
createFogEffect(scene)
} }
const updateScene = () => { const updateScene = () => {
updateEffects() updateEffects()
} }
// Effect setup
const setupEffects = (scene: Phaser.Scene) => {
createLightEffect(scene)
createRainEffect(scene)
createFogEffect(scene)
}
const createLightEffect = (scene: Phaser.Scene) => { const createLightEffect = (scene: Phaser.Scene) => {
lightEffect.value = scene.add.graphics() lightEffect.value = scene.add.graphics()
lightEffect.value.setDepth(1000) lightEffect.value.setDepth(1000)
@ -59,9 +79,18 @@ const createFogEffect = (scene: Phaser.Scene) => {
fogSprite.value.setDepth(950) fogSprite.value.setDepth(950)
} }
// Effect updates
const updateEffects = () => { const updateEffects = () => {
const effects = zoneStore.zone?.zoneEffects || [] const effects = zoneStore.zone?.zoneEffects || []
if (effects.length > 0) {
updateZoneEffects(effects)
} else {
updateWeatherEffects()
}
}
const updateZoneEffects = (effects: any[]) => {
effects.forEach((effect) => { effects.forEach((effect) => {
switch (effect.effect) { switch (effect.effect) {
case 'light': case 'light':
@ -77,6 +106,11 @@ const updateEffects = () => {
}) })
} }
const updateWeatherEffects = () => {
updateRainEffect(weatherState.value.isRainEnabled ? weatherState.value.rainPercentage : 0)
updateFogEffect(weatherState.value.isFogEnabled ? weatherState.value.fogDensity * 100 : 0)
}
const updateLightEffect = (strength: number) => { const updateLightEffect = (strength: number) => {
if (!lightEffect.value) return if (!lightEffect.value) return
const darkness = 1 - strength / 100 const darkness = 1 - strength / 100
@ -100,11 +134,28 @@ const updateFogEffect = (strength: number) => {
fogSprite.value.setAlpha(strength / 100) fogSprite.value.setAlpha(strength / 100)
} }
watch(() => zoneStore.zone?.zoneEffects, updateEffects, { deep: true }) // Weather socket handlers
const setupWeatherListeners = () => {
onBeforeUnmount(() => { // Initial weather state
if (sceneRef.value) sceneRef.value.scene.remove('effects') gameStore.connection?.emit('weather', (response: WeatherState) => {
if (!zoneStore.zone) return
weatherState.value = response
updateEffects()
}) })
// @TODO : Fix resize issue // Weather updates
gameStore.connection?.on('weather', (data: WeatherState) => {
weatherState.value = data
updateEffects()
})
}
// Watchers
watch(() => zoneStore.zone?.zoneEffects, updateEffects, { deep: true })
// Cleanup
onBeforeUnmount(() => {
if (sceneRef.value) sceneRef.value.scene.remove('effects')
gameStore.connection?.off('weather')
})
</script> </script>

View File

@ -224,3 +224,10 @@ export type WorldSettings = {
isFogEnabled: boolean isFogEnabled: boolean
fogDensity: number fogDensity: number
} }
export type WeatherState = {
isRainEnabled: boolean
rainPercentage: number
isFogEnabled: boolean
fogDensity: number
}