gohan-bunnymark/system/bounce.go

46 lines
1.1 KiB
Go

package system
import (
"code.rocketnine.space/tslocum/gohan"
"code.rocketnine.space/tslocum/gohan-bunnymark/component"
"code.rocketnine.space/tslocum/gohan-bunnymark/helper"
"code.rocketnine.space/tslocum/gohan-bunnymark/world"
"github.com/hajimehoshi/ebiten/v2"
)
type Bounce struct {
Position *component.Position
Velocity *component.Velocity
Sprite *component.Sprite
}
func (b *Bounce) Update(e gohan.Entity) error {
sw, sh := float64(world.ScreenWidth), float64(world.ScreenHeight)
iw, ih := float64(b.Sprite.Image.Bounds().Dx()), float64(b.Sprite.Image.Bounds().Dy())
relW, relH := iw/sw, ih/sh
if b.Position.X+relW > 1 {
b.Velocity.X *= -1
b.Position.X = 1 - relW
}
if b.Position.X < 0 {
b.Velocity.X *= -1
b.Position.X = 0
}
if b.Position.Y+relH > 1 {
b.Velocity.Y *= -0.85
b.Position.Y = 1 - relH
if helper.Chance(0.5) {
b.Velocity.Y -= helper.RangeFloat(0, 0.009)
}
}
if b.Position.Y < 0 {
b.Velocity.Y = 0
b.Position.Y = 0
}
return nil
}
func (g *Bounce) Draw(_ gohan.Entity, _ *ebiten.Image) error {
return gohan.ErrUnregister
}