Keyboard

Instructions

  • Click on demo to allow inputs
  • WASD controls sprite
  • Arrow keys also controls sprite
keyboard.go
  1package main
  2
  3import (
  4 "embed"
  5 "log"
  6
  7 "github.com/TheBitDrifter/bappa/blueprint"
  8 "github.com/TheBitDrifter/bappa/blueprint/client"
  9 "github.com/TheBitDrifter/bappa/blueprint/input"
 10 "github.com/TheBitDrifter/bappa/coldbrew"
 11 "github.com/TheBitDrifter/bappa/coldbrew/coldbrew_clientsystems"
 12 "github.com/TheBitDrifter/bappa/coldbrew/coldbrew_rendersystems"
 13 "github.com/TheBitDrifter/bappa/tteokbokki/spatial"
 14
 15 "github.com/TheBitDrifter/bappa/warehouse"
 16 "github.com/hajimehoshi/ebiten/v2"
 17)
 18
 19//go:embed assets/*
 20var assets embed.FS
 21
 22var actions = struct {
 23 Up, Down, Left, Right input.Input
 24}{
 25 Up:    input.NewInput(),
 26 Down:  input.NewInput(),
 27 Left:  input.NewInput(),
 28 Right: input.NewInput(),
 29}
 30
 31func main() {
 32 client := coldbrew.NewClient(
 33  640,
 34  360,
 35  10,
 36  10,
 37  10,
 38  assets,
 39 )
 40
 41 client.SetTitle("Capturing Keyboard Inputs")
 42
 43 err := client.RegisterScene(
 44  "Example Scene",
 45  640,
 46  360,
 47  exampleScenePlan,
 48  []coldbrew.RenderSystem{},
 49  []coldbrew.ClientSystem{},
 50  []blueprint.CoreSystem{
 51   inputSystem{},
 52  },
 53 )
 54 if err != nil {
 55  log.Fatal(err)
 56 }
 57
 58 client.RegisterGlobalRenderSystem(coldbrew_rendersystems.GlobalRenderer{})
 59 client.RegisterGlobalClientSystem(coldbrew_clientsystems.InputBufferSystem{})
 60 client.ActivateCamera()
 61
 62 receiver, _ := client.ActivateReceiver()
 63
 64 receiver.RegisterKey(ebiten.KeyUp, actions.Up)
 65 receiver.RegisterKey(ebiten.KeyW, actions.Up)
 66
 67 receiver.RegisterKey(ebiten.KeyDown, actions.Down)
 68 receiver.RegisterKey(ebiten.KeyS, actions.Down)
 69
 70 receiver.RegisterKey(ebiten.KeyLeft, actions.Left)
 71 receiver.RegisterKey(ebiten.KeyA, actions.Left)
 72
 73 receiver.RegisterKey(ebiten.KeyRight, actions.Right)
 74 receiver.RegisterKey(ebiten.KeyD, actions.Right)
 75
 76 if err := client.Start(); err != nil {
 77  log.Fatal(err)
 78 }
 79}
 80
 81func exampleScenePlan(height, width int, sto warehouse.Storage) error {
 82 spriteArchetype, err := sto.NewOrExistingArchetype(
 83  input.Components.InputBuffer,
 84  spatial.Components.Position,
 85  client.Components.SpriteBundle,
 86 )
 87 if err != nil {
 88  return err
 89 }
 90
 91 err = spriteArchetype.Generate(1,
 92  input.Components.InputBuffer,
 93
 94  spatial.NewPosition(255, 20),
 95  client.NewSpriteBundle().
 96   AddSprite("sprite.png", true),
 97 )
 98 if err != nil {
 99  return err
100 }
101 return nil
102}
103
104type inputSystem struct{}
105
106func (inputSystem) Run(scene blueprint.Scene, _ float64) error {
107 query := warehouse.Factory.NewQuery().
108  And(input.Components.InputBuffer, spatial.Components.Position)
109
110 cursor := scene.NewCursor(query)
111
112 for range cursor.Next() {
113  pos := spatial.Components.Position.GetFromCursor(cursor)
114  inputBuffer := input.Components.InputBuffer.GetFromCursor(cursor)
115
116  if stampedAction, ok := inputBuffer.ConsumeInput(actions.Up); ok {
117   log.Println("Tick", stampedAction.Tick)
118   pos.Y -= 2
119  }
120  if stampedAction, ok := inputBuffer.ConsumeInput(actions.Down); ok {
121   log.Println("Tick", stampedAction.Tick)
122   pos.Y += 2
123  }
124  if stampedAction, ok := inputBuffer.ConsumeInput(actions.Left); ok {
125   log.Println("Tick", stampedAction.Tick)
126   pos.X -= 2
127  }
128  if stampedAction, ok := inputBuffer.ConsumeInput(actions.Right); ok {
129   log.Println("Tick", stampedAction.Tick)
130   pos.X += 2
131  }
132
133 }
134 return nil
135}