Ran gofmt

This commit is contained in:
David Alasow 2017-12-22 23:13:31 +01:00
parent 003fb15ecf
commit 291aff7aea

View File

@ -1,155 +1,154 @@
package main package main
import ( import (
"fmt"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"image/color"
_ "image/jpeg" _ "image/jpeg"
"fmt" "io/ioutil"
"image/color" "log"
"io/ioutil" "strings"
"log"
"strings"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/text" "github.com/hajimehoshi/ebiten/audio/mp3"
"github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/audio/mp3" "github.com/hajimehoshi/ebiten/text"
) )
const ( const (
screenWidth = 640 screenWidth = 640
screenHeight = 480 screenHeight = 480
sampleRate = 44100 sampleRate = 44100
) )
type dialogue struct { type dialogue struct {
Speaker character Speaker character
Text string Text string
} }
type character struct { type character struct {
Name string Name string
Color color.RGBA Color color.RGBA
} }
var ( var (
m = character{`Me`, color.RGBA{200, 200, 255, 255}} m = character{`Me`, color.RGBA{200, 200, 255, 255}}
s = character{`Sylvie`, color.RGBA{200, 255, 200, 255}} s = character{`Sylvie`, color.RGBA{200, 255, 200, 255}}
d = character{``, color.RGBA{0, 255, 0, 0}} d = character{``, color.RGBA{0, 255, 0, 0}}
messages = []dialogue{ messages = []dialogue{
{d, `playSong ./Departures.mp3`}, {d, `playSong ./Departures.mp3`},
{s, `Hi there! How was class?`}, {s, `Hi there! How was class?`},
{m, `Good...`}, {m, `Good...`},
{d, `I can't bring myself to admit that it all went in one ear and out the other.`}, {d, `I can't bring myself to admit that it all went in one ear and out the other.`},
{s, `Are you going home now? Wanna walk back with me?`}, {s, `Are you going home now? Wanna walk back with me?`},
{d, `playSong ./Hisoku.mp3`}, {d, `playSong ./Hisoku.mp3`},
{m, `Sure!`}, {m, `Sure!`},
} }
mplusNormalFont font.Face mplusNormalFont font.Face
line = 0 line = 0
bg *ebiten.Image bg *ebiten.Image
audioContext *audio.Context audioContext *audio.Context
audioPlayer *audio.Player audioPlayer *audio.Player
playingMusic = false playingMusic = false
) )
func playSong(path string) { func playSong(path string) {
var err error var err error
if playingMusic { if playingMusic {
audioPlayer.Close() audioPlayer.Close()
} }
f, err := ebitenutil.OpenFile(path) f, err := ebitenutil.OpenFile(path)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
d, err := mp3.Decode(audioContext, f) d, err := mp3.Decode(audioContext, f)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
audioPlayer, err = audio.NewPlayer(audioContext, d) audioPlayer, err = audio.NewPlayer(audioContext, d)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
audioPlayer.Play() audioPlayer.Play()
playingMusic = true playingMusic = true
} }
func init() { func init() {
f, err := ebitenutil.OpenFile("./mplus-1p-regular.ttf") f, err := ebitenutil.OpenFile("./mplus-1p-regular.ttf")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer f.Close() defer f.Close()
b, err := ioutil.ReadAll(f) b, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
tt, err := truetype.Parse(b) tt, err := truetype.Parse(b)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
const dpi = 72 const dpi = 72
mplusNormalFont = truetype.NewFace(tt, &truetype.Options{ mplusNormalFont = truetype.NewFace(tt, &truetype.Options{
Size: 24, Size: 24,
DPI: dpi, DPI: dpi,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
audioContext, err = audio.NewContext(sampleRate) audioContext, err = audio.NewContext(sampleRate)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
} }
action := strings.Split(messages[line].Text, " ") action := strings.Split(messages[line].Text, " ")
for _, v := range ebiten.InputChars() { for _, v := range ebiten.InputChars() {
if v == 32 { if v == 32 {
if line+1 != len(messages) { if line+1 != len(messages) {
line++ line++
} }
} }
} }
action = strings.Split(messages[line].Text, " ") action = strings.Split(messages[line].Text, " ")
if (action[0] == "playSong") { if action[0] == "playSong" {
if line+1 != len(messages) { if line+1 != len(messages) {
playSong(action[1]) playSong(action[1])
line++ line++
} }
} }
msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS()) msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())
const x = 20 const x = 20
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
screen.DrawImage(bg, op) screen.DrawImage(bg, op)
ebitenutil.DrawRect(screen, 30, 350, screenWidth-60, 100, color.RGBA{128, 128, 128, 200}) ebitenutil.DrawRect(screen, 30, 350, screenWidth-60, 100, color.RGBA{128, 128, 128, 200})
text.Draw(screen, msg, mplusNormalFont, x, 40, color.White) text.Draw(screen, msg, mplusNormalFont, x, 40, color.White)
text.Draw(screen, messages[line].Speaker.Name, mplusNormalFont, 50, 370, messages[line].Speaker.Color) text.Draw(screen, messages[line].Speaker.Name, mplusNormalFont, 50, 370, messages[line].Speaker.Color)
text.Draw(screen, messages[line].Text, mplusNormalFont, 50, 400, color.White) text.Draw(screen, messages[line].Text, mplusNormalFont, 50, 400, color.White)
return nil return nil
} }
func main() { func main() {
var err error var err error
bg, _, err = ebitenutil.NewImageFromFile("./bg.jpg", ebiten.FilterNearest) bg, _, err = ebitenutil.NewImageFromFile("./bg.jpg", ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Font (Ebiten Demo)"); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Font (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }