-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
85 lines (69 loc) · 1.67 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/ungerik/go3d/float64/vec2"
"image"
"image/color"
"time"
)
type GameEntities interface {
Draw(*ebiten.Image)
Update(*ebiten.Image, int64)
}
type Game struct {
ShowFPS bool
lastUpdate time.Time
bgColor color.Color
gameSize image.Point
entities []GameEntities
lastDraw time.Time
}
func newGame(bg color.Color, windowSize image.Point) *Game {
game := &Game{
bgColor: bg,
gameSize: windowSize,
lastUpdate: time.Now(),
}
playerPosition := vec2.T{
(windowWidth - (playerSize * j0hnScale)) / 2,
(windowHeight - (playerSize * j0hnScale)) - 77,
}
player := NewJ0hn().SetPosition(playerPosition)
starfield := NewBackgroundSystem(player)
planets := NewPlanetSpawner(player)
powerups := NewPowerupSpawner(player)
ui := NewUi(player)
platform := NewPlatform(player)
platform.SetPosition(&vec2.T{(windowWidth - (platformSize * j0hnScale)) / 2, windowHeight - platformSize*3})
game.entities = append(game.entities,
starfield,
planets,
powerups,
platform,
player,
ui,
)
return game
}
func (g *Game) Update(screen *ebiten.Image) error {
d := time.Since(g.lastUpdate).Milliseconds()
for _, e := range g.entities {
e.Update(screen, d)
}
g.lastUpdate = time.Now()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
_ = screen.Fill(g.bgColor)
for _, e := range g.entities {
e.Draw(screen)
}
if g.ShowFPS {
_ = ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
}
}
func (g *Game) Layout(int, int) (int, int) {
return windowWidth, windowHeight
}