Merge remote-tracking branch 'origin/main' into feature/map-refactor

This commit is contained in:
Dennis Postma 2025-01-25 02:38:13 +01:00
commit 807bc2066e
4 changed files with 45 additions and 57 deletions

View File

@ -242,15 +242,11 @@ export type Chat = {
export type WorldSettings = { export type WorldSettings = {
date: Date date: Date
isRainEnabled: boolean weatherState: WeatherState
isFogEnabled: boolean
fogDensity: number
} }
export type WeatherState = { export type WeatherState = {
isRainEnabled: boolean
rainPercentage: number rainPercentage: number
isFogEnabled: boolean
fogDensity: number fogDensity: number
} }

View File

@ -15,7 +15,8 @@ const LIGHT_CONFIG = {
SUNRISE_HOUR: 6, SUNRISE_HOUR: 6,
SUNSET_HOUR: 20, SUNSET_HOUR: 20,
DAY_STRENGTH: 100, DAY_STRENGTH: 100,
NIGHT_STRENGTH: 30 NIGHT_STRENGTH: 30,
TRANSITION_HOURS: 3
} }
// Stores and refs // Stores and refs
@ -23,7 +24,6 @@ const gameStore = useGameStore()
const mapStore = useMapStore() const mapStore = useMapStore()
const mapStorage = new MapStorage() const mapStorage = new MapStorage()
const sceneRef = ref<Phaser.Scene | null>(null) const sceneRef = ref<Phaser.Scene | null>(null)
const mapEffectsReady = ref(false)
const mapObject = ref<Map | null>(null) const mapObject = ref<Map | null>(null)
// Effect objects // Effect objects
@ -35,9 +35,7 @@ const effects = {
// Weather state // Weather state
const weatherState = ref<WeatherState>({ const weatherState = ref<WeatherState>({
isRainEnabled: false,
rainPercentage: 0, rainPercentage: 0,
isFogEnabled: false,
fogDensity: 0 fogDensity: 0
}) })
@ -63,7 +61,6 @@ watch(
() => mapStore.mapId, () => mapStore.mapId,
async (newMapId) => { async (newMapId) => {
if (newMapId) { if (newMapId) {
mapEffectsReady.value = false
await loadMap() await loadMap()
updateScene() updateScene()
} }
@ -109,54 +106,54 @@ const updateScene = () => {
{} {}
) as { [key: string]: number } ) as { [key: string]: number }
// Only update effects once mapEffects are loaded const finalEffects = {...mapEffects,
if (!mapEffectsReady.value) {
if (mapObject.value) {
mapEffectsReady.value = true
} else {
return
}
}
const finalEffects =
mapEffects && Object.keys(mapEffects).length
? mapEffects
: {
light: timeBasedLight, light: timeBasedLight,
rain: weatherState.value.isRainEnabled ? weatherState.value.rainPercentage : 0, rain: weatherState.value.rainPercentage,
fog: weatherState.value.isFogEnabled ? weatherState.value.fogDensity * 100 : 0 fog: weatherState.value.fogDensity }
} console.log(finalEffects)
applyEffects(finalEffects) applyEffects(finalEffects)
} }
const applyEffects = (effectValues: any) => { const applyEffects = (effectValues: any) => {
if (effects.light.value) { if (effects.light.value) {
const darkness = 1 - (effectValues.light ?? 100) / 100 const darkness = 1 - (effectValues.light) / 100
effects.light.value.clear().fillStyle(0x000000, darkness).fillRect(0, 0, window.innerWidth, window.innerHeight) effects.light.value.clear().fillStyle(0x000000, darkness).fillRect(0, 0, window.innerWidth, window.innerHeight)
} }
if (effects.rain.value) { if (effects.rain)
const strength = effectValues.rain ?? 0 effects.rain.value.start().setQuantity(effectValues.rain / 10)
strength > 0 ? effects.rain.value.start().setQuantity(Math.floor((strength / 100) * 10)) : effects.rain.value.stop() else effects.rain.value.stop()
}
if (effects.fog.value) { effects.fog.value.setAlpha(effectValues.fog/100)
effects.fog.value.setAlpha((effectValues.fog ?? 0) / 100)
}
} }
// Linear Interpolation
// moves a value between x and y where a is the percentage of progress the value has made in moving from x to y
// a = 0 return x
// a = 0.5 return (x+y)/2
// a = 1 return y
const lerp = (x,y,a) => x * (1-a) + y * a
const calculateLightStrength = (time: Date): number => { const calculateLightStrength = (time: Date): number => {
const hour = time.getHours() const hour = time.getHours()
const minute = time.getMinutes() const minute = time.getMinutes()
if (hour >= LIGHT_CONFIG.SUNSET_HOUR || hour < LIGHT_CONFIG.SUNRISE_HOUR) return LIGHT_CONFIG.NIGHT_STRENGTH
if (hour > LIGHT_CONFIG.SUNRISE_HOUR && hour < LIGHT_CONFIG.SUNSET_HOUR - 2) return LIGHT_CONFIG.DAY_STRENGTH
if (hour === LIGHT_CONFIG.SUNRISE_HOUR) return LIGHT_CONFIG.NIGHT_STRENGTH + ((LIGHT_CONFIG.DAY_STRENGTH - LIGHT_CONFIG.NIGHT_STRENGTH) * minute) / 60
const totalMinutes = (hour - (LIGHT_CONFIG.SUNSET_HOUR - 2)) * 60 + minute const totalMinutes = (hour - (LIGHT_CONFIG.SUNSET_HOUR - 2)) * 60 + minute
return LIGHT_CONFIG.DAY_STRENGTH - (LIGHT_CONFIG.DAY_STRENGTH - LIGHT_CONFIG.NIGHT_STRENGTH) * (totalMinutes / 120)
//Transition from daylight to night
if (hour >= LIGHT_CONFIG.SUNSET_HOUR - LIGHT_CONFIG.TRANSITION_HOURS && hour < LIGHT_CONFIG.SUNSET_HOUR)
return lerp(LIGHT_CONFIG.DAY_STRENGTH, LIGHT_CONFIG.NIGHT_STRENGTH,
(hour+(minute/60)-(LIGHT_CONFIG.SUNSET_HOUR-LIGHT_CONFIG.TRANSITION_HOURS))/LIGHT_CONFIG.TRANSITION_HOURS)
//Transition from sunrise to morning
else if (hour >= LIGHT_CONFIG.SUNRISE_HOUR && hour < LIGHT_CONFIG.SUNRISE_HOUR + LIGHT_CONFIG.TRANSITION_HOURS)
return lerp(LIGHT_CONFIG.NIGHT_STRENGTH, LIGHT_CONFIG.DAY_STRENGTH,
(hour+(minute/60)-LIGHT_CONFIG.SUNRISE_HOUR)/LIGHT_CONFIG.TRANSITION_HOURS)
else if (hour > LIGHT_CONFIG.SUNRISE_HOUR && hour < LIGHT_CONFIG.SUNSET_HOUR)
return LIGHT_CONFIG.DAY_STRENGTH
else
return LIGHT_CONFIG.NIGHT_STRENGTH
} }
// Socket and window handlers // Socket and window handlers
@ -180,14 +177,9 @@ const handleResize = () => {
} }
// Lifecycle // Lifecycle
watch( watch(() => mapObject.value, () => {
() => mapObject.value,
() => {
mapEffectsReady.value = false
updateScene() updateScene()
}, })
{ deep: true }
)
onMounted(() => window.addEventListener('resize', handleResize)) onMounted(() => window.addEventListener('resize', handleResize))

