Add music

This commit is contained in:
Trevor Slocum 2023-02-01 10:04:41 -08:00
parent 3b8602014d
commit a28dfd7d94
8 changed files with 69 additions and 5 deletions

View File

@ -1,21 +1,29 @@
package asset
import (
"bytes"
"embed"
"image"
"io"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
_ "image/png"
)
//go:embed image
//go:embed image sound
var FS embed.FS
const tileSize = 64
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)
func LoadImage(p string) *ebiten.Image {
f, err := FS.Open(p)
if err != nil {
@ -39,6 +47,36 @@ func LoadBytes(p string) []byte {
return b
}
func LoadOGG(context *audio.Context, p string, loop bool) *audio.Player {
b := LoadBytes(p)
stream, err := vorbis.DecodeWithSampleRate(sampleRate, bytes.NewReader(b))
if err != nil {
panic(err)
}
var s io.Reader
if loop {
s = audio.NewInfiniteLoop(stream, stream.Length())
} else {
s = stream
}
player, err := context.NewPlayer(s)
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 FrameAt(img *ebiten.Image, x int, y int) *ebiten.Image {
xPos, yPos := x*tileSize, y*tileSize
return img.SubImage(image.Rect(xPos, yPos, xPos+tileSize, yPos+tileSize)).(*ebiten.Image)

Binary file not shown.

View File

@ -23,6 +23,7 @@ func parseFlags() {
flag.StringVar(&hostAddress, "host", "", "start hosting a match against a remote opponent at the specified address:port")
flag.StringVar(&connectAddress, "connect", "", "connect to a match hosted by a remote opponent at the specified address:port")
flag.IntVar(&world.LocalPort, "local", 0, "set local port (this should be different from your opponent's local port)")
flag.BoolVar(&world.StartMuted, "mute", false, "mute music")
flag.IntVar(&world.Debug, "debug", 0, "debug level (0 - disabled, 1 - print fps and net stats, 2 - draw hitboxes)")
flag.BoolVar(&printDebug, "debug-ggpo", false, "print GGPO debug messages")
flag.IntVar(&world.TPS, "tps", world.DefaultTPS, "set ticks per second (this is not normally required)")

View File

@ -10,6 +10,7 @@ import (
"strings"
"time"
"code.rocketnine.space/tslocum/boxbrawl/asset"
"code.rocketnine.space/tslocum/boxbrawl/component"
"code.rocketnine.space/tslocum/boxbrawl/entity"
"code.rocketnine.space/tslocum/boxbrawl/system"
@ -38,10 +39,17 @@ func NewGame() (*Game, error) {
g.reset()
if !addedGame {
// Set up entity component system.
entity.NewOnceEntity()
gohan.AddSystem(&system.MapSystem{})
gohan.AddSystem(&system.PlayerSystem{})
gohan.AddSystem(&system.UISystem{})
// Start playing music.
if !world.StartMuted {
asset.SoundMusic.Play()
}
addedGame = true
}
@ -681,6 +689,15 @@ func (g *Game) Update() error {
}
}
// Toggle music.
if inpututil.IsKeyJustPressed(ebiten.KeyM) && ebiten.IsKeyPressed(ebiten.KeyControl) {
if asset.SoundMusic.IsPlaying() {
asset.SoundMusic.Pause()
} else {
asset.SoundMusic.Play()
}
}
// Change debug level.
if inpututil.IsKeyJustPressed(ebiten.KeyV) && ebiten.IsKeyPressed(ebiten.KeyControl) {
world.Debug++
@ -695,7 +712,7 @@ func (g *Game) Update() error {
if preset == world.TPS {
if i > 0 {
world.TPS = world.TPSPresets[i-1]
ebiten.SetTPS(world.TPS)
ebiten.SetMaxTPS(world.TPS)
log.Printf("Set TPS to %d", world.TPS)
break
}
@ -709,7 +726,7 @@ func (g *Game) Update() error {
if preset == world.TPS {
if i < len(world.TPSPresets)-1 {
world.TPS = world.TPSPresets[i+1]
ebiten.SetTPS(world.TPS)
ebiten.SetMaxTPS(world.TPS)
log.Printf("Set TPS to %d", world.TPS)
break
}

3
go.mod
View File

@ -12,7 +12,10 @@ require (
github.com/ebitengine/purego v0.1.1 // 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.3.1 // indirect
github.com/jezek/xgb v1.1.0 // indirect
github.com/jfreymuth/oggvorbis v1.0.4 // indirect
github.com/jfreymuth/vorbis v1.0.2 // indirect
golang.org/x/exp v0.0.0-20230131160201-f062dba9d201 // indirect
golang.org/x/exp/shiny v0.0.0-20230131160201-f062dba9d201 // indirect
golang.org/x/image v0.3.0 // indirect

3
go.sum
View File

@ -15,12 +15,15 @@ github.com/hajimehoshi/file2byteslice v1.0.0 h1:ljd5KTennqyJ4vG9i/5jS8MD1prof97v
github.com/hajimehoshi/file2byteslice v1.0.0/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
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 h1:qrLKpNus2UfD674oxckKjNJmesp9hMh7u7QCrStB3Rc=
github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo=
github.com/jakecoffman/cp v1.2.1/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
github.com/jezek/xgb v1.0.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/jfreymuth/oggvorbis v1.0.4 h1:cyJCd0XSoxkKzUPmqM0ZoQJ0h/WbhfyvUR+FTMxQEac=
github.com/jfreymuth/oggvorbis v1.0.4/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHixthRlBeD6uII=
github.com/jfreymuth/vorbis v1.0.2 h1:m1xH6+ZI4thH927pgKD8JOH4eaGRm18rEE9/0WKjvNE=
github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3TWXyuzrqQ=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/tslocum/ggpo v0.0.0-20230130221056-9cfe5a275c31 h1:4Lx50UoTEn6YHtHbN2NJe2WVJ95jO53svDIjXtGgep0=

View File

@ -45,7 +45,7 @@ func main() {
}
// Run normally.
ebiten.SetTPS(world.TPS)
ebiten.SetMaxTPS(world.TPS)
ebiten.SetFullscreen(world.Fullscreen)
if world.DisableVsync {
ebiten.SetFPSMode(ebiten.FPSModeVsyncOffMaximum)

View File

@ -45,6 +45,8 @@ var (
CamX, CamY = 0, 0 // TODO currently static
StartMuted bool // Start with music muted.
LocalPort int
ConnectPromptVisible = true // When false, we are connected