|
|
@ -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") |
|
|
|