From 90ae4320d306c8e39821c6aa18d5d76418d31cb6 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Mon, 17 Jun 2024 19:45:19 +0300 Subject: [PATCH] FIX: Fixed game not working on Web --- src/game/capybara.go | 2 +- src/game/game.go | 6 +++++- src/game/mandarinRain.go | 30 +++++++++++++----------------- src/game/sprite.go | 16 ++++++---------- src/main.go | 2 +- 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/game/capybara.go b/src/game/capybara.go index 1b70397..6081014 100644 --- a/src/game/capybara.go +++ b/src/game/capybara.go @@ -65,7 +65,7 @@ func (c *Capybara) Draw(screen *ebiten.Image, level uint32) { capyWidth := float64(c.Sprite.RealBounds().Dx()) capyHeight := float64(c.Sprite.RealBounds().Dy()) - c.Sprite.MoveTo(float64(screen.Bounds().Dx()/2)-capyWidth/2, float64(screen.Bounds().Dy()/2)-capyHeight/2) + c.Sprite.MoveTo(float64(screen.Bounds().Dx()/2)-capyWidth/2, float64(screen.Bounds().Dy()/2)-capyHeight/2, screen) op.GeoM.Translate(c.Sprite.X, c.Sprite.Y) diff --git a/src/game/game.go b/src/game/game.go index 95e840a..f07bacb 100644 --- a/src/game/game.go +++ b/src/game/game.go @@ -25,6 +25,7 @@ type Game struct { AudioPlayers map[string]*audio.Player FontFace font.Face PassiveIncomeTicker int + Screen *ebiten.Image Capybara *Capybara Background *Sprite MandarinRain *MandarinRain @@ -47,6 +48,7 @@ func NewGame() Game { "orange_put": resources.GetAudioPlayer(audioCtx, "orange_put.wav"), "mandarin_rain_completed": resources.GetAudioPlayer(audioCtx, "mandarin_rain_completed.wav"), }, + Screen: nil, Capybara: NewCapybara(NewSpriteFromFile("capybara_1.png")), Background: NewSpriteFromFile("background_1.png"), FontFace: util.NewFace(fnt, &opentype.FaceOptions{ @@ -131,7 +133,7 @@ func (g *Game) Update() error { if !g.MandarinRain.InProgress && g.Save.TimesClicked > 0 && g.Save.TimesClicked%100 == 0 { // Have some oranges! - g.MandarinRain.Run() + g.MandarinRain.Run(g) logger.Info("Started mandarin rain at %d points!", g.Save.Points) } @@ -149,6 +151,8 @@ func (g *Game) Update() error { } func (g *Game) Draw(screen *ebiten.Image) { + g.Screen = screen + // Background screen.Fill(color.Black) diff --git a/src/game/mandarinRain.go b/src/game/mandarinRain.go index edaa04f..9b78da3 100644 --- a/src/game/mandarinRain.go +++ b/src/game/mandarinRain.go @@ -1,7 +1,6 @@ package game import ( - "image" "math/rand" "github.com/hajimehoshi/ebiten/v2" @@ -18,12 +17,12 @@ type MandarinRain struct { mandarinsInBox uint16 boxFull bool mandarinCountRange [2]uint16 - screenBounds image.Rectangle + // screenBounds image.Rectangle } func NewMandarinRain(from uint16, to uint16) *MandarinRain { rain := MandarinRain{} - rain.screenBounds = WindowBounds() + // rain.screenBounds = WindowBounds() rain.InProgress = false rain.mandarinInitialCount = uint16(rand.Int31n(int32(to-from)) + int32(from)) rain.mandarinCountRange = [2]uint16{from, to} @@ -42,29 +41,26 @@ func NewMandarinRain(from uint16, to uint16) *MandarinRain { return &rain } -func (mr *MandarinRain) Run() { +func (mr *MandarinRain) Run(game *Game) { if mr.InProgress { return } - mr.screenBounds = WindowBounds() mr.InProgress = true // Move oranges to random positions on the top of the screen for _, orange := range mr.Mandarins { - orange.Sprite.MoveTo(float64(rand.Int31n(int32(mr.screenBounds.Bounds().Dx()-orange.Sprite.RealBounds().Dx()))), 10.0) + orange.Sprite.MoveTo(float64(rand.Int31n(int32(game.Screen.Bounds().Dx()-orange.Sprite.Img.Bounds().Dx()))), 10.0, game.Screen) } // Create mandarin box mr.MandarinBox.Sprite.MoveTo( - float64(rand.Int31n(int32(mr.screenBounds.Bounds().Dx()-mr.MandarinBox.Sprite.RealBounds().Dx()))), - 10.0, + float64(rand.Int31n(int32(game.Screen.Bounds().Dx()-mr.MandarinBox.Sprite.Img.Bounds().Dx()))), + 10.0, game.Screen, ) } func (mr *MandarinRain) Update(game *Game) { - mr.screenBounds = WindowBounds() - cPosX, cPosY := ebiten.CursorPosition() var tPosX int = 0 var tPosY int = 0 @@ -109,7 +105,7 @@ func (mr *MandarinRain) Update(game *Game) { // Constraints // Right - if oX+float64(oBounds.Dx()) >= float64(mr.screenBounds.Dx()) { + if oX+float64(oBounds.Dx()) >= float64(game.Screen.Bounds().Dx()) { orange.Velocity.Vx = -orange.Velocity.Vx * 0.4 } @@ -124,7 +120,7 @@ func (mr *MandarinRain) Update(game *Game) { } // Bottom - if oY+float64(oBounds.Dy()) >= float64(mr.screenBounds.Dy()) { + if oY+float64(oBounds.Dy()) >= float64(game.Screen.Bounds().Dy()) { orange.Velocity.Vx = orange.Velocity.Vx * 0.4 // friction on the floor orange.Velocity.Vy = -orange.Velocity.Vy * 0.4 } @@ -133,7 +129,7 @@ func (mr *MandarinRain) Update(game *Game) { orange.Sprite.Y += orange.Velocity.Vy // Move the orange - orange.Sprite.MoveTo(orange.Sprite.X, orange.Sprite.Y) + orange.Sprite.MoveTo(orange.Sprite.X, orange.Sprite.Y, game.Screen) // Check whether it touches mandarin box if orange.InVicinity(mr.MandarinBox.Sprite.X, mr.MandarinBox.Sprite.Y, float64(mr.MandarinBox.Sprite.RealBounds().Dx())) { @@ -182,7 +178,7 @@ func (mr *MandarinRain) Update(game *Game) { mY := mr.MandarinBox.Sprite.Y // Right - if mX+float64(mBounds.Dx()) >= float64(mr.screenBounds.Dx()) { + if mX+float64(mBounds.Dx()) >= float64(game.Screen.Bounds().Dx()) { mr.MandarinBox.Velocity.Vx = -mr.MandarinBox.Velocity.Vx * 0.3 } @@ -197,7 +193,7 @@ func (mr *MandarinRain) Update(game *Game) { } // Bottom - if mY+float64(mBounds.Dy()) >= float64(mr.screenBounds.Dy()) { + if mY+float64(mBounds.Dy()) >= float64(game.Screen.Bounds().Dy()) { mr.MandarinBox.Velocity.Vx = mr.MandarinBox.Velocity.Vx * 0.3 // friction on the floor mr.MandarinBox.Velocity.Vy = -mr.MandarinBox.Velocity.Vy * 0.3 } @@ -206,7 +202,7 @@ func (mr *MandarinRain) Update(game *Game) { mr.MandarinBox.Sprite.Y += mr.MandarinBox.Velocity.Vy // Move box - mr.MandarinBox.Sprite.MoveTo(mr.MandarinBox.Sprite.X, mr.MandarinBox.Sprite.Y) + mr.MandarinBox.Sprite.MoveTo(mr.MandarinBox.Sprite.X, mr.MandarinBox.Sprite.Y, game.Screen) if mr.mandarinsInBox == mr.mandarinInitialCount && !mr.boxFull { // All oranges are in a box! @@ -218,7 +214,7 @@ func (mr *MandarinRain) Update(game *Game) { if mr.boxFull && mr.MandarinBox.InVicinity( game.Capybara.Sprite.X+float64(game.Capybara.Sprite.RealBounds().Dx()/2), game.Capybara.Sprite.Y+float64(game.Capybara.Sprite.RealBounds().Dy()/2), - float64(mr.screenBounds.Dx())/7) { + float64(game.Screen.Bounds().Dx())/7) { // Give a reward and finish this mandarin rain! game.Save.Points += pointsForLevel(game.Save.Level+1) / 5 game.PlaySound("mandarin_rain_completed") diff --git a/src/game/sprite.go b/src/game/sprite.go index ffff24d..3e751f9 100644 --- a/src/game/sprite.go +++ b/src/game/sprite.go @@ -20,7 +20,6 @@ type Sprite struct { Y float64 Animation AnimationData Scale float64 - Dragged bool } func NewSprite(img image.Image) *Sprite { @@ -33,8 +32,7 @@ func NewSprite(img image.Image) *Sprite { Theta: 0.0, BounceDirectionFlag: false, }, - Scale: 1.0, - Dragged: false, + Scale: 1.0, } } @@ -63,15 +61,13 @@ func (s *Sprite) IsIn(x int, y int) bool { } // Moves sprite to given positions. Respects window constraints -func (s *Sprite) MoveTo(x float64, y float64) { +func (s *Sprite) MoveTo(x float64, y float64, screenBounds *ebiten.Image) { s.X = x s.Y = y - screenBounds := WindowBounds() - // Constraints // Right - if s.X+float64(s.RealBounds().Dx()) >= float64(screenBounds.Dx()) { - s.X = float64(screenBounds.Dx()) - float64(s.RealBounds().Dx()) + if s.X+float64(s.RealBounds().Dx()) >= float64(screenBounds.Bounds().Dx()) { + s.X = float64(screenBounds.Bounds().Dx()) - float64(s.RealBounds().Dx()) } // Left @@ -85,8 +81,8 @@ func (s *Sprite) MoveTo(x float64, y float64) { } // Bottom - if s.Y+float64(s.RealBounds().Dy()) >= float64(screenBounds.Dy()) { - s.Y = float64(screenBounds.Dy()) - float64(s.RealBounds().Dy()) + if s.Y+float64(s.RealBounds().Dy()) >= float64(screenBounds.Bounds().Dy()) { + s.Y = float64(screenBounds.Bounds().Dy()) - float64(s.RealBounds().Dy()) } } diff --git a/src/main.go b/src/main.go index c4dfc50..d6ca95d 100644 --- a/src/main.go +++ b/src/main.go @@ -36,7 +36,7 @@ import ( "github.com/hajimehoshi/ebiten/v2" ) -const Version string = "v0.1-release" +const Version string = "v0.1.1-release" var ( silent *bool = flag.Bool("silent", false, "Set to true in order to discard all logging")