mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
parent
e8ea4046cb
commit
5c4885c988
@ -112,6 +112,7 @@ func (t musicType) String() string {
|
|||||||
|
|
||||||
// Player represents the current audio state.
|
// Player represents the current audio state.
|
||||||
type Player struct {
|
type Player struct {
|
||||||
|
game *Game
|
||||||
audioContext *audio.Context
|
audioContext *audio.Context
|
||||||
audioPlayer *audio.Player
|
audioPlayer *audio.Player
|
||||||
current time.Duration
|
current time.Duration
|
||||||
@ -129,7 +130,7 @@ func playerBarRect() (x, y, w, h int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlayer(audioContext *audio.Context, musicType musicType) (*Player, error) {
|
func NewPlayer(game *Game, audioContext *audio.Context, musicType musicType) (*Player, error) {
|
||||||
type audioStream interface {
|
type audioStream interface {
|
||||||
io.ReadSeeker
|
io.ReadSeeker
|
||||||
Length() int64
|
Length() int64
|
||||||
@ -160,6 +161,7 @@ func NewPlayer(audioContext *audio.Context, musicType musicType) (*Player, error
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
player := &Player{
|
player := &Player{
|
||||||
|
game: game,
|
||||||
audioContext: audioContext,
|
audioContext: audioContext,
|
||||||
audioPlayer: p,
|
audioPlayer: p,
|
||||||
total: time.Second * time.Duration(s.Length()) / bytesPerSample / sampleRate,
|
total: time.Second * time.Duration(s.Length()) / bytesPerSample / sampleRate,
|
||||||
@ -232,7 +234,7 @@ func (p *Player) shouldPlaySE() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, id := range inpututil.JustPressedTouchIDs() {
|
for _, id := range p.game.justPressedTouchIDs {
|
||||||
if image.Pt(ebiten.TouchPosition(id)).In(r) {
|
if image.Pt(ebiten.TouchPosition(id)).In(r) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -277,7 +279,7 @@ func (p *Player) shouldSwitchPlayStateIfNeeded() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, id := range inpututil.JustPressedTouchIDs() {
|
for _, id := range p.game.justPressedTouchIDs {
|
||||||
if image.Pt(ebiten.TouchPosition(id)).In(r) {
|
if image.Pt(ebiten.TouchPosition(id)).In(r) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -296,14 +298,14 @@ func (p *Player) switchPlayStateIfNeeded() {
|
|||||||
p.audioPlayer.Play()
|
p.audioPlayer.Play()
|
||||||
}
|
}
|
||||||
|
|
||||||
func justPressedPosition() (int, int, bool) {
|
func (p *Player) justPressedPosition() (int, int, bool) {
|
||||||
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
||||||
x, y := ebiten.CursorPosition()
|
x, y := ebiten.CursorPosition()
|
||||||
return x, y, true
|
return x, y, true
|
||||||
}
|
}
|
||||||
|
|
||||||
if ts := inpututil.JustPressedTouchIDs(); len(ts) > 0 {
|
if len(p.game.justPressedTouchIDs) > 0 {
|
||||||
x, y := ebiten.TouchPosition(ts[0])
|
x, y := ebiten.TouchPosition(p.game.justPressedTouchIDs[0])
|
||||||
return x, y, true
|
return x, y, true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,7 +314,7 @@ func justPressedPosition() (int, int, bool) {
|
|||||||
|
|
||||||
func (p *Player) seekBarIfNeeded() {
|
func (p *Player) seekBarIfNeeded() {
|
||||||
// Calculate the next seeking position from the current cursor position.
|
// Calculate the next seeking position from the current cursor position.
|
||||||
x, y, ok := justPressedPosition()
|
x, y, ok := p.justPressedPosition()
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -376,21 +378,25 @@ type Game struct {
|
|||||||
musicPlayer *Player
|
musicPlayer *Player
|
||||||
musicPlayerCh chan *Player
|
musicPlayerCh chan *Player
|
||||||
errCh chan error
|
errCh chan error
|
||||||
|
|
||||||
|
justPressedTouchIDs []ebiten.TouchID
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGame() (*Game, error) {
|
func NewGame() (*Game, error) {
|
||||||
audioContext := audio.NewContext(sampleRate)
|
audioContext := audio.NewContext(sampleRate)
|
||||||
|
|
||||||
m, err := NewPlayer(audioContext, typeOgg)
|
g := &Game{
|
||||||
|
musicPlayerCh: make(chan *Player),
|
||||||
|
errCh: make(chan error),
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err := NewPlayer(g, audioContext, typeOgg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Game{
|
g.musicPlayer = m
|
||||||
musicPlayer: m,
|
return g, nil
|
||||||
musicPlayerCh: make(chan *Player),
|
|
||||||
errCh: make(chan error),
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) Update() error {
|
func (g *Game) Update() error {
|
||||||
@ -402,6 +408,8 @@ func (g *Game) Update() error {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g.justPressedTouchIDs = inpututil.AppendJustPressedTouchIDs(g.justPressedTouchIDs[:0])
|
||||||
|
|
||||||
if g.musicPlayer != nil && inpututil.IsKeyJustPressed(ebiten.KeyA) {
|
if g.musicPlayer != nil && inpututil.IsKeyJustPressed(ebiten.KeyA) {
|
||||||
var t musicType
|
var t musicType
|
||||||
switch g.musicPlayer.musicType {
|
switch g.musicPlayer.musicType {
|
||||||
@ -417,7 +425,7 @@ func (g *Game) Update() error {
|
|||||||
g.musicPlayer = nil
|
g.musicPlayer = nil
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
p, err := NewPlayer(audio.CurrentContext(), t)
|
p, err := NewPlayer(g, audio.CurrentContext(), t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.errCh <- err
|
g.errCh <- err
|
||||||
return
|
return
|
||||||
|
@ -184,6 +184,7 @@ func (s *Stroke) SetDraggingObject(object interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
|
touchIDs []ebiten.TouchID
|
||||||
strokes map[*Stroke]struct{}
|
strokes map[*Stroke]struct{}
|
||||||
sprites []*Sprite
|
sprites []*Sprite
|
||||||
}
|
}
|
||||||
@ -273,7 +274,8 @@ func (g *Game) Update() error {
|
|||||||
s.SetDraggingObject(g.spriteAt(s.Position()))
|
s.SetDraggingObject(g.spriteAt(s.Position()))
|
||||||
g.strokes[s] = struct{}{}
|
g.strokes[s] = struct{}{}
|
||||||
}
|
}
|
||||||
for _, id := range inpututil.JustPressedTouchIDs() {
|
g.touchIDs = inpututil.AppendJustPressedTouchIDs(g.touchIDs[:0])
|
||||||
|
for _, id := range g.touchIDs {
|
||||||
s := NewStroke(&TouchStrokeSource{id})
|
s := NewStroke(&TouchStrokeSource{id})
|
||||||
s.SetDraggingObject(g.spriteAt(s.Position()))
|
s.SetDraggingObject(g.spriteAt(s.Position()))
|
||||||
g.strokes[s] = struct{}{}
|
g.strokes[s] = struct{}{}
|
||||||
|
@ -178,6 +178,7 @@ type Game struct {
|
|||||||
gameoverCount int
|
gameoverCount int
|
||||||
|
|
||||||
keys []ebiten.Key
|
keys []ebiten.Key
|
||||||
|
touchIDs []ebiten.TouchID
|
||||||
gamepadIDs []ebiten.GamepadID
|
gamepadIDs []ebiten.GamepadID
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,7 +216,8 @@ func (g *Game) jump() bool {
|
|||||||
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if len(inpututil.JustPressedTouchIDs()) > 0 {
|
g.touchIDs = inpututil.AppendJustPressedTouchIDs(g.touchIDs)
|
||||||
|
if len(g.touchIDs) > 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
g.gamepadIDs = ebiten.AppendGamepadIDs(g.gamepadIDs[:0])
|
g.gamepadIDs = ebiten.AppendGamepadIDs(g.gamepadIDs[:0])
|
||||||
|
@ -110,7 +110,8 @@ func (g *Game) Update() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// What touches are new in this frame?
|
// What touches are new in this frame?
|
||||||
for _, id := range inpututil.JustPressedTouchIDs() {
|
g.touchIDs = inpututil.AppendJustPressedTouchIDs(g.touchIDs[:0])
|
||||||
|
for _, id := range g.touchIDs {
|
||||||
x, y := ebiten.TouchPosition(id)
|
x, y := ebiten.TouchPosition(id)
|
||||||
g.touches[id] = &touch{
|
g.touches[id] = &touch{
|
||||||
originX: x, originY: y,
|
originX: x, originY: y,
|
||||||
|
@ -253,8 +253,6 @@ func MouseButtonPressDuration(button ebiten.MouseButton) int {
|
|||||||
// and returns the extended buffer.
|
// and returns the extended buffer.
|
||||||
// Giving a slice that already has enough capacity works efficiently.
|
// Giving a slice that already has enough capacity works efficiently.
|
||||||
//
|
//
|
||||||
// AppendJustConnectedGamepadIDs might append nothing when there is no connected gamepad.
|
|
||||||
//
|
|
||||||
// AppendJustConnectedGamepadIDs is concurrent safe.
|
// AppendJustConnectedGamepadIDs is concurrent safe.
|
||||||
func AppendJustConnectedGamepadIDs(gamepadIDs []ebiten.GamepadID) []ebiten.GamepadID {
|
func AppendJustConnectedGamepadIDs(gamepadIDs []ebiten.GamepadID) []ebiten.GamepadID {
|
||||||
origLen := len(gamepadIDs)
|
origLen := len(gamepadIDs)
|
||||||
@ -330,24 +328,32 @@ func GamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.GamepadButton
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// JustPressedTouchIDs returns touch IDs that are created just in the current frame.
|
// AppendJustPressedTouchIDs append touch IDs that are created just in the current frame to touchIDs,
|
||||||
|
// and returns the extended buffer.
|
||||||
|
// Giving a slice that already has enough capacity works efficiently.
|
||||||
//
|
//
|
||||||
// JustPressedTouchIDs might return nil when there is not touch.
|
// AppendJustPressedTouchIDs is concurrent safe.
|
||||||
//
|
func AppendJustPressedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
|
||||||
// JustPressedTouchIDs is concurrent safe.
|
origLen := len(touchIDs)
|
||||||
func JustPressedTouchIDs() []ebiten.TouchID {
|
|
||||||
var ids []ebiten.TouchID
|
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
for id, s := range theInputState.touchDurations {
|
for id, s := range theInputState.touchDurations {
|
||||||
if s == 1 {
|
if s == 1 {
|
||||||
ids = append(ids, id)
|
touchIDs = append(touchIDs, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
theInputState.m.RUnlock()
|
theInputState.m.RUnlock()
|
||||||
sort.Slice(ids, func(a, b int) bool {
|
s := touchIDs[origLen:]
|
||||||
return ids[a] < ids[b]
|
sort.Slice(s, func(a, b int) bool {
|
||||||
|
return s[a] < s[b]
|
||||||
})
|
})
|
||||||
return ids
|
return touchIDs
|
||||||
|
}
|
||||||
|
|
||||||
|
// JustPressedTouchIDs returns touch IDs that are created just in the current frame.
|
||||||
|
//
|
||||||
|
// Deprecated: as of v2.2. Use AppendJustPressedTouchIDs instead.
|
||||||
|
func JustPressedTouchIDs() []ebiten.TouchID {
|
||||||
|
return AppendJustPressedTouchIDs(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsTouchJustReleased returns a boolean value indicating
|
// IsTouchJustReleased returns a boolean value indicating
|
||||||
|
Loading…
Reference in New Issue
Block a user