Add hit sound

This commit is contained in:
Trevor Slocum 2023-02-01 10:45:53 -08:00
parent a28dfd7d94
commit 3483dd4174
3 changed files with 45 additions and 3 deletions

View File

@ -6,10 +6,12 @@ import (
"image"
"io"
_ "image/png"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
_ "image/png"
"github.com/hajimehoshi/ebiten/v2/audio/wav"
)
//go:embed image sound
@ -21,8 +23,12 @@ var ImgPlayer = LoadImage("image/player.png")
const sampleRate = 44100
var audioContext = audio.NewContext(sampleRate)
var SoundMusic = LoadOGG(audioContext, "sound/an_individual_who_fights_on_roads.ogg", true)
var (
audioContext = audio.NewContext(sampleRate)
SoundHitP1 = LoadWAV(audioContext, "sound/hit.wav")
SoundHitP2 = LoadWAV(audioContext, "sound/hit.wav")
SoundMusic = LoadOGG(audioContext, "sound/an_individual_who_fights_on_roads.ogg", true)
)
func LoadImage(p string) *ebiten.Image {
f, err := FS.Open(p)
@ -47,6 +53,33 @@ func LoadBytes(p string) []byte {
return b
}
func LoadWAV(context *audio.Context, p string) *audio.Player {
f, err := FS.Open(p)
if err != nil {
panic(err)
}
defer f.Close()
stream, err := wav.DecodeWithSampleRate(sampleRate, f)
if err != nil {
panic(err)
}
player, err := context.NewPlayer(stream)
if err != nil {
panic(err)
}
// Workaround to prevent delays when playing for the first time.
player.SetVolume(0)
player.Play()
player.Pause()
player.Rewind()
player.SetVolume(1)
return player
}
func LoadOGG(context *audio.Context, p string, loop bool) *audio.Player {
b := LoadBytes(p)

BIN
asset/sound/hit.wav Normal file

Binary file not shown.

View File

@ -543,6 +543,15 @@ func (g *Game) UpdateByInputs(inputs []InputBits) {
if g.Players[opp].Action != component.ActionBlock {
opponent.Action = component.ActionStunned
opponent.ActionTicksLeft = stunTicks
// Play hit sound.
sound := asset.SoundHitP1
if i == 1 {
sound = asset.SoundHitP2
}
sound.SetVolume(0.2)
sound.Rewind()
sound.Play()
}
}
}