add sounds

This commit is contained in:
ILikePython256
2023-09-29 21:04:56 -06:00
parent 0b56a08165
commit 93dabe465b
3 changed files with 59 additions and 7 deletions

1
go.mod
View File

@@ -13,6 +13,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/hajimehoshi/file2byteslice v1.0.0 // indirect github.com/hajimehoshi/file2byteslice v1.0.0 // indirect
github.com/hajimehoshi/oto/v2 v2.4.1 // indirect
github.com/jezek/xgb v1.1.0 // indirect github.com/jezek/xgb v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect

2
go.sum
View File

@@ -31,6 +31,8 @@ github.com/hajimehoshi/file2byteslice v1.0.0/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334
github.com/hajimehoshi/go-mp3 v0.3.3/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM= github.com/hajimehoshi/go-mp3 v0.3.3/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI= github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo= github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo=
github.com/hajimehoshi/oto/v2 v2.4.1 h1:iTfZSulqdmQ5Hh4tVyVzNnK3aA4SgjbDapSM0YH3Lc4=
github.com/hajimehoshi/oto/v2 v2.4.1/go.mod h1:guyF8uIgSrchrKewS1E6Xyx7joUbKOi4g9W7vpcYBSc=
github.com/jakecoffman/cp v1.2.1/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg= github.com/jakecoffman/cp v1.2.1/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
github.com/jezek/xgb v1.0.1 h1:YUGhxps0aR7J2Xplbs23OHnV1mWaxFVcOl9b+1RQkt8= github.com/jezek/xgb v1.0.1 h1:YUGhxps0aR7J2Xplbs23OHnV1mWaxFVcOl9b+1RQkt8=
github.com/jezek/xgb v1.0.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= github.com/jezek/xgb v1.0.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=

53
main.go
View File

@@ -5,6 +5,7 @@ import (
"embed" "embed"
"image" "image"
_ "image/png" _ "image/png"
"io"
"io/fs" "io/fs"
"log" "log"
"math" "math"
@@ -13,6 +14,9 @@ import (
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/wav"
"crg.eti.br/go/config" "crg.eti.br/go/config"
_ "crg.eti.br/go/config/ini" _ "crg.eti.br/go/config/ini"
) )
@@ -37,24 +41,39 @@ type Config struct {
var ( var (
mSprite map[string]*ebiten.Image mSprite map[string]*ebiten.Image
mSound map[string][]byte
//go:embed assets/*.png //go:embed assets/*
f embed.FS f embed.FS
width = 32 width = 32
height = 32 height = 32
cfg = &Config{} cfg = &Config{}
currentplayer *audio.Player = nil
) )
func (m *neko) Layout(outsideWidth, outsideHeight int) (int, int) { func (m *neko) Layout(outsideWidth, outsideHeight int) (int, int) {
return width, height return width, height
} }
func playsound(sound []byte) {
if currentplayer != nil && currentplayer.IsPlaying() {
currentplayer.Close()
}
currentplayer = audio.CurrentContext().NewPlayerFromBytes(sound)
currentplayer.SetVolume(.3)
currentplayer.Play()
}
func (m *neko) Update() error { func (m *neko) Update() error {
mx, my := ebiten.CursorPosition() mx, my := ebiten.CursorPosition()
m.count++ m.count++
if m.state == 10 && m.count == m.min {
playsound(mSound["idle3"])
}
// sw, sh := ebiten.ScreenSizeInFullscreen() // sw, sh := ebiten.ScreenSizeInFullscreen()
@@ -103,6 +122,9 @@ func (m *neko) Update() error {
return nil return nil
} }
if m.state >= 13 {
playsound(mSound["awake"])
}
m.state = 0 m.state = 0
m.min = 8 m.min = 8
m.max = 16 m.max = 16
@@ -179,6 +201,10 @@ func (m *neko) Draw(screen *ebiten.Image) {
if m.state > 0 { if m.state > 0 {
m.state++ m.state++
switch m.state {
case 13:
playsound(mSound["sleep"])
}
} }
} }
@@ -204,19 +230,42 @@ func main() {
height *= int(cfg.Scale) height *= int(cfg.Scale)
mSprite = make(map[string]*ebiten.Image) mSprite = make(map[string]*ebiten.Image)
mSound = make(map[string][]byte)
a, _ := fs.ReadDir(f, "assets") a, _ := fs.ReadDir(f, "assets")
for _, v := range a { for _, v := range a {
data, _ := f.ReadFile("assets/" + v.Name()) data, _ := f.ReadFile("assets/" + v.Name())
name := strings.TrimSuffix(v.Name(), filepath.Ext(v.Name()))
ext := filepath.Ext(v.Name())
switch ext {
case ".png":
img, _, err := image.Decode(bytes.NewReader(data)) img, _, err := image.Decode(bytes.NewReader(data))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
name := strings.TrimSuffix(v.Name(), filepath.Ext(v.Name()))
mSprite[name] = ebiten.NewImageFromImage(img) mSprite[name] = ebiten.NewImageFromImage(img)
case ".wav":
stream, err := wav.DecodeWithSampleRate(44100, bytes.NewReader(data))
if err != nil {
log.Fatal(err)
} }
data, err := io.ReadAll(stream)
if err != nil {
log.Fatal(err)
}
mSound[name] = data
}
}
audio.NewContext(44100)
// Workaround: for some reason playing the first sound can incur significant delay.
// So let's do this at the start.
audio.CurrentContext().NewPlayerFromBytes([]byte{}).Play()
sw, sh := ebiten.ScreenSizeInFullscreen() sw, sh := ebiten.ScreenSizeInFullscreen()
n := &neko{ n := &neko{