From b260779bc0b12ab58982b740b8f15df731c8199e Mon Sep 17 00:00:00 2001 From: two-six Date: Thu, 12 Oct 2023 18:07:32 +0200 Subject: [PATCH 1/4] Fix multiple monitor issues --- main.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 61be196..f2c405e 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,8 @@ var ( width = 32 height = 32 + monitorWidth, monitorHeight = ebiten.ScreenSizeInFullscreen() + cfg = &Config{} currentplayer *audio.Player = nil @@ -60,7 +62,7 @@ func (m *neko) Layout(outsideWidth, outsideHeight int) (int, int) { return width, height } -func playsound(sound []byte) { +func playSound(sound []byte) { if currentplayer != nil && currentplayer.IsPlaying() { currentplayer.Close() } @@ -72,7 +74,19 @@ func playsound(sound []byte) { func (m *neko) Update() error { m.count++ if m.state == 10 && m.count == m.min { - playsound(mSound["idle3"]) + playSound(mSound["idle3"]) + } + // Prevents neko from being stuck on the side of the screen + // or randomly travelling to another monitor + if m.x < 0 { + m.x = 0 + } else if m.x >= monitorWidth { + m.x = monitorWidth + } + if m.y < 0 { + m.y = 0 + } else if m.y >= monitorHeight { + m.y = monitorHeight } ebiten.SetWindowPosition(m.x, m.y) @@ -98,11 +112,8 @@ func (m *neko) Update() error { } if m.state >= 13 { - playsound(mSound["awake"]) + playSound(mSound["awake"]) } - m.state = 0 - m.min = 8 - m.max = 16 m.catchCursor(x, y) return nil } @@ -134,6 +145,9 @@ func (m *neko) stayIdle() { } func (m *neko) catchCursor(x, y int) { + m.state = 0 + m.min = 8 + m.max = 16 tr := 0.0 // get mouse direction r := math.Atan2(float64(y), float64(x)) @@ -206,7 +220,7 @@ func (m *neko) Draw(screen *ebiten.Image) { m.state++ switch m.state { case 13: - playsound(mSound["sleep"]) + playSound(mSound["sleep"]) } } } @@ -270,10 +284,9 @@ func main() { // So let's do this at the start. audio.CurrentContext().NewPlayerFromBytes([]byte{}).Play() - sw, sh := ebiten.ScreenSizeInFullscreen() n := &neko{ - x: sw / 2, - y: sh / 2, + x: monitorWidth / 2, + y: monitorHeight / 2, min: 8, max: 16, } From 47b93e8b0342fa628b8eb75e455e0cb9bc04d394 Mon Sep 17 00:00:00 2001 From: two-six Date: Thu, 12 Oct 2023 20:33:14 +0200 Subject: [PATCH 2/4] min max functions --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index f2c405e..666a0ed 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,8 @@ func (m *neko) Update() error { } // Prevents neko from being stuck on the side of the screen // or randomly travelling to another monitor + m.x = max(0, min(m.x, monitorWidth)) + m.y = max(0, min(m.y, monitorWidth)) if m.x < 0 { m.x = 0 } else if m.x >= monitorWidth { From 0f66cfbb9c3d7db8f854ca06b7c1d61bdd825286 Mon Sep 17 00:00:00 2001 From: two-six Date: Thu, 12 Oct 2023 20:36:42 +0200 Subject: [PATCH 3/4] Fix --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 666a0ed..2569be0 100644 --- a/main.go +++ b/main.go @@ -79,7 +79,7 @@ func (m *neko) Update() error { // Prevents neko from being stuck on the side of the screen // or randomly travelling to another monitor m.x = max(0, min(m.x, monitorWidth)) - m.y = max(0, min(m.y, monitorWidth)) + m.y = max(0, min(m.y, monitorHeight)) if m.x < 0 { m.x = 0 } else if m.x >= monitorWidth { From 1285cdb05953fe80ae67c53a8ab8a920a86661c8 Mon Sep 17 00:00:00 2001 From: two-six Date: Thu, 12 Oct 2023 20:40:02 +0200 Subject: [PATCH 4/4] Fixed and tested --- main.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/main.go b/main.go index 2569be0..65c7c2f 100644 --- a/main.go +++ b/main.go @@ -80,16 +80,6 @@ func (m *neko) Update() error { // or randomly travelling to another monitor m.x = max(0, min(m.x, monitorWidth)) m.y = max(0, min(m.y, monitorHeight)) - if m.x < 0 { - m.x = 0 - } else if m.x >= monitorWidth { - m.x = monitorWidth - } - if m.y < 0 { - m.y = 0 - } else if m.y >= monitorHeight { - m.y = monitorHeight - } ebiten.SetWindowPosition(m.x, m.y) mx, my := ebiten.CursorPosition()