Fix multiple monitor issues

This commit is contained in:
two-six
2023-10-12 18:07:32 +02:00
parent 2ef5e04fd3
commit b260779bc0

33
main.go
View File

@@ -51,6 +51,8 @@ var (
width = 32 width = 32
height = 32 height = 32
monitorWidth, monitorHeight = ebiten.ScreenSizeInFullscreen()
cfg = &Config{} cfg = &Config{}
currentplayer *audio.Player = nil currentplayer *audio.Player = nil
@@ -60,7 +62,7 @@ func (m *neko) Layout(outsideWidth, outsideHeight int) (int, int) {
return width, height return width, height
} }
func playsound(sound []byte) { func playSound(sound []byte) {
if currentplayer != nil && currentplayer.IsPlaying() { if currentplayer != nil && currentplayer.IsPlaying() {
currentplayer.Close() currentplayer.Close()
} }
@@ -72,7 +74,19 @@ func playsound(sound []byte) {
func (m *neko) Update() error { func (m *neko) Update() error {
m.count++ m.count++
if m.state == 10 && m.count == m.min { 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) ebiten.SetWindowPosition(m.x, m.y)
@@ -98,11 +112,8 @@ func (m *neko) Update() error {
} }
if m.state >= 13 { if m.state >= 13 {
playsound(mSound["awake"]) playSound(mSound["awake"])
} }
m.state = 0
m.min = 8
m.max = 16
m.catchCursor(x, y) m.catchCursor(x, y)
return nil return nil
} }
@@ -134,6 +145,9 @@ func (m *neko) stayIdle() {
} }
func (m *neko) catchCursor(x, y int) { func (m *neko) catchCursor(x, y int) {
m.state = 0
m.min = 8
m.max = 16
tr := 0.0 tr := 0.0
// get mouse direction // get mouse direction
r := math.Atan2(float64(y), float64(x)) r := math.Atan2(float64(y), float64(x))
@@ -206,7 +220,7 @@ func (m *neko) Draw(screen *ebiten.Image) {
m.state++ m.state++
switch m.state { switch m.state {
case 13: case 13:
playsound(mSound["sleep"]) playSound(mSound["sleep"])
} }
} }
} }
@@ -270,10 +284,9 @@ func main() {
// So let's do this at the start. // So let's do this at the start.
audio.CurrentContext().NewPlayerFromBytes([]byte{}).Play() audio.CurrentContext().NewPlayerFromBytes([]byte{}).Play()
sw, sh := ebiten.ScreenSizeInFullscreen()
n := &neko{ n := &neko{
x: sw / 2, x: monitorWidth / 2,
y: sh / 2, y: monitorHeight / 2,
min: 8, min: 8,
max: 16, max: 16,
} }