Music

Instructions

  • Click on demo to allow inputs
  • Press 1 key to toggle (turn on) music
music.go
  1package main
  2
  3import (
  4 "embed"
  5 "log"
  6
  7 "github.com/TheBitDrifter/bappa/blueprint"
  8
  9 "github.com/TheBitDrifter/bappa/blueprint/client"
 10 "github.com/TheBitDrifter/bappa/blueprint/vector"
 11 "github.com/TheBitDrifter/bappa/coldbrew"
 12 "github.com/TheBitDrifter/bappa/coldbrew/coldbrew_rendersystems"
 13 "github.com/TheBitDrifter/bappa/warehouse"
 14 "github.com/hajimehoshi/ebiten/v2"
 15 "github.com/hajimehoshi/ebiten/v2/inpututil"
 16 "github.com/hajimehoshi/ebiten/v2/text/v2"
 17 "golang.org/x/image/font/basicfont"
 18)
 19
 20//go:embed assets/*
 21var assets embed.FS
 22
 23func main() {
 24 client := coldbrew.NewClient(
 25  640,
 26  360,
 27  10,
 28  10,
 29  10,
 30  assets,
 31 )
 32
 33 client.SetTitle("Playing Music")
 34
 35 err := client.RegisterScene(
 36  "Example Scene",
 37  640,
 38  360,
 39  exampleScenePlan,
 40  []coldbrew.RenderSystem{instructions{}},
 41  []coldbrew.ClientSystem{
 42   &musicSystem{},
 43  },
 44  []blueprint.CoreSystem{},
 45 )
 46 if err != nil {
 47  log.Fatal(err)
 48 }
 49
 50 client.RegisterGlobalRenderSystem(coldbrew_rendersystems.GlobalRenderer{})
 51 client.ActivateCamera()
 52
 53 if err := client.Start(); err != nil {
 54  log.Fatal(err)
 55 }
 56}
 57
 58var musicSoundConfig = client.SoundConfig{
 59 Path:             "music.wav",
 60 AudioPlayerCount: 1,
 61}
 62
 63func exampleScenePlan(height, width int, sto warehouse.Storage) error {
 64 spriteArchetype, err := sto.NewOrExistingArchetype(
 65  client.Components.SoundBundle,
 66 )
 67 if err != nil {
 68  return err
 69 }
 70
 71 err = spriteArchetype.Generate(1,
 72  client.NewSoundBundle().AddSoundFromConfig(musicSoundConfig),
 73 )
 74 if err != nil {
 75  return err
 76 }
 77 return nil
 78}
 79
 80type musicSystem struct {
 81 volume float64
 82}
 83
 84func (sys *musicSystem) Run(lc coldbrew.LocalClient, scene coldbrew.Scene) error {
 85 if inpututil.IsKeyJustPressed(ebiten.Key1) && sys.volume == 0 {
 86  sys.volume = 1
 87 } else if inpututil.IsKeyJustPressed(ebiten.Key1) && sys.volume == 1 {
 88  sys.volume = 0
 89 }
 90
 91 musicQuery := warehouse.Factory.NewQuery().And(client.Components.SoundBundle)
 92 cursor := scene.NewCursor(musicQuery)
 93
 94 for range cursor.Next() {
 95  soundBundle := client.Components.SoundBundle.GetFromCursor(cursor)
 96
 97  sound, _ := coldbrew.MaterializeSound(soundBundle, musicSoundConfig)
 98  player := sound.GetAny()
 99  player.SetVolume(sys.volume)
100
101  if !player.IsPlaying() {
102   player.Rewind()
103   player.Play()
104  }
105 }
106 return nil
107}
108
109type instructions struct{}
110
111func (instructions) Render(scene coldbrew.Scene, screen coldbrew.Screen, cu coldbrew.CameraUtility) {
112 cam := cu.ActiveCamerasFor(scene)[0]
113 instructionText := "Press 1 to toggle music!"
114 textFace := text.NewGoXFace(basicfont.Face7x13)
115 cam.DrawTextBasicStatic(instructionText, &text.DrawOptions{}, textFace, vector.Two{
116  X: 230,
117  Y: 160,
118 })
119 cam.PresentToScreen(screen, 0)
120}