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) {
e := c.Engine
keyStrs := []string{}
keyStrs = append(keyStrs, fmt.Sprintf(
"counter: %d", counter,
))
keyStrs = append(keyStrs, fmt.Sprintf(
"tps: %d", int(c.TPS()),
))

View file

@ -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) {
}

View file

@ -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)),
)
}
}
func (t *Tri) Event(c *Context) {
switch e := c.Event.(type) {
case *gg.KeyDown:
fmt.Println(e)
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.KeyV:
t.Rotation += d * gg.Pi * 0.3 * dt
case gg.Key2 :
if t.Spawned {
return
break
}
fmt.Println("shit")
counter++
tt := *t
tt.Spawned = true
tt.Disconnect()
c.Spawn(&tt)
}}
for _, event := range c.Events {
switch e := event.(type) { case *gg.KeyDown:
switch e.Key {
case gg.Key1 :
if t.Connected() {
t.Disconnect()
} else {
t.Connect(player)
}
}
}}}
}

View file

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

View file

@ -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,7 +269,11 @@ 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

View file

@ -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) {}