forked from yohamta/ganim8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (102 loc) · 3.17 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"bytes"
"embed"
"image"
_ "image/png"
"log"
"math"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/yohamta/ganim8/v2"
)
const (
screenWidth = 800
screenHeight = 600
)
type Game struct {
spinning []*ganim8.Animation
plane *ganim8.Animation
seaplane *ganim8.Animation
seaplaneAngle float64
submarine *ganim8.Animation
}
func NewGame() *Game {
g := &Game{}
g.setupAnimations()
return g
}
func (g *Game) Update() error {
for _, a := range g.spinning {
a.Update()
}
g.plane.Update()
g.seaplane.Update()
g.submarine.Update()
g.seaplaneAngle += 10 * math.Pi / 180
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.Clear()
for i, a := range g.spinning {
a.Draw(screen, ganim8.DrawOpts(float64(i)*75, float64(i)*50))
// Alternative way to draw an animation:
// ganim8.DrawAnime(screen, a, float64(i)*75, float64(i)*50, 0, 1, 1, .5, .5)
}
g.plane.Draw(screen, ganim8.DrawOpts(100, 400))
g.seaplane.Draw(screen, ganim8.DrawOpts(250, 432, g.seaplaneAngle, 1, 1, 0.5, 0.5))
g.submarine.Draw(screen, ganim8.DrawOpts(600, 100))
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func (g *Game) setupAnimations() {
img := ebiten.NewImageFromImage(readImage("assets/1945.png"))
// frame(w,h), image(w,h), offsets, border
g32 := ganim8.NewGrid(32, 32, 1024, 768, 3, 3, 1)
g.spinning = []*ganim8.Animation{
ganim8.New(img, g32.Frames("1-8", 1), time.Millisecond*100),
ganim8.New(img, g32.Frames(18, "8-11", 18, "10-7"), time.Millisecond*200),
ganim8.New(img, g32.Frames("1-8", 2), time.Millisecond*300),
ganim8.New(img, g32.Frames(19, "8-11", 19, "10-7"), time.Millisecond*400),
ganim8.New(img, g32.Frames("1-8", 3), time.Millisecond*500),
ganim8.New(img, g32.Frames(20, "8-11", 20, "10-7"), time.Millisecond*600),
ganim8.New(img, g32.Frames("1-8", 4), time.Millisecond*700),
ganim8.New(img, g32.Frames(21, "8-11", 21, "10-7"), time.Millisecond*800),
ganim8.New(img, g32.Frames("1-8", 5), time.Millisecond*900),
}
// frame(w,h), image(w,h), offsets, border
g64 := ganim8.NewGrid(64, 64, 1024, 768, 299, 101, 2)
g.plane = ganim8.New(img, g64.Frames(1, "1-3"), time.Millisecond*100)
g.seaplane = ganim8.New(img, g64.Frames("2-4", 3), time.Millisecond*100)
g.seaplaneAngle = 0
// frame(w,h), image(w,h), offsets, border
gs := ganim8.NewGrid(32, 98, 1024, 768, 366, 102, 1)
g.submarine = ganim8.New(img, gs.Frames("1-7", 1, "6-2", 1),
// individual frame delays
map[string]time.Duration{
"1": time.Second * 2,
"2-6": time.Millisecond * 100,
"7": time.Second * 1,
"8-12": time.Millisecond * 100,
})
}
//go:embed assets/*
var assets embed.FS
func readImage(file string) image.Image {
b, _ := assets.ReadFile(file)
return bytes2Image(&b)
}
func bytes2Image(rawImage *[]byte) image.Image {
img, format, error := image.Decode(bytes.NewReader(*rawImage))
if error != nil {
log.Fatal("Bytes2Image Failed: ", format, error)
}
return img
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
if err := ebiten.RunGame(NewGame()); err != nil {
log.Fatal(err)
}
}