mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
examples/audio: Refactoring: Reduce global variables (#302)
This commit is contained in:
parent
5ad5b2a8b8
commit
d340a50188
@ -53,19 +53,19 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
audioContext *audio.Context
|
audioContext *audio.Context
|
||||||
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) {
|
||||||
@ -90,9 +90,12 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
player := &Player{
|
player := &Player{
|
||||||
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()
|
||||||
|
Loading…
Reference in New Issue
Block a user