/
spawn.go
77 lines (65 loc) · 1.75 KB
/
spawn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package system
import (
"math"
"github.com/elemir/herd"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/elemir/herd/examples/bunnymark/component"
"github.com/elemir/herd/examples/bunnymark/helper"
)
type Spawn struct {
Manager *herd.Manager
Settings *component.Settings
System *herd.SystemInfo
}
func NewSpawn(app *herd.App, settings *component.Settings) (Spawn, error) {
return Spawn{
Manager: app.Manager,
Settings: settings,
System: app.SystemInfo,
}, nil
}
func (s Spawn) Update() error {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
s.addBunnies()
}
if ids := ebiten.AppendTouchIDs(nil); len(ids) > 0 {
s.addBunnies()
}
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
}
type Bunnie struct {
Pos component.Position
Vel component.Velocity
Hue component.Hue
Gravity component.Gravity
Sprite component.Sprite
}
func (s Spawn) addBunnies() {
// 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++ {
s.Manager.Spawn(Bunnie{component.Position{
X: float64(s.System.Entities % 2), // Alternate screen edges
}, component.Velocity{
X: helper.RangeFloat(0, 0.005),
Y: helper.RangeFloat(0.0025, 0.005),
}, component.Hue{
Colorful: &s.Settings.Colorful,
Value: helper.RangeFloat(0, 2*math.Pi),
}, component.Gravity{
Value: 0.00095,
}, component.Sprite{
Image: s.Settings.Sprite,
}})
}
}