-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon.go
94 lines (80 loc) · 2 KB
/
icon.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
package main
import (
"bytes"
"image"
"log"
"github.com/hajimehoshi/ebiten/v2"
)
type icon struct {
x, y int
width, height int
scale float64
zindex int
image *ebiten.Image
}
func newIcon(x, y int, img *ebiten.Image) *icon {
iconWidth, iconHeight := 100, 100
sizeWidth, sizeHeight := img.Bounds().Dx(), img.Bounds().Dy()
var scale float64
if sizeWidth > sizeHeight {
scale = float64(iconWidth) / float64(img.Bounds().Dx())
iconHeight = int(float64(sizeHeight) * scale)
} else {
scale = float64(iconHeight) / float64(img.Bounds().Dy())
iconWidth = int(float64(sizeWidth) * scale)
}
return &icon{
x: x,
y: y,
width: iconWidth,
height: iconHeight,
scale: scale,
zindex: 15,
image: img,
}
}
func (i *icon) Draw(screen *ebiten.Image) {
opts := &ebiten.DrawImageOptions{}
opts.GeoM.Scale(i.scale, i.scale)
opts.GeoM.Translate(float64(i.x)-float64(i.width)/2, float64(i.y)-float64(i.height)/2)
screen.DrawImage(i.image, opts)
}
func (i *icon) ZIndex() int {
return i.zindex
}
// TODO: これらは icon.go にいるのはふさわしくない
func newHouseIcon(x, y int) *icon {
img, _, err := image.Decode(bytes.NewReader(houseImageData))
if err != nil {
log.Fatal(err)
}
return newIcon(x, y, ebiten.NewImageFromImage(img))
}
func newBarricadeIcon(x, y int) *icon {
img, _, err := image.Decode(bytes.NewReader(barricadeImageData))
if err != nil {
log.Fatal(err)
}
return newIcon(x, y, ebiten.NewImageFromImage(img))
}
func newBugIcon(x, y int, bugColor bugColor) *icon {
img, _, err := image.Decode(bytes.NewReader(bugsImageData))
if err != nil {
log.Fatal(err)
}
bugsImage := ebiten.NewImageFromImage(img)
rect := func() image.Rectangle {
switch bugColor {
case bugsRed:
return redBug()
case bugsBlue:
return blueBug()
case bugsGreen:
return greenBug()
}
log.Fatal("invalid bug color")
return image.Rectangle{}
}()
bugImage := bugsImage.SubImage(rect).(*ebiten.Image)
return newIcon(x, y, bugImage)
}