This commit is contained in:
Andrey Parhomenko 2024-01-08 07:12:35 +03:00
parent aa4bcc3f8d
commit f4bf94a0e6
6 changed files with 51 additions and 67 deletions

View file

@ -14,6 +14,9 @@ type Debug struct {
func (d *Debug) Draw(c *Context) { func (d *Debug) Draw(c *Context) {
e := c.Engine e := c.Engine
keyStrs := []string{} keyStrs := []string{}
keyStrs = append(keyStrs, fmt.Sprintf(
"counter: %d", counter,
))
keyStrs = append(keyStrs, fmt.Sprintf( keyStrs = append(keyStrs, fmt.Sprintf(
"tps: %d", int(c.TPS()), "tps: %d", int(c.TPS()),
)) ))

View file

@ -99,12 +99,6 @@ func (p *Player) Update(c *Context) {
} else { } else {
cam.Scale.Y += gg.Pi * p.ScaleSpeed * dt 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: case gg.KeyLeftBracket:
if shift { if shift {
rect.Rotation -= gg.Pi * 0.3 * dt rect.Rotation -= gg.Pi * 0.3 * dt
@ -117,10 +111,8 @@ func (p *Player) Update(c *Context) {
} }
} }
} for _, event := range c.Events {
switch ec := event.(type) {
func (p *Player) Event(c *gg.Context) {
switch ec := c.Event.(type) {
case *gg.KeyDown: case *gg.KeyDown:
switch { switch {
case ec.Key == gg.KeyB: 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( c.Camera.Scale = c.Camera.Scale.Add(gg.V2(
ec.Offset.Y * c.DT() * p.ScaleSpeed * 40, ec.Offset.Y * c.DT() * p.ScaleSpeed * 40,
)) ))
} }}
}
func (p *Player) Event(c *gg.Context) {
} }

View file

@ -1,7 +1,11 @@
package main package main
import "vultras.su/core/gg" import "vultras.su/core/gg"
import "fmt" //import "fmt"
var (
counter int
)
type Tri struct { type Tri struct {
gg.DrawablePolygon gg.DrawablePolygon
@ -28,56 +32,56 @@ func NewTri() *Tri {
} }
func (t *Tri) Update(c *Context) { func (t *Tri) Update(c *Context) {
if t.Spawned {
return
}
dt := c.DT()
if t.ContainsPoint(c.AbsCursorPosition()) { if t.ContainsPoint(c.AbsCursorPosition()) {
t.Color = gg.Rgba(0, 1, 0, 1) t.Color = gg.Rgba(0, 1, 0, 1)
} else { } else {
t.Color = gg.Rgba(1, 0, 1, 1) t.Color = gg.Rgba(1, 0, 1, 1)
} }
/*if t.Spawned { keys := c.Keys()
return
}*/
d := +1. d := +1.
if c.IsPressed(gg.KeyShift) { if c.IsPressed(gg.KeyShift) {
d = -1. d = -1
} }
if c.IsPressed(gg.KeyM) {
absPos := tri.AbsPosition() for _, key := range keys { switch key {
tri.SetAbsPosition( case gg.KeyM:
absPos := t.AbsPosition()
t.SetAbsPosition(
absPos.Add(gg.V(0, 100*c.DT()*d)), absPos.Add(gg.V(0, 100*c.DT()*d)),
) )
} case gg.KeyN:
if c.IsPressed(gg.KeyN) { absPos := t.AbsPosition()
absPos := tri.AbsPosition() t.SetAbsPosition(
tri.SetAbsPosition(
absPos.Add(gg.V(100*c.DT()*d, 0)), 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) { for _, event := range c.Events {
switch e := c.Event.(type) { switch e := event.(type) { case *gg.KeyDown:
case *gg.KeyDown:
fmt.Println(e)
switch e.Key { switch e.Key {
case gg.Key1 : case gg.Key1 :
if t.Connected() { if t.Connected() {
fmt.Println("disconnecting:", tri.Transform)
t.Disconnect() t.Disconnect()
} else { } else {
t.Connect(player) 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)
}
}
} }

View file

@ -11,8 +11,7 @@ const (
type Context struct { type Context struct {
typ contextType typ contextType
events []any Events []any
*Engine *Engine
*Image *Image
Event any
} }

View file

@ -139,13 +139,6 @@ func (e *Engine) Spawn(b Objecter) error {
switch c.typ { switch c.typ {
case updateContext: case updateContext:
b.Update(c) b.Update(c)
case eventContext:
for _, event := range c.events {
b.Event(&Context{
Engine: e,
Event: event,
})
}
} }
e.wg.Done() e.wg.Done()
} }
@ -215,12 +208,6 @@ func (e *engine) Update() error {
e.dt = time.Since(e.lastTime).Seconds() e.dt = time.Since(e.lastTime).Seconds()
// Buffering the context for faster. // 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.prevKeys = e.keys
e.keys = inpututil. e.keys = inpututil.
@ -282,12 +269,16 @@ func (e *engine) Update() error {
// Providing the events to the objects. // Providing the events to the objects.
// Maybe should think of the better way, // Maybe should think of the better way,
// but for it is simple enough. // 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() { for object := range e.Objects.KeyChan() {
e.wg.Add(1) e.wg.Add(1)
object.Input() <- c object.Input() <- c
} }
e.wg.Wait() e.wg.Wait()
e.lastTime = time.Now() e.lastTime = time.Now()
return nil return nil

View file

@ -16,12 +16,6 @@ type Updater interface {
Update(*Context) Update(*Context)
} }
// Implementing the interface lets the object
// to handle emited events.
type Eventer interface {
Event(*Context)
}
// Implementing the interface type // Implementing the interface type
// will call the function on deleting // will call the function on deleting
// the object. // the object.
@ -56,7 +50,6 @@ type Objecter interface {
Input() chan *Context Input() chan *Context
Starter Starter
Updater Updater
Eventer
Drawer Drawer
Deleter Deleter
} }
@ -73,7 +66,6 @@ func (o *Object) Input() chan *Context { return o.input }
func (o *Object) Start(c *Context) {} func (o *Object) Start(c *Context) {}
func (o *Object) Update(c *Context) {} func (o *Object) Update(c *Context) {}
func (o *Object) Event(c *Context) {}
func (o *Object) Draw(c *Context) {} func (o *Object) Draw(c *Context) {}
func (o *Object) Delete(c *Context) {} func (o *Object) Delete(c *Context) {}