-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.go
93 lines (79 loc) · 1.85 KB
/
display.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
86
87
88
89
90
91
92
93
package astar
import (
"math/rand"
"time"
"github.com/hajimehoshi/ebiten/v2"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
const (
ScreenWidth = 1600
ScreenHeight = 1000
)
// Game represents a game state.
type Game struct {
board *Board
boardImage *ebiten.Image
counter int
input *Input
ticksPerInterval int
}
// NewGame generates a new Game object.
func NewGame(h, w, movesPerSecond int) (*Game, error) {
g := &Game{
counter: 0,
input: &Input{},
ticksPerInterval: 60 / movesPerSecond,
}
var err error
g.board, err = NewBoard(h, w)
if err != nil {
return nil, err
}
return g, nil
}
// Layout implements ebiten.Game's Layout.
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return ScreenWidth, ScreenHeight
}
// Update updates the current game state.
func (g *Game) Update() error {
g.input.Update()
if g.input.ResetPressed {
b, err := NewBoard(g.board.height, g.board.width)
if err != nil {
return err
}
g.board = b
g.input.CopyAndReset()
return nil
}
g.counter++
if g.counter%g.ticksPerInterval != 0 {
return nil
}
g.counter = 0
// Adjust coordinates to be relative to board.
bw, bh := g.board.Size()
g.input.MouseX = g.input.MouseX - (ScreenWidth-bw)/2
g.input.MouseY = g.input.MouseY - (ScreenHeight-bh)/2
g.board.Update(g.input.CopyAndReset())
return nil
}
// Draw draws the current game to the given screen.
func (g *Game) Draw(screen *ebiten.Image) {
if g.boardImage == nil {
w, h := g.board.Size()
g.boardImage = ebiten.NewImage(w, h)
}
screen.Fill(BackgroundColor)
g.board.Draw(g.boardImage)
op := &ebiten.DrawImageOptions{}
sw, sh := screen.Size()
bw, bh := g.boardImage.Size()
x := (sw - bw) / 2
y := (sh - bh) / 2
op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(g.boardImage, op)
}