diff --git a/cmd/test/debug.go b/cmd/test/debug.go index 8b3419a..dd8d16e 100644 --- a/cmd/test/debug.go +++ b/cmd/test/debug.go @@ -14,6 +14,9 @@ type Debug struct { func (d *Debug) Draw(c *Context) { e := c.Engine keyStrs := []string{} + keyStrs = append(keyStrs, fmt.Sprintf( + "counter: %d", counter, + )) keyStrs = append(keyStrs, fmt.Sprintf( "tps: %d", int(c.TPS()), )) diff --git a/cmd/test/player.go b/cmd/test/player.go index e3f83db..5b1e5e4 100644 --- a/cmd/test/player.go +++ b/cmd/test/player.go @@ -99,12 +99,6 @@ func (p *Player) Update(c *Context) { } else { cam.Scale.Y += gg.Pi * p.ScaleSpeed * dt } - case gg.KeyV: - if shift { - tri.Rotation -= gg.Pi * 0.3 * dt - } else { - tri.Rotation += gg.Pi * 0.3 * dt - } case gg.KeyLeftBracket: if shift { rect.Rotation -= gg.Pi * 0.3 * dt @@ -117,10 +111,8 @@ func (p *Player) Update(c *Context) { } } -} - -func (p *Player) Event(c *gg.Context) { - switch ec := c.Event.(type) { + for _, event := range c.Events { + switch ec := event.(type) { case *gg.KeyDown: switch { case ec.Key == gg.KeyB: @@ -140,6 +132,9 @@ func (p *Player) Event(c *gg.Context) { c.Camera.Scale = c.Camera.Scale.Add(gg.V2( ec.Offset.Y * c.DT() * p.ScaleSpeed * 40, )) - } + }} +} + +func (p *Player) Event(c *gg.Context) { } diff --git a/cmd/test/trianlge.go b/cmd/test/trianlge.go index 71abbd3..3bb7cce 100644 --- a/cmd/test/trianlge.go +++ b/cmd/test/trianlge.go @@ -1,7 +1,11 @@ package main import "vultras.su/core/gg" -import "fmt" +//import "fmt" + +var ( + counter int +) type Tri struct { gg.DrawablePolygon @@ -28,56 +32,56 @@ func NewTri() *Tri { } func (t *Tri) Update(c *Context) { + if t.Spawned { + return + } + dt := c.DT() if t.ContainsPoint(c.AbsCursorPosition()) { t.Color = gg.Rgba(0, 1, 0, 1) } else { t.Color = gg.Rgba(1, 0, 1, 1) } - /*if t.Spawned { - return - }*/ + keys := c.Keys() d := +1. if c.IsPressed(gg.KeyShift) { - d = -1. + d = -1 } - if c.IsPressed(gg.KeyM) { - absPos := tri.AbsPosition() - tri.SetAbsPosition( + + for _, key := range keys { switch key { + case gg.KeyM: + absPos := t.AbsPosition() + t.SetAbsPosition( absPos.Add(gg.V(0, 100*c.DT()*d)), ) - } - if c.IsPressed(gg.KeyN) { - absPos := tri.AbsPosition() - tri.SetAbsPosition( + case gg.KeyN: + absPos := t.AbsPosition() + t.SetAbsPosition( absPos.Add(gg.V(100*c.DT()*d, 0)), ) - } -} + case gg.KeyV: + t.Rotation += d * gg.Pi * 0.3 * dt + case gg.Key2 : + if t.Spawned { + break + } + counter++ + tt := *t + tt.Spawned = true + tt.Disconnect() + c.Spawn(&tt) + }} -func (t *Tri) Event(c *Context) { - switch e := c.Event.(type) { - case *gg.KeyDown: - fmt.Println(e) + for _, event := range c.Events { + switch e := event.(type) { case *gg.KeyDown: switch e.Key { case gg.Key1 : if t.Connected() { - fmt.Println("disconnecting:", tri.Transform) t.Disconnect() } else { t.Connect(player) - fmt.Println("connecting:", tri.Transform) } - case gg.Key2 : - if t.Spawned { - return - } - fmt.Println("shit") - tt := *t - tt.Spawned = true - tt.Disconnect() - c.Spawn(&tt) - } - } + }}} } + diff --git a/context.go b/context.go index 926a687..0b4a3fc 100644 --- a/context.go +++ b/context.go @@ -11,8 +11,7 @@ const ( type Context struct { typ contextType - events []any + Events []any *Engine *Image - Event any } diff --git a/engine.go b/engine.go index 6320f93..0ddcb7e 100644 --- a/engine.go +++ b/engine.go @@ -139,13 +139,6 @@ func (e *Engine) Spawn(b Objecter) error { switch c.typ { case updateContext: b.Update(c) - case eventContext: - for _, event := range c.events { - b.Event(&Context{ - Engine: e, - Event: event, - }) - } } e.wg.Done() } @@ -215,12 +208,6 @@ func (e *engine) Update() error { e.dt = time.Since(e.lastTime).Seconds() // Buffering the context for faster. - c := &Context{Engine: eng, typ: updateContext} - for object := range e.Objects.KeyChan() { - e.wg.Add(1) - object.Input() <- c - } - e.wg.Wait() e.prevKeys = e.keys e.keys = inpututil. @@ -282,12 +269,16 @@ func (e *engine) Update() error { // Providing the events to the objects. // Maybe should think of the better way, // but for it is simple enough. - c = &Context{Engine: eng, typ: eventContext, events: events} + c := &Context{ + Engine: eng, + typ: updateContext, + Events: events, + } for object := range e.Objects.KeyChan() { e.wg.Add(1) object.Input() <- c } - e.wg.Wait() + e.wg.Wait() e.lastTime = time.Now() return nil diff --git a/object.go b/object.go index f1d6a84..c053b7a 100644 --- a/object.go +++ b/object.go @@ -16,12 +16,6 @@ type Updater interface { Update(*Context) } -// Implementing the interface lets the object -// to handle emited events. -type Eventer interface { - Event(*Context) -} - // Implementing the interface type // will call the function on deleting // the object. @@ -56,7 +50,6 @@ type Objecter interface { Input() chan *Context Starter Updater - Eventer Drawer Deleter } @@ -73,7 +66,6 @@ func (o *Object) Input() chan *Context { return o.input } func (o *Object) Start(c *Context) {} func (o *Object) Update(c *Context) {} -func (o *Object) Event(c *Context) {} func (o *Object) Draw(c *Context) {} func (o *Object) Delete(c *Context) {}