-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
192 lines (179 loc) · 3.58 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"bytes"
_ "embed"
"fmt"
"image"
"image/color"
_ "image/png"
"log"
"os"
"pokedexProject/pokemon"
"pokedexProject/util"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
)
var img *ebiten.Image
var factsFont font.Face
var artFont font.Face
//go:embed images/pokedex_open.png
var pokedexImage []byte
//go:embed fonts/SFNSMono.ttf
var loadedFonts []byte
func init() {
var err error
imgDecoded, _, err := image.Decode(bytes.NewReader(pokedexImage))
if err != nil {
log.Fatal(err)
}
img = ebiten.NewImageFromImage(imgDecoded)
tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}
const dpi = 72
factsFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 12,
DPI: dpi,
Hinting: font.HintingFull,
})
if err != nil {
log.Fatal(err)
}
art_tt, err := opentype.Parse(loadedFonts)
if err != nil {
log.Fatal(err)
}
artFont, err = opentype.NewFace(art_tt, &opentype.FaceOptions{
Size: 8,
DPI: dpi,
Hinting: font.HintingFull,
})
if err != nil {
log.Fatal(err)
}
}
type Game struct {
newPokemon bool
voice string
facts string
art string
text string
title string
runes []rune
counter int
}
func (g *Game) Update() error {
// Collect user input
g.runes = ebiten.AppendInputChars(g.runes[:0])
g.text += string(g.runes)
if util.RepeatKey(ebiten.KeyEnter) || util.RepeatKey(ebiten.KeyNumpadEnter) {
if g.text == "" {
return nil
}
if g.text == "yes" {
g.text = ""
g.facts = ""
g.art = ""
g.newPokemon = false
} else if g.text == "no" {
os.Exit(1)
} else {
// Attempt to find pokemon info.
content, found, err := pokemon.GetBase(g.text)
if err != nil {
log.Fatal(err)
return err
}
if !found {
g.facts = "Check spelling or internet.\nRetry\n"
} else if found {
art, err := pokemon.GetArt(content)
if err != nil {
log.Fatal(err)
g.art = fmt.Sprintf("%v", err)
}
g.art = art
g.facts = pokemon.GetInfo(content)
}
g.text = ""
g.newPokemon = true
g.voice = "initial"
}
}
if g.voice == "drawn" {
g.voice = "off"
util.OakLine(g.facts)
}
// If the backspace key is pressed, remove one character.
if util.RepeatKey(ebiten.KeyBackspace) {
if len(g.text) >= 1 {
g.text = g.text[:len(g.text)-1]
}
}
g.counter++
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
// x, y are the text box coordinates.
const (
x_facts = 634
y_facts = 280
x_art = 120
y_art = 270
)
screen.DrawImage(img, nil)
if !g.newPokemon {
prompt := "Type a Pokemon's name:\n" + g.text
text.Draw(screen,
prompt,
factsFont,
x_facts,
y_facts,
color.White)
text.Draw(screen,
g.title,
artFont,
x_art+30,
y_art,
color.White)
}
if g.newPokemon {
// draw ascii art
text.Draw(screen,
g.art,
artFont,
x_art,
y_art,
color.White)
// draw collected facts
wrappedFacts := util.WordWrap(g.facts, 50)
text.Draw(screen,
wrappedFacts+g.text,
factsFont,
x_facts,
y_facts,
color.White)
if g.voice == "initial" {
g.voice = "drawn"
}
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return outsideWidth, outsideHeight
}
func main() {
ebiten.SetWindowSize(1040, 880)
ebiten.SetWindowTitle("Pokédex")
ebiten.SetWindowResizable(true)
game := &Game{
title: util.Banner(),
counter: 0,
}
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
}
}