Browse Source

FIX: Fixed game not working on Web

master
parent
commit
90ae4320d3
  1. 2
      src/game/capybara.go
  2. 6
      src/game/game.go
  3. 30
      src/game/mandarinRain.go
  4. 14
      src/game/sprite.go
  5. 2
      src/main.go

2
src/game/capybara.go

@ -65,7 +65,7 @@ func (c *Capybara) Draw(screen *ebiten.Image, level uint32) {
capyWidth := float64(c.Sprite.RealBounds().Dx()) capyWidth := float64(c.Sprite.RealBounds().Dx())
capyHeight := float64(c.Sprite.RealBounds().Dy()) 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) op.GeoM.Translate(c.Sprite.X, c.Sprite.Y)

6
src/game/game.go

@ -25,6 +25,7 @@ type Game struct {
AudioPlayers map[string]*audio.Player AudioPlayers map[string]*audio.Player
FontFace font.Face FontFace font.Face
PassiveIncomeTicker int PassiveIncomeTicker int
Screen *ebiten.Image
Capybara *Capybara Capybara *Capybara
Background *Sprite Background *Sprite
MandarinRain *MandarinRain MandarinRain *MandarinRain
@ -47,6 +48,7 @@ func NewGame() Game {
"orange_put": resources.GetAudioPlayer(audioCtx, "orange_put.wav"), "orange_put": resources.GetAudioPlayer(audioCtx, "orange_put.wav"),
"mandarin_rain_completed": resources.GetAudioPlayer(audioCtx, "mandarin_rain_completed.wav"), "mandarin_rain_completed": resources.GetAudioPlayer(audioCtx, "mandarin_rain_completed.wav"),
}, },
Screen: nil,
Capybara: NewCapybara(NewSpriteFromFile("capybara_1.png")), Capybara: NewCapybara(NewSpriteFromFile("capybara_1.png")),
Background: NewSpriteFromFile("background_1.png"), Background: NewSpriteFromFile("background_1.png"),
FontFace: util.NewFace(fnt, &opentype.FaceOptions{ 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 { if !g.MandarinRain.InProgress && g.Save.TimesClicked > 0 && g.Save.TimesClicked%100 == 0 {
// Have some oranges! // Have some oranges!
g.MandarinRain.Run() g.MandarinRain.Run(g)
logger.Info("Started mandarin rain at %d points!", g.Save.Points) 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) { func (g *Game) Draw(screen *ebiten.Image) {
g.Screen = screen
// Background // Background
screen.Fill(color.Black) screen.Fill(color.Black)

30
src/game/mandarinRain.go

@ -1,7 +1,6 @@
package game package game
import ( import (
"image"
"math/rand" "math/rand"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
@ -18,12 +17,12 @@ type MandarinRain struct {
mandarinsInBox uint16 mandarinsInBox uint16
boxFull bool boxFull bool
mandarinCountRange [2]uint16 mandarinCountRange [2]uint16
screenBounds image.Rectangle // screenBounds image.Rectangle
} }
func NewMandarinRain(from uint16, to uint16) *MandarinRain { func NewMandarinRain(from uint16, to uint16) *MandarinRain {
rain := MandarinRain{} rain := MandarinRain{}
rain.screenBounds = WindowBounds() // rain.screenBounds = WindowBounds()
rain.InProgress = false rain.InProgress = false
rain.mandarinInitialCount = uint16(rand.Int31n(int32(to-from)) + int32(from)) rain.mandarinInitialCount = uint16(rand.Int31n(int32(to-from)) + int32(from))
rain.mandarinCountRange = [2]uint16{from, to} rain.mandarinCountRange = [2]uint16{from, to}
@ -42,29 +41,26 @@ func NewMandarinRain(from uint16, to uint16) *MandarinRain {
return &rain return &rain
} }
func (mr *MandarinRain) Run() { func (mr *MandarinRain) Run(game *Game) {
if mr.InProgress { if mr.InProgress {
return return
} }
mr.screenBounds = WindowBounds()
mr.InProgress = true mr.InProgress = true
// Move oranges to random positions on the top of the screen // Move oranges to random positions on the top of the screen
for _, orange := range mr.Mandarins { 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 // Create mandarin box
mr.MandarinBox.Sprite.MoveTo( mr.MandarinBox.Sprite.MoveTo(
float64(rand.Int31n(int32(mr.screenBounds.Bounds().Dx()-mr.MandarinBox.Sprite.RealBounds().Dx()))), float64(rand.Int31n(int32(game.Screen.Bounds().Dx()-mr.MandarinBox.Sprite.Img.Bounds().Dx()))),
10.0, 10.0, game.Screen,
) )
} }
func (mr *MandarinRain) Update(game *Game) { func (mr *MandarinRain) Update(game *Game) {
mr.screenBounds = WindowBounds()
cPosX, cPosY := ebiten.CursorPosition() cPosX, cPosY := ebiten.CursorPosition()
var tPosX int = 0 var tPosX int = 0
var tPosY int = 0 var tPosY int = 0
@ -109,7 +105,7 @@ func (mr *MandarinRain) Update(game *Game) {
// Constraints // Constraints
// Right // 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 orange.Velocity.Vx = -orange.Velocity.Vx * 0.4
} }
@ -124,7 +120,7 @@ func (mr *MandarinRain) Update(game *Game) {
} }
// Bottom // 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.Vx = orange.Velocity.Vx * 0.4 // friction on the floor
orange.Velocity.Vy = -orange.Velocity.Vy * 0.4 orange.Velocity.Vy = -orange.Velocity.Vy * 0.4
} }
@ -133,7 +129,7 @@ func (mr *MandarinRain) Update(game *Game) {
orange.Sprite.Y += orange.Velocity.Vy orange.Sprite.Y += orange.Velocity.Vy
// Move the orange // 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 // Check whether it touches mandarin box
if orange.InVicinity(mr.MandarinBox.Sprite.X, mr.MandarinBox.Sprite.Y, float64(mr.MandarinBox.Sprite.RealBounds().Dx())) { 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 mY := mr.MandarinBox.Sprite.Y
// Right // 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 mr.MandarinBox.Velocity.Vx = -mr.MandarinBox.Velocity.Vx * 0.3
} }
@ -197,7 +193,7 @@ func (mr *MandarinRain) Update(game *Game) {
} }
// Bottom // 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.Vx = mr.MandarinBox.Velocity.Vx * 0.3 // friction on the floor
mr.MandarinBox.Velocity.Vy = -mr.MandarinBox.Velocity.Vy * 0.3 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 mr.MandarinBox.Sprite.Y += mr.MandarinBox.Velocity.Vy
// Move box // 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 { if mr.mandarinsInBox == mr.mandarinInitialCount && !mr.boxFull {
// All oranges are in a box! // All oranges are in a box!
@ -218,7 +214,7 @@ func (mr *MandarinRain) Update(game *Game) {
if mr.boxFull && mr.MandarinBox.InVicinity( if mr.boxFull && mr.MandarinBox.InVicinity(
game.Capybara.Sprite.X+float64(game.Capybara.Sprite.RealBounds().Dx()/2), game.Capybara.Sprite.X+float64(game.Capybara.Sprite.RealBounds().Dx()/2),
game.Capybara.Sprite.Y+float64(game.Capybara.Sprite.RealBounds().Dy()/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! // Give a reward and finish this mandarin rain!
game.Save.Points += pointsForLevel(game.Save.Level+1) / 5 game.Save.Points += pointsForLevel(game.Save.Level+1) / 5
game.PlaySound("mandarin_rain_completed") game.PlaySound("mandarin_rain_completed")

14
src/game/sprite.go

@ -20,7 +20,6 @@ type Sprite struct {
Y float64 Y float64
Animation AnimationData Animation AnimationData
Scale float64 Scale float64
Dragged bool
} }
func NewSprite(img image.Image) *Sprite { func NewSprite(img image.Image) *Sprite {
@ -34,7 +33,6 @@ func NewSprite(img image.Image) *Sprite {
BounceDirectionFlag: false, BounceDirectionFlag: false,
}, },
Scale: 1.0, Scale: 1.0,
Dragged: false,
} }
} }
@ -63,15 +61,13 @@ func (s *Sprite) IsIn(x int, y int) bool {
} }
// Moves sprite to given positions. Respects window constraints // 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.X = x
s.Y = y s.Y = y
screenBounds := WindowBounds()
// Constraints // Constraints
// Right // Right
if s.X+float64(s.RealBounds().Dx()) >= float64(screenBounds.Dx()) { if s.X+float64(s.RealBounds().Dx()) >= float64(screenBounds.Bounds().Dx()) {
s.X = float64(screenBounds.Dx()) - float64(s.RealBounds().Dx()) s.X = float64(screenBounds.Bounds().Dx()) - float64(s.RealBounds().Dx())
} }
// Left // Left
@ -85,8 +81,8 @@ func (s *Sprite) MoveTo(x float64, y float64) {
} }
// Bottom // Bottom
if s.Y+float64(s.RealBounds().Dy()) >= float64(screenBounds.Dy()) { if s.Y+float64(s.RealBounds().Dy()) >= float64(screenBounds.Bounds().Dy()) {
s.Y = float64(screenBounds.Dy()) - float64(s.RealBounds().Dy()) s.Y = float64(screenBounds.Bounds().Dy()) - float64(s.RealBounds().Dy())
} }
} }

2
src/main.go

@ -36,7 +36,7 @@ import (
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
const Version string = "v0.1-release" const Version string = "v0.1.1-release"
var ( var (
silent *bool = flag.Bool("silent", false, "Set to true in order to discard all logging") silent *bool = flag.Bool("silent", false, "Set to true in order to discard all logging")

Loading…
Cancel
Save