
.soundData
Data 7	; # of sound files
Data "step1.mp3"
Data "step2.mp3"
Data "ching.mp3"
Data "bell.mp3"
Data "strings.mp3"
Data "circuit.mp3"
Data "trance.mp3"

Function LoadSounds()
	Restore soundData
	Read NumberOfSounds%
	
	For i = 1 To NumberOfSounds
		Read filename$
		
		sound = LoadSound("sounds\" + filename)
		
		If sound = 0 Then
			DebugLog "unable to load sounds\" + filename
		Else
			DebugLog "loaded sounds\" + filename
			s.SndEffect = New SndEffect
			s\queued = 0.0
			s\volume = 0.0
			s\name  = Left(filename, Instr(filename, ".")-1)
			s\sound = sound
			
			;LoopSound s\sound
			;s\channel = PlaySound(s\sound)
			;ChannelVolume s\channel,s\volume
		EndIf
	Next
End Function
			

Function QueueSound(name$, volume#)
	For s.SndEffect = Each SndEffect
		If s\name = name Then s\queued = volume : Return
	Next
End Function

Global currentsound.SndEffect = First SndEffect

Function UpdateSound()
	Select archer\state
		Case AT_CORE
			QueueSound("bell",  1.0)
		Case AT_SPACE
			QueueSound("circuit", 0.9)
			QueueSound("trance",  0.5)
			QueueSound("strings", 0.9)
		Case AT_SURFACE
			QueueSound("trance",  0.6)
			QueueSound("ching",   1.0)
	End Select
	Select archer\animode
		Case A_STARTWALK
			QueueSound("step1", 0.5)
			QueueSound("ching", 0.9)
		Case A_WALK
			QueueSound("step1", 0.5)
			QueueSound("ching", 1.0)
		Case A_STARTRUN
			QueueSound("step1", 0.5)
			QueueSound("step2", 0.5)
			QueueSound("ching", 1.0)
			QueueSound("bell",  1.0)
		Case A_RUN
			QueueSound("step1", 0.7)
			QueueSound("step2", 0.7)
			QueueSound("ching", 1.0)
			QueueSound("bell",  1.0)
	End Select

	For s.SndEffect = Each SndEffect
		If s\queued <= 0.0 Then
			StopChannel(s\channel)
		ElseIf ChannelPlaying(s\channel) Then
			ChannelVolume s\channel,s\volume
			s\queued = 0.0
		Else
			s\volume = s\queued
			If s\volume > 0.0 Then
				s\channel = PlaySound(s\sound)
				ChannelVolume s\channel,s\volume
			EndIf
		EndIf
		;DebugLog "Channel Volume:  " + s\volume#
	Next
	
	;If KeyHit(26) And currentsound <> First SndEffect Then ; '['
	;	currentsound = Before currentsound
	;	DebugLog "["
	;Else If KeyHit(27) And currentsound <> Last SndEffect Then ; ']'
	;	currentsound = After currentsound
	;	DebugLog "]"
	;Else If KeyHit(50) And currentsound\volume < 1.0 Then ; 'M'ore
	;	currentsound\volume# = currentsound\volume# + 0.5
	;	DebugLog "M"
	;Else If KeyHit(38) And currentsound\volume > 0.0 Then ; 'L'ess
	;	currentsound\volume# = currentsound\volume# - 0.5
	;	DebugLog "L"
	;EndIf
End Function