2020-07-30 16:17:26 -04:00
|
|
|
package d2audio
|
|
|
|
|
|
|
|
import (
|
2020-07-30 21:50:27 -04:00
|
|
|
"math/rand"
|
|
|
|
|
2020-07-30 16:17:26 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
|
|
|
)
|
|
|
|
|
2020-08-05 01:17:25 -04:00
|
|
|
const assumedFPS = 25
|
|
|
|
|
|
|
|
// SoundEnvironment represents the audio environment for map areas
|
2020-07-30 16:17:26 -04:00
|
|
|
type SoundEnvironment struct {
|
|
|
|
environment *d2datadict.SoundEnvironRecord
|
|
|
|
engine *SoundEngine
|
|
|
|
bgm *Sound
|
|
|
|
ambiance *Sound
|
|
|
|
eventTimer float64
|
|
|
|
}
|
|
|
|
|
2020-08-05 01:17:25 -04:00
|
|
|
// NewSoundEnvironment creates a SoundEnvironment using the given SoundEngine
|
2020-07-30 16:17:26 -04:00
|
|
|
func NewSoundEnvironment(soundEngine *SoundEngine) SoundEnvironment {
|
|
|
|
r := SoundEnvironment{
|
|
|
|
// Start with env NONE
|
|
|
|
environment: d2datadict.SoundEnvirons[0],
|
|
|
|
engine: soundEngine,
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-08-05 01:17:25 -04:00
|
|
|
// SetEnv sets the sound environment using the given record index
|
|
|
|
func (s *SoundEnvironment) SetEnv(environmentIdx int) {
|
|
|
|
if s.environment.Index != environmentIdx {
|
|
|
|
newEnv := d2datadict.SoundEnvirons[environmentIdx]
|
2020-07-30 16:17:26 -04:00
|
|
|
|
|
|
|
if s.environment.Song != newEnv.Song {
|
|
|
|
if s.bgm != nil {
|
|
|
|
s.bgm.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.bgm = s.engine.PlaySoundID(newEnv.Song)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.environment.DayAmbience != newEnv.DayAmbience {
|
|
|
|
if s.ambiance != nil {
|
|
|
|
s.ambiance.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.ambiance = s.engine.PlaySoundID(newEnv.DayAmbience)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.environment = newEnv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 01:17:25 -04:00
|
|
|
// Advance advances the sound engine and plays sounds when necessary
|
2020-07-30 16:17:26 -04:00
|
|
|
func (s *SoundEnvironment) Advance(elapsed float64) {
|
|
|
|
s.eventTimer -= elapsed
|
|
|
|
if s.eventTimer < 0 {
|
2020-08-05 01:17:25 -04:00
|
|
|
s.eventTimer = float64(s.environment.EventDelay) / assumedFPS
|
2020-07-30 21:50:27 -04:00
|
|
|
|
|
|
|
snd := s.engine.PlaySoundID(s.environment.DayEvent)
|
|
|
|
if snd != nil {
|
|
|
|
pan := (rand.Float64() * 2) - 1
|
|
|
|
snd.SetPan(pan)
|
|
|
|
}
|
2020-07-30 16:17:26 -04:00
|
|
|
}
|
|
|
|
}
|