View File

@ -1,5 +1,5 @@
<template> <template>
<Modal v-for="notification in gameStore.notifications" :key="notification.id" :isModalOpen="true" @modal:close="closeNotification(notification.id)"> <Modal v-for="notification in gameStore.notifications" :key="notification.id" :isModalOpen="true" @modal:close="closeNotification(notification!.id as string)">
<template #modalHeader v-if="notification.title"> <template #modalHeader v-if="notification.title">
<h3 class="m-0 font-medium shrink-0 text-white">{{ notification.title }}</h3> <h3 class="m-0 font-medium shrink-0 text-white">{{ notification.title }}</h3>
</template> </template>

View File

@ -15,9 +15,10 @@ export const useGameStore = defineStore('game', {
character: null as Character | null, character: null as Character | null,
world: { world: {
date: new Date(), date: new Date(),
isRainEnabled: false, weatherState: {
isFogEnabled: false, rainPercentage: 0,
fogDensity: 0 fogDensity: 0
}
} as WorldSettings, } as WorldSettings,
game: { game: {
isLoading: false, isLoading: false,
@ -119,9 +120,8 @@ export const useGameStore = defineStore('game', {
this.uiSettings.isGmPanelOpen = false this.uiSettings.isGmPanelOpen = false
this.world.date = new Date() this.world.date = new Date()
this.world.isRainEnabled = false this.world.weatherState.rainPercentage = 0
this.world.isFogEnabled = false this.world.weatherState.fogDensity = 0
this.world.fogDensity = 0
} }
} }
}) })