-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.go
80 lines (65 loc) · 1.36 KB
/
assets.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
package main
import (
"embed"
"image"
_ "image/png"
"log"
"github.com/hajimehoshi/ebiten/v2"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
)
//go:embed assets/*
var assets embed.FS
var (
// main player sprite
playerIdle *ebiten.Image
playerWalk *ebiten.Image
uiFont font.Face
)
const (
playerWidth = 16
playerHeight = 64
playerOX = 0
playerOY = 0
playerFrameCount = 4
playerFrameAnimationSequence = 15
playerFrameWalkSpeed = 10
)
func requiredAssetImage(filepath string) *ebiten.Image {
f, err := assets.Open(filepath)
if err != nil {
log.Fatal(err)
panic(err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
log.Fatal(err)
panic(err)
}
return ebiten.NewImageFromImage(img)
}
func requiredAssetFont(filepath string) font.Face {
f, err := assets.ReadFile(filepath)
if err != nil {
panic(err)
}
tt, err := opentype.Parse(f)
if err != nil {
panic(err)
}
face, err := opentype.NewFace(tt, &opentype.FaceOptions{
Size: 32,
DPI: 72,
Hinting: font.HintingVertical,
})
if err != nil {
panic(err)
}
return face
}
func LoadGameAssets() {
uiFont = requiredAssetFont("assets/font.ttf")
playerIdle = requiredAssetImage("assets/playeridle.png")
playerWalk = requiredAssetImage("assets/playerwalk.png")
}