Started implementing the event system.

This commit is contained in:
Andrey Parhomenko 2023-11-23 22:05:22 +03:00
parent a9a4764432
commit e1504de702
4 changed files with 259 additions and 11 deletions

View file

@ -184,43 +184,43 @@ func (p *Player) Update(e *gg.Engine) error {
case ebiten.KeyT: case ebiten.KeyT:
c.R -= gg.Pi * p.ScaleSpeed * dt c.R -= gg.Pi * p.ScaleSpeed * dt
case ebiten.KeyRightBracket: case ebiten.KeyRightBracket:
if e.KeyIsPressed(ebiten.KeyShift) { if e.IsPressed(ebiten.KeyShift) {
p.R -= gg.Pi * 0.3 * dt p.R -= gg.Pi * 0.3 * dt
} else { } else {
p.R += gg.Pi * 0.3 * dt p.R += gg.Pi * 0.3 * dt
} }
case ebiten.KeyF: case ebiten.KeyF:
if e.KeyIsPressed(ebiten.KeyShift) { if e.IsPressed(ebiten.KeyShift) {
c.S.X -= gg.Pi * p.ScaleSpeed * dt c.S.X -= gg.Pi * p.ScaleSpeed * dt
} else { } else {
c.S.X += gg.Pi * p.ScaleSpeed * dt c.S.X += gg.Pi * p.ScaleSpeed * dt
} }
case ebiten.KeyG: case ebiten.KeyG:
if e.KeyIsPressed(ebiten.KeyShift) { if e.IsPressed(ebiten.KeyShift) {
c.S.Y -= gg.Pi * p.ScaleSpeed * dt c.S.Y -= gg.Pi * p.ScaleSpeed * dt
} else { } else {
c.S.Y += gg.Pi * p.ScaleSpeed * dt c.S.Y += gg.Pi * p.ScaleSpeed * dt
} }
case ebiten.KeyZ: case ebiten.KeyZ:
if e.KeyIsPressed(ebiten.KeyShift) { if e.IsPressed(ebiten.KeyShift) {
c.RA.X -= gg.Pi * p.MoveSpeed * dt c.RA.X -= gg.Pi * p.MoveSpeed * dt
} else { } else {
c.RA.X += gg.Pi * p.MoveSpeed * dt c.RA.X += gg.Pi * p.MoveSpeed * dt
} }
case ebiten.KeyX: case ebiten.KeyX:
if e.KeyIsPressed(ebiten.KeyShift) { if e.IsPressed(ebiten.KeyShift) {
c.RA.Y -= gg.Pi * p.MoveSpeed * dt c.RA.Y -= gg.Pi * p.MoveSpeed * dt
} else { } else {
c.RA.Y += gg.Pi * p.MoveSpeed * dt c.RA.Y += gg.Pi * p.MoveSpeed * dt
} }
case ebiten.KeyV: case ebiten.KeyV:
if e.KeyIsPressed(ebiten.KeyShift) { if e.IsPressed(ebiten.KeyShift) {
tri.R -= gg.Pi * 0.3 * dt tri.R -= gg.Pi * 0.3 * dt
} else { } else {
tri.R += gg.Pi * 0.3 * dt tri.R += gg.Pi * 0.3 * dt
} }
case ebiten.KeyLeftBracket: case ebiten.KeyLeftBracket:
if e.KeyIsPressed(ebiten.KeyShift) { if e.IsPressed(ebiten.KeyShift) {
rect.R -= gg.Pi * 0.3 * dt rect.R -= gg.Pi * 0.3 * dt
} else { } else {
rect.R += gg.Pi * 0.3 * dt rect.R += gg.Pi * 0.3 * dt
@ -228,6 +228,18 @@ func (p *Player) Update(e *gg.Engine) error {
case ebiten.Key0: case ebiten.Key0:
e.Del(p) e.Del(p)
case ebiten.KeyB: case ebiten.KeyB:
}
}
return nil
}
func (p *Player) Event(e *gg.Engine, ev any) {
fmt.Println("event:", ev)
switch ec := ev.(type) {
case *gg.KeyDown:
switch {
case ec.Key == gg.KeyB :
if p.Layer != PlayerL { if p.Layer != PlayerL {
p.Layer = PlayerL p.Layer = PlayerL
} else { } else {
@ -235,8 +247,6 @@ func (p *Player) Update(e *gg.Engine) error {
} }
} }
} }
return nil
} }
func (d *Debug) Draw( func (d *Debug) Draw(

View file

@ -35,7 +35,8 @@ type Engine struct {
lastTime time.Time lastTime time.Time
dt Float dt Float
camera *Camera camera *Camera
keys []Key keys, prevKeys []Key
outerEvents, handleEvents EventChan
} }
type engine Engine type engine Engine
@ -72,9 +73,15 @@ func NewEngine(
}, },
}, },
objects: maps.NewOrdered[Object, struct{}](), objects: maps.NewOrdered[Object, struct{}](),
outerEvents: make(EventChan),
handleEvents: make(EventChan),
} }
} }
func (e *Engine) EventInput() EventChan {
return e.outerEvents
}
// Add new object considering what // Add new object considering what
// interfaces it implements. // interfaces it implements.
func (e *Engine) Add(b any) error { func (e *Engine) Add(b any) error {
@ -119,11 +126,35 @@ func (e *engine) Update() error {
var err error var err error
eng := (*Engine)(e) eng := (*Engine)(e)
e.prevKeys = e.keys
e.keys = inpututil. e.keys = inpututil.
AppendPressedKeys(e.keys[:0]) AppendPressedKeys(e.keys[:0])
events := []any{}
diff := keyDiff(e.prevKeys, e.keys)
for _, key := range diff {
var event any
if eng.IsPressed(key) {
event = &KeyDown{
Key: key,
}
} else {
event = &KeyUp{
Key: key,
}
}
events = append(events, event)
}
e.dt = time.Since(e.lastTime).Seconds() e.dt = time.Since(e.lastTime).Seconds()
for object := range e.objects.KeyChan() { for object := range e.objects.KeyChan() {
eventer, ok := object.(Eventer)
if ok {
for _, event := range events {
eventer.Event(eng, event)
}
}
updater, ok := object.(Updater) updater, ok := object.(Updater)
if !ok { if !ok {
continue continue

57
event.go Normal file
View file

@ -0,0 +1,57 @@
package gg
import (
"github.com/hajimehoshi/ebiten/v2"
)
type Eventer interface {
Event(e *Engine, ev any)
}
func keyDiff(s1, s2 []Key) []Key {
combinedSlice := append(s1, s2...)
dm := make(map[Key]int)
for _, v := range combinedSlice {
if _, ok := dm[v]; ok {
// remove element later as it exist in both slice.
dm[v] += 1
continue
}
// new entry, add in map!
dm[v] = 1
}
var retSlice []Key
for k, v := range dm {
if v == 1 {
retSlice = append(retSlice, k)
}
}
return retSlice
}
type KeyDown struct {
Key
}
type KeyUp struct {
Key
}
type MouseButton = ebiten.MouseButton
type MouseButtonDown struct {
MouseButton
P Vector
}
type MouseButtonUp struct {
MouseButton
P Vector
}
type MouseMove struct {
Delta Vector
}
type EventChan chan any

152
keys.go
View file

@ -5,8 +5,158 @@ import (
) )
type Key = ebiten.Key type Key = ebiten.Key
const (
KeyA Key = Key(ebiten.KeyA)
KeyB Key = Key(ebiten.KeyB)
KeyC Key = Key(ebiten.KeyC)
KeyD Key = Key(ebiten.KeyD)
KeyE Key = Key(ebiten.KeyE)
KeyF Key = Key(ebiten.KeyF)
KeyG Key = Key(ebiten.KeyG)
KeyH Key = Key(ebiten.KeyH)
KeyI Key = Key(ebiten.KeyI)
KeyJ Key = Key(ebiten.KeyJ)
KeyK Key = Key(ebiten.KeyK)
KeyL Key = Key(ebiten.KeyL)
KeyM Key = Key(ebiten.KeyM)
KeyN Key = Key(ebiten.KeyN)
KeyO Key = Key(ebiten.KeyO)
KeyP Key = Key(ebiten.KeyP)
KeyQ Key = Key(ebiten.KeyQ)
KeyR Key = Key(ebiten.KeyR)
KeyS Key = Key(ebiten.KeyS)
KeyT Key = Key(ebiten.KeyT)
KeyU Key = Key(ebiten.KeyU)
KeyV Key = Key(ebiten.KeyV)
KeyW Key = Key(ebiten.KeyW)
KeyX Key = Key(ebiten.KeyX)
KeyY Key = Key(ebiten.KeyY)
KeyZ Key = Key(ebiten.KeyZ)
KeyAltLeft Key = Key(ebiten.KeyAltLeft)
KeyAltRight Key = Key(ebiten.KeyAltRight)
//KeyArrowDown Key = Key(ebiten.KeyArrowDown)
//KeyArrowLeft Key = Key(ebiten.KeyArrowLeft)
//KeyArrowRight Key = Key(ebiten.KeyArrowRight)
//KeyArrowUp Key = Key(ebiten.KeyArrowUp)
KeyBackquote Key = Key(ebiten.KeyBackquote)
KeyBackslash Key = Key(ebiten.KeyBackslash)
KeyBackspace Key = Key(ebiten.KeyBackspace)
KeyBracketLeft Key = Key(ebiten.KeyBracketLeft)
KeyBracketRight Key = Key(ebiten.KeyBracketRight)
KeyCapsLock Key = Key(ebiten.KeyCapsLock)
KeyComma Key = Key(ebiten.KeyComma)
KeyContextMenu Key = Key(ebiten.KeyContextMenu)
KeyControlLeft Key = Key(ebiten.KeyControlLeft)
KeyControlRight Key = Key(ebiten.KeyControlRight)
KeyDelete Key = Key(ebiten.KeyDelete)
KeyDigit0 Key = Key(ebiten.KeyDigit0)
KeyDigit1 Key = Key(ebiten.KeyDigit1)
KeyDigit2 Key = Key(ebiten.KeyDigit2)
KeyDigit3 Key = Key(ebiten.KeyDigit3)
KeyDigit4 Key = Key(ebiten.KeyDigit4)
KeyDigit5 Key = Key(ebiten.KeyDigit5)
KeyDigit6 Key = Key(ebiten.KeyDigit6)
KeyDigit7 Key = Key(ebiten.KeyDigit7)
KeyDigit8 Key = Key(ebiten.KeyDigit8)
KeyDigit9 Key = Key(ebiten.KeyDigit9)
KeyEnd Key = Key(ebiten.KeyEnd)
KeyEnter Key = Key(ebiten.KeyEnter)
KeyEqual Key = Key(ebiten.KeyEqual)
KeyEscape Key = Key(ebiten.KeyEscape)
KeyF1 Key = Key(ebiten.KeyF1)
KeyF2 Key = Key(ebiten.KeyF2)
KeyF3 Key = Key(ebiten.KeyF3)
KeyF4 Key = Key(ebiten.KeyF4)
KeyF5 Key = Key(ebiten.KeyF5)
KeyF6 Key = Key(ebiten.KeyF6)
KeyF7 Key = Key(ebiten.KeyF7)
KeyF8 Key = Key(ebiten.KeyF8)
KeyF9 Key = Key(ebiten.KeyF9)
KeyF10 Key = Key(ebiten.KeyF10)
KeyF11 Key = Key(ebiten.KeyF11)
KeyF12 Key = Key(ebiten.KeyF12)
KeyHome Key = Key(ebiten.KeyHome)
KeyInsert Key = Key(ebiten.KeyInsert)
KeyMetaLeft Key = Key(ebiten.KeyMetaLeft)
KeyMetaRight Key = Key(ebiten.KeyMetaRight)
KeyMinus Key = Key(ebiten.KeyMinus)
KeyNumLock Key = Key(ebiten.KeyNumLock)
KeyNumpad0 Key = Key(ebiten.KeyNumpad0)
KeyNumpad1 Key = Key(ebiten.KeyNumpad1)
KeyNumpad2 Key = Key(ebiten.KeyNumpad2)
KeyNumpad3 Key = Key(ebiten.KeyNumpad3)
KeyNumpad4 Key = Key(ebiten.KeyNumpad4)
KeyNumpad5 Key = Key(ebiten.KeyNumpad5)
KeyNumpad6 Key = Key(ebiten.KeyNumpad6)
KeyNumpad7 Key = Key(ebiten.KeyNumpad7)
KeyNumpad8 Key = Key(ebiten.KeyNumpad8)
KeyNumpad9 Key = Key(ebiten.KeyNumpad9)
KeyNumpadAdd Key = Key(ebiten.KeyNumpadAdd)
KeyNumpadDecimal Key = Key(ebiten.KeyNumpadDecimal)
KeyNumpadDivide Key = Key(ebiten.KeyNumpadDivide)
KeyNumpadEnter Key = Key(ebiten.KeyNumpadEnter)
KeyNumpadEqual Key = Key(ebiten.KeyNumpadEqual)
KeyNumpadMultiply Key = Key(ebiten.KeyNumpadMultiply)
KeyNumpadSubtract Key = Key(ebiten.KeyNumpadSubtract)
KeyPageDown Key = Key(ebiten.KeyPageDown)
KeyPageUp Key = Key(ebiten.KeyPageUp)
KeyPause Key = Key(ebiten.KeyPause)
KeyPeriod Key = Key(ebiten.KeyPeriod)
KeyPrintScreen Key = Key(ebiten.KeyPrintScreen)
KeyQuote Key = Key(ebiten.KeyQuote)
KeyScrollLock Key = Key(ebiten.KeyScrollLock)
KeySemicolon Key = Key(ebiten.KeySemicolon)
KeyShiftLeft Key = Key(ebiten.KeyShiftLeft)
KeyShiftRight Key = Key(ebiten.KeyShiftRight)
KeySlash Key = Key(ebiten.KeySlash)
KeySpace Key = Key(ebiten.KeySpace)
KeyTab Key = Key(ebiten.KeyTab)
KeyAlt Key = Key(ebiten.KeyAlt)
KeyControl Key = Key(ebiten.KeyControl)
KeyShift Key = Key(ebiten.KeyShift)
KeyMeta Key = Key(ebiten.KeyMeta)
KeyMax Key = KeyMeta
func (e *Engine) KeyIsPressed(k Key) bool { // Keys for backward compatibility.
// Deprecated: as of v2.1.
Key0 Key = Key(ebiten.KeyDigit0)
Key1 Key = Key(ebiten.KeyDigit1)
Key2 Key = Key(ebiten.KeyDigit2)
Key3 Key = Key(ebiten.KeyDigit3)
Key4 Key = Key(ebiten.KeyDigit4)
Key5 Key = Key(ebiten.KeyDigit5)
Key6 Key = Key(ebiten.KeyDigit6)
Key7 Key = Key(ebiten.KeyDigit7)
Key8 Key = Key(ebiten.KeyDigit8)
Key9 Key = Key(ebiten.KeyDigit9)
KeyApostrophe Key = Key(ebiten.KeyQuote)
KeyArrowDown Key = Key(ebiten.KeyArrowDown)
KeyGraveAccent Key = Key(ebiten.KeyBackquote)
KeyKP0 Key = Key(ebiten.KeyNumpad0)
KeyKP1 Key = Key(ebiten.KeyNumpad1)
KeyKP2 Key = Key(ebiten.KeyNumpad2)
KeyKP3 Key = Key(ebiten.KeyNumpad3)
KeyKP4 Key = Key(ebiten.KeyNumpad4)
KeyKP5 Key = Key(ebiten.KeyNumpad5)
KeyKP6 Key = Key(ebiten.KeyNumpad6)
KeyKP7 Key = Key(ebiten.KeyNumpad7)
KeyKP8 Key = Key(ebiten.KeyNumpad8)
KeyKP9 Key = Key(ebiten.KeyNumpad9)
KeyKPAdd Key = Key(ebiten.KeyNumpadAdd)
KeyKPDecimal Key = Key(ebiten.KeyNumpadDecimal)
KeyKPDivide Key = Key(ebiten.KeyNumpadDivide)
KeyKPEnter Key = Key(ebiten.KeyNumpadEnter)
KeyKPEqual Key = Key(ebiten.KeyNumpadEqual)
KeyKPMultiply Key = Key(ebiten.KeyNumpadMultiply)
KeyKPSubtract Key = Key(ebiten.KeyNumpadSubtract)
KeyArrowLeft Key = Key(ebiten.KeyArrowLeft)
KeyLeftBracket Key = Key(ebiten.KeyBracketLeft)
KeyMenu Key = Key(ebiten.KeyContextMenu)
KeyArrowRight Key = Key(ebiten.KeyArrowRight)
KeyRightBracket Key = Key(ebiten.KeyBracketRight)
KeyArrowUp Key = Key(ebiten.KeyArrowUp)
)
func (e *Engine) IsPressed(k Key) bool {
keys := e.Keys() keys := e.Keys()
for _, v := range keys { for _, v := range keys {
if v == k { if v == k {