generated from sionleroux/ebitengine-game-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cursor.go
59 lines (52 loc) · 1.27 KB
/
cursor.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
// Use of this source code is subject to an MIT-style
// licence which can be found in the LICENSE file.
package main
import "github.com/hajimehoshi/ebiten/v2"
// Cursor represents the mouse cursor on the screen
type Cursor struct {
Hit bool // whether the shot hit or not
state int
// tick int
images []*ebiten.Image
position Coord
}
const (
cursorNormal int = iota
cursorMiss
cursorHit
)
func NewCursor() *Cursor {
return &Cursor{
images: []*ebiten.Image{
loadImage("assets/sprites/Cursor_1.png"),
loadImage("assets/sprites/Cursor_2.png"),
loadImage("assets/sprites/Cursor_3.png"),
},
}
}
func (c *Cursor) Update(g *GameScreen) {
cx, cy := ebiten.CursorPosition()
c.position.X, c.position.Y = float64(cx), float64(cy)
switch g.Player.State {
case playerDryFire:
c.state = cursorMiss
case playerShooting:
if c.Hit {
c.state = cursorHit
} else {
c.state = cursorMiss
}
default:
c.state = cursorNormal
}
return
}
func (c *Cursor) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(c.position.X, c.position.Y)
op.GeoM.Translate( // position image centre around coords
float64(-c.images[c.state].Bounds().Dx())/2,
float64(-c.images[c.state].Bounds().Dy())/2,
)
screen.DrawImage(c.images[c.state], op)
}