-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (85 loc) · 1.9 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"encoding/csv"
"image"
_ "image/png"
"io"
"math"
"math/rand"
"os"
"path/filepath"
"strconv"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
func loadAnimationSheet(name string, frameWidth float64, sheetBasePath string) (sheet pixel.Picture, anims map[string][]pixel.Rect, err error) {
// open and load the spritesheet
sheetFile, err := os.Open(filepath.Join(sheetBasePath, name+".png"))
if err != nil {
return nil, nil, err
}
defer sheetFile.Close()
sheetImg, _, err := image.Decode(sheetFile)
if err != nil {
return nil, nil, err
}
sheet = pixel.PictureDataFromImage(sheetImg)
// create a slice of frames inside the spritesheet
var frames []pixel.Rect
for x := 0.0; x+frameWidth <= sheet.Bounds().Max.X; x += frameWidth {
frames = append(frames, pixel.R(
x,
0,
x+frameWidth,
sheet.Bounds().H(),
))
}
descFile, err := os.Open(filepath.Join(sheetBasePath, name+".csv"))
if err != nil {
return nil, nil, err
}
defer descFile.Close()
anims = make(map[string][]pixel.Rect)
// load the animation information, name and interval inside the spritesheet
desc := csv.NewReader(descFile)
for {
anim, err := desc.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, nil, err
}
name := anim[0]
start, _ := strconv.Atoi(anim[1])
end, _ := strconv.Atoi(anim[2])
anims[name] = frames[start : end+1]
}
return sheet, anims, nil
}
func randomNiceColor() pixel.RGBA {
r := rand.Float64()
g := rand.Float64()
b := rand.Float64()
l := math.Sqrt(r*r + g*g + b*b)
if l == 0 {
return randomNiceColor()
}
return pixel.RGB(r/l, g/l, b/l)
}
func main() {
pixelgl.Run(run)
}
func run() {
var err error
win, err = pixelgl.NewWindow(pixelgl.WindowConfig{
Title: "Beat Phaser",
Bounds: pixel.R(0, 0, 1920, 1080),
Resizable: true,
})
if err != nil {
panic(err)
}
game := &game{}
game.run()
}