Added Music

This commit is contained in:
David Alasow 2017-12-22 23:10:41 +01:00
parent a6673a855b
commit 003fb15ecf
1 changed files with 50 additions and 8 deletions

View File

@ -6,18 +6,21 @@ import (
"image/color"
"io/ioutil"
"log"
"strings"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/text"
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/mp3"
)
const (
screenWidth = 640
screenHeight = 480
sampleRate = 44100
)
type dialogue struct {
@ -31,19 +34,46 @@ type character struct {
}
var (
m = character{`Me`, color.RGBA{255, 0, 0, 255}}
s = character{`Sylvie`, color.RGBA{0, 255, 0, 255}}
m = character{`Me`, color.RGBA{200, 200, 255, 255}}
s = character{`Sylvie`, color.RGBA{200, 255, 200, 255}}
d = character{``, color.RGBA{0, 255, 0, 0}}
messages = []dialogue{
{s, `Hi there! How was class?`},
{d, `playSong ./Departures.mp3`},
{s, `Hi there! How was class?`},
{m, `Good...`},
{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?`},
{d, `playSong ./Hisoku.mp3`},
{m, `Sure!`},
}
mplusNormalFont font.Face
line = 0
bg *ebiten.Image
audioContext *audio.Context
audioPlayer *audio.Player
playingMusic = false
)
func playSong(path string) {
var err error
if playingMusic {
audioPlayer.Close()
}
f, err := ebitenutil.OpenFile(path)
if err != nil {
log.Fatal(err)
}
d, err := mp3.Decode(audioContext, f)
if err != nil {
log.Fatal(err)
}
audioPlayer, err = audio.NewPlayer(audioContext, d)
if err != nil {
log.Fatal(err)
}
audioPlayer.Play()
playingMusic = true
}
func init() {
@ -69,6 +99,11 @@ func init() {
DPI: dpi,
Hinting: font.HintingFull,
})
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
}
@ -77,14 +112,21 @@ func update(screen *ebiten.Image) error {
if ebiten.IsRunningSlowly() {
return nil
}
action := strings.Split(messages[line].Text, " ")
for _, v := range ebiten.InputChars() {
if v == 32 {
if line+1 != len(messages) {
line++
}
if line+1 != len(messages) {
line++
}
}
}
action = strings.Split(messages[line].Text, " ")
if (action[0] == "playSong") {
if line+1 != len(messages) {
playSong(action[1])
line++
}
}
msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())
const x = 20