/
game.go
61 lines (50 loc) · 1.05 KB
/
game.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
package boids
import (
"fmt"
"image/color"
"math/rand"
"github.com/bioerrorlog/boids-ebitengine/vector"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
const (
ScreenWidth = 1920
ScreenHeight = 1080
boidCount = 100
)
type Game struct {
boids []*Boid
}
func NewGame() (*Game, error) {
g := &Game{
boids: make([]*Boid, boidCount),
}
for i := range g.boids {
g.boids[i] = NewBoid(
rand.Float64()*ScreenWidth,
rand.Float64()*ScreenHeight,
vector.Vec2{X: ScreenWidth / 2, Y: ScreenHeight / 2},
)
}
return g, nil
}
func (g *Game) Update() error {
for _, b := range g.boids {
b.Update(g.boids)
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
// Backgroud
screen.Fill(color.RGBA{255, 245, 228, 0xff})
// Boids
for _, b := range g.boids {
b.Draw(screen)
}
// Debug
fps := fmt.Sprintf("FPS: %0.2f", ebiten.ActualFPS())
ebitenutil.DebugPrint(screen, fps)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return ScreenWidth, ScreenHeight
}