gohan-bunnymark/system/spawn.go

67 lines
1.7 KiB
Go

package system
import (
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"code.rocketnine.space/tslocum/gohan"
"code.rocketnine.space/tslocum/gohan-bunnymark/component"
"code.rocketnine.space/tslocum/gohan-bunnymark/helper"
)
type Spawn struct {
Settings *component.Settings
}
func (s *Spawn) Update(e gohan.Entity) error {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
s.addBunnies()
}
if ids := ebiten.AppendTouchIDs(nil); len(ids) > 0 {
s.addBunnies() // not accurate, cause no input manager for this
}
if _, offset := ebiten.Wheel(); offset != 0 {
s.Settings.Amount += int(offset * 10)
if s.Settings.Amount < 0 {
s.Settings.Amount = 0
}
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.Settings.Colorful = !s.Settings.Colorful
}
return nil
}
func (s *Spawn) addBunnies() {
numEntities := len(gohan.AllEntities())
// Spawns specific amount of bunnies at the edges of the screen
// It will alternately add bunnies to the left and right corners of the screen
for i := 0; i < s.Settings.Amount; i++ {
e := gohan.NewEntity()
e.AddComponent(&component.Position{
X: float64(numEntities % 2), // Alternate screen edges
})
e.AddComponent(&component.Velocity{
X: helper.RangeFloat(0, 0.005),
Y: helper.RangeFloat(0.0025, 0.005)})
e.AddComponent(&component.Hue{
Colorful: &s.Settings.Colorful,
Value: helper.RangeFloat(0, 2*math.Pi),
})
e.AddComponent(&component.Gravity{Value: 0.00095})
e.AddComponent(&component.Sprite{Image: s.Settings.Sprite})
numEntities++
}
}
func (s *Spawn) Draw(_ gohan.Entity, _ *ebiten.Image) error {
return gohan.ErrUnregister
}