Skip to content

Instantly share code, notes, and snippets.

@Noofbiz
Created November 26, 2017 14:58
Show Gist options
  • Save Noofbiz/0b57df144787007d5e0fdd8d4d0ad74d to your computer and use it in GitHub Desktop.
Save Noofbiz/0b57df144787007d5e0fdd8d4d0ad74d to your computer and use it in GitHub Desktop.
Handling modifier keys in Engo maybe?
package main
import (
"fmt"
"engo.io/ecs"
"engo.io/engo"
"engo.io/engo/common"
)
type DefaultScene struct{}
func (*DefaultScene) Preload() {}
func (*DefaultScene) Setup(w *ecs.World) {
w.AddSystem(&common.RenderSystem{})
w.AddSystem(&InputSystem{})
engo.Input.RegisterAxis("sideways", engo.AxisKeyPair{engo.A, engo.D})
engo.Input.RegisterButton("action", engo.Space, engo.Enter)
engo.Input.RegisterButton("modifier", engo.LeftShift, engo.RightShift, engo.LeftAlt, engo.RightAlt)
}
func (*DefaultScene) Type() string { return "Game" }
type inputEntity struct {
*ecs.BasicEntity
}
type InputSystem struct {
entities []inputEntity
}
func (c *InputSystem) Add(basic *ecs.BasicEntity) {
c.entities = append(c.entities, inputEntity{basic})
}
func (c *InputSystem) Remove(basic ecs.BasicEntity) {
delete := -1
for index, e := range c.entities {
if e.BasicEntity.ID() == basic.ID() {
delete = index
break
}
}
if delete >= 0 {
c.entities = append(c.entities[:delete], c.entities[delete+1:]...)
}
}
func (c *InputSystem) Update(dt float32) {
if v := engo.Input.Axis("sideways").Value(); v != 0 {
fmt.Println(v)
}
btn := engo.Input.Button("action")
mod := engo.Input.Button("modifier")
if mod.Down() {
if btn.JustPressed() {
fmt.Println("Modified!")
}
} else {
if btn.JustPressed() {
fmt.Println("DOWN!")
}
}
}
func main() {
opts := engo.RunOptions{
Title: "Input Demo",
Width: 1024,
Height: 640,
}
engo.Run(opts, &DefaultScene{})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment