add sounds
This commit is contained in:
1
go.mod
1
go.mod
@@ -13,6 +13,7 @@ require (
|
||||
github.com/fsnotify/fsnotify v1.6.0 // 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/oto/v2 v2.4.1 // indirect
|
||||
github.com/jezek/xgb v1.1.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -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/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.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/jezek/xgb v1.0.1 h1:YUGhxps0aR7J2Xplbs23OHnV1mWaxFVcOl9b+1RQkt8=
|
||||
github.com/jezek/xgb v1.0.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
||||
|
||||
63
main.go
63
main.go
@@ -5,6 +5,7 @@ import (
|
||||
"embed"
|
||||
"image"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"math"
|
||||
@@ -13,6 +14,9 @@ import (
|
||||
|
||||
"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/ini"
|
||||
)
|
||||
@@ -37,24 +41,39 @@ type Config struct {
|
||||
|
||||
var (
|
||||
mSprite map[string]*ebiten.Image
|
||||
mSound map[string][]byte
|
||||
|
||||
//go:embed assets/*.png
|
||||
//go:embed assets/*
|
||||
f embed.FS
|
||||
|
||||
width = 32
|
||||
height = 32
|
||||
|
||||
cfg = &Config{}
|
||||
|
||||
currentplayer *audio.Player = nil
|
||||
)
|
||||
|
||||
func (m *neko) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
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 {
|
||||
mx, my := ebiten.CursorPosition()
|
||||
|
||||
m.count++
|
||||
if m.state == 10 && m.count == m.min {
|
||||
playsound(mSound["idle3"])
|
||||
}
|
||||
|
||||
// sw, sh := ebiten.ScreenSizeInFullscreen()
|
||||
|
||||
@@ -103,6 +122,9 @@ func (m *neko) Update() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if m.state >= 13 {
|
||||
playsound(mSound["awake"])
|
||||
}
|
||||
m.state = 0
|
||||
m.min = 8
|
||||
m.max = 16
|
||||
@@ -179,6 +201,10 @@ func (m *neko) Draw(screen *ebiten.Image) {
|
||||
|
||||
if m.state > 0 {
|
||||
m.state++
|
||||
switch m.state {
|
||||
case 13:
|
||||
playsound(mSound["sleep"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,20 +230,43 @@ func main() {
|
||||
height *= int(cfg.Scale)
|
||||
|
||||
mSprite = make(map[string]*ebiten.Image)
|
||||
mSound = make(map[string][]byte)
|
||||
|
||||
a, _ := fs.ReadDir(f, "assets")
|
||||
for _, v := range a {
|
||||
data, _ := f.ReadFile("assets/" + v.Name())
|
||||
|
||||
img, _, err := image.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
name := strings.TrimSuffix(v.Name(), filepath.Ext(v.Name()))
|
||||
mSprite[name] = ebiten.NewImageFromImage(img)
|
||||
ext := filepath.Ext(v.Name())
|
||||
|
||||
switch ext {
|
||||
case ".png":
|
||||
img, _, err := image.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
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()
|
||||
n := &neko{
|
||||
x: sw / 2,
|
||||
|
||||
Reference in New Issue
Block a user