examples/audio: Refactoring: Reduce global variables (#302)

This commit is contained in:
Hajime Hoshi 2017-06-03 23:37:00 +09:00
parent 5ad5b2a8b8
commit d340a50188

View File

@ -57,15 +57,15 @@ type Player struct {
audioPlayer *audio.Player audioPlayer *audio.Player
total time.Duration total time.Duration
seekedCh chan error seekedCh chan error
mouseButtonState map[ebiten.MouseButton]int
keyState map[ebiten.Key]int
volume128 int
} }
var ( var (
musicPlayer *Player musicPlayer *Player
seBytes []byte seBytes []byte
seCh = make(chan []byte) seCh = make(chan []byte)
mouseButtonState = map[ebiten.MouseButton]int{}
keyState = map[ebiten.Key]int{}
volume128 = 128
) )
func playerBarRect() (x, y, w, h int) { func playerBarRect() (x, y, w, h int) {
@ -93,6 +93,9 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
audioContext: audioContext, audioContext: audioContext,
audioPlayer: p, audioPlayer: p,
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate, total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
mouseButtonState: map[ebiten.MouseButton]int{},
keyState: map[ebiten.Key]int{},
volume128: 128,
} }
player.audioPlayer.Play() player.audioPlayer.Play()
return player, nil return player, nil
@ -123,11 +126,11 @@ func (p *Player) updateSE() {
return return
} }
if !ebiten.IsKeyPressed(ebiten.KeyP) { if !ebiten.IsKeyPressed(ebiten.KeyP) {
keyState[ebiten.KeyP] = 0 p.keyState[ebiten.KeyP] = 0
return return
} }
keyState[ebiten.KeyP]++ p.keyState[ebiten.KeyP]++
if keyState[ebiten.KeyP] != 1 { if p.keyState[ebiten.KeyP] != 1 {
return return
} }
sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, seBytes) sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, seBytes)
@ -136,27 +139,27 @@ func (p *Player) updateSE() {
func (p *Player) updateVolume() { func (p *Player) updateVolume() {
if ebiten.IsKeyPressed(ebiten.KeyZ) { if ebiten.IsKeyPressed(ebiten.KeyZ) {
volume128-- p.volume128--
} }
if ebiten.IsKeyPressed(ebiten.KeyX) { if ebiten.IsKeyPressed(ebiten.KeyX) {
volume128++ p.volume128++
} }
if volume128 < 0 { if p.volume128 < 0 {
volume128 = 0 p.volume128 = 0
} }
if 128 < volume128 { if 128 < p.volume128 {
volume128 = 128 p.volume128 = 128
} }
p.audioPlayer.SetVolume(float64(volume128) / 128) p.audioPlayer.SetVolume(float64(p.volume128) / 128)
} }
func (p *Player) updatePlayPause() { func (p *Player) updatePlayPause() {
if !ebiten.IsKeyPressed(ebiten.KeyS) { if !ebiten.IsKeyPressed(ebiten.KeyS) {
keyState[ebiten.KeyS] = 0 p.keyState[ebiten.KeyS] = 0
return return
} }
keyState[ebiten.KeyS]++ p.keyState[ebiten.KeyS]++
if keyState[ebiten.KeyS] != 1 { if p.keyState[ebiten.KeyS] != 1 {
return return
} }
if p.audioPlayer.IsPlaying() { if p.audioPlayer.IsPlaying() {
@ -171,11 +174,11 @@ func (p *Player) updateBar() {
return return
} }
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
mouseButtonState[ebiten.MouseButtonLeft] = 0 p.mouseButtonState[ebiten.MouseButtonLeft] = 0
return return
} }
mouseButtonState[ebiten.MouseButtonLeft]++ p.mouseButtonState[ebiten.MouseButtonLeft]++
if mouseButtonState[ebiten.MouseButtonLeft] != 1 { if p.mouseButtonState[ebiten.MouseButtonLeft] != 1 {
return return
} }
x, y := ebiten.CursorPosition() x, y := ebiten.CursorPosition()