From 373086a709ea096e215cd063436bdc6200587cb1 Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 26 Dec 2023 23:31:04 +0300 Subject: [PATCH] feat: implemented the basic parenting for objects. --- cmd/test/debug.go | 13 ++++++++++++- cmd/test/main.go | 2 +- cmd/test/player.go | 6 ++++-- cmd/test/trianlge.go | 21 +++++++++++++++++++-- engine.go | 7 +++++++ polygon.go | 8 +------- rect.go | 20 ++++++-------------- sprite.go | 3 ++- transform.go | 37 +++++++++++++++++++++++++++++++------ 9 files changed, 83 insertions(+), 34 deletions(-) diff --git a/cmd/test/debug.go b/cmd/test/debug.go index 5425657..4dff5f0 100644 --- a/cmd/test/debug.go +++ b/cmd/test/debug.go @@ -15,6 +15,18 @@ type Debug struct { func (d *Debug) Draw(c *Context) { e := c.Engine keyStrs := []string{} + keyStrs = append(keyStrs, fmt.Sprintf( + "tps: %d", int(c.TPS()), + )) + keyStrs = append(keyStrs, fmt.Sprintf( + "fps: %d", int(c.FPS()), + )) + keyStrs = append(keyStrs, fmt.Sprintf( + "absPlayerPos: %v", player.Position, + )) + keyStrs = append(keyStrs, fmt.Sprintf( + "absTriPos: %v", tri.AbsPosition(), + )) keys := []string{} for _, k := range e.Keys() { @@ -30,7 +42,6 @@ func (d *Debug) Draw(c *Context) { keyStrs = append(keyStrs, fmt.Sprintf( "wheel: %v", c.Wheel(), )) - /*if rectMove.ContainsPoint(e.AbsCursorPosition()) { keyStrs = append(keyStrs, "contains cursor") } diff --git a/cmd/test/main.go b/cmd/test/main.go index 52aac05..f51f86e 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -42,8 +42,8 @@ func main() { log.Fatal(err) } - player = NewPlayer() rect = NewRect() + player = NewPlayer() tri = NewTri() e.Add(&Debug{}) diff --git a/cmd/test/player.go b/cmd/test/player.go index 1f7f5b9..4044671 100644 --- a/cmd/test/player.go +++ b/cmd/test/player.go @@ -2,7 +2,7 @@ package main import ( //"math/rand" - //"fmt" + "fmt" ) import "github.com/di4f/gg" @@ -17,9 +17,11 @@ type Player struct { func NewPlayer() *Player { ret := &Player{} ret.Transform = gg.T() + fmt.Println("transform:", ret.Transform) + //ret.Parent = rect ret.Scale = gg.V(5, 5) // Around center. - ret.Around = gg.V(.5, .5) + ret.Around = gg.V2(.5) ret.MoveSpeed = 90. ret.ScaleSpeed = .2 diff --git a/cmd/test/trianlge.go b/cmd/test/trianlge.go index 7a1e459..09b82cd 100644 --- a/cmd/test/trianlge.go +++ b/cmd/test/trianlge.go @@ -4,13 +4,13 @@ import "github.com/di4f/gg" //import "fmt" type Tri struct { - *gg.DrawablePolygon + gg.DrawablePolygon gg.Layer } func NewTri() *Tri { ret := &Tri{} - ret.DrawablePolygon = &gg.DrawablePolygon{} + ret.Parent = player ret.Transform.Scale = gg.V2(1) ret.Triangles = gg.Triangles{ @@ -38,4 +38,21 @@ func (t *Tri) Update(c *Context) { } else { t.Color = gg.Rgba(1, 0, 1, 1) } + + d := +1. + if c.IsPressed(gg.KeyShift) { + d = -1. + } + if c.IsPressed(gg.KeyM) { + absPos := tri.AbsPosition() + tri.SetAbsPosition( + absPos.Add(gg.V(0, 100 * c.DT() * d)), + ) + } + if c.IsPressed(gg.KeyN) { + absPos := tri.AbsPosition() + tri.SetAbsPosition( + absPos.Add(gg.V(100 * c.DT() * d, 0)), + ) + } } diff --git a/engine.go b/engine.go index d268365..5dd0389 100644 --- a/engine.go +++ b/engine.go @@ -358,6 +358,13 @@ func (e *engine) Layout(ow, oh int) (int, int) { func (e *Engine) DT() Float { return e.dt } +func (e *Engine) FPS() float64 { + return ebiten.ActualFPS() +} + +func (e *Engine) TPS() float64 { + return ebiten.ActualTPS() +} func (e *Engine) Run() error { ebiten.ReadDebugInfo(&e.wcfg.DebugInfo) diff --git a/polygon.go b/polygon.go index 0734b87..d10a0e9 100644 --- a/polygon.go +++ b/polygon.go @@ -3,10 +3,6 @@ package gg import ( ) -type PolygonTriangle struct { - T, S int -} - // Grouped triangles type. type Polygon struct { Transform @@ -27,9 +23,7 @@ type DrawablePolygon struct { } func (p *Polygon) MakeTriangles() Triangles { - mv := p.Matrix() - m := &mv - + m := p.Matrix() ret := make(Triangles, len(p.Triangles)) for i, t := range p.Triangles { ret[i] = Triangle{ diff --git a/rect.go b/rect.go index 29f4726..06dd36e 100644 --- a/rect.go +++ b/rect.go @@ -29,10 +29,10 @@ type DrawableRectangle struct { // Return points of vertices of the rectangle. func (r Rectangle) Vertices() Points { m := r.Matrix() - p1 := V(0, 0).Apply(&m) - p2 := V(1, 0).Apply(&m) - p3 := V(1, 1).Apply(&m) - p4 := V(0, 1).Apply(&m) + p1 := V(0, 0).Apply(m) + p2 := V(1, 0).Apply(m) + p3 := V(1, 1).Apply(m) + p4 := V(0, 1).Apply(m) return Points{p1, p2, p3, p4} } @@ -81,7 +81,7 @@ func (r *DrawableRectangle) Draw(c *Context) { opts := &ebiten.DrawImageOptions{ - GeoM: m, + GeoM: *m, } c.DrawImage(img, opts) return @@ -93,21 +93,13 @@ func (r *DrawableRectangle) Draw(c *Context) { if r.Images[0] == nil { r.Images[0] = NewImage(1, 1) r.Images[0].Set(0, 0, r.Color) - //did = true } w, h := r.Images[0].Size() - /*if !did { - t.S.X /= Float(w) - t.S.Y /= Float(h) - - t.S.X *= r.W - t.S.Y *= r.H - }*/ // Drawing with shader. opts := &ebiten.DrawRectShaderOptions{ - GeoM: m, + GeoM: *m, Images: r.Images, Uniforms: r.Uniforms, } diff --git a/sprite.go b/sprite.go index 714bbb0..c91c078 100644 --- a/sprite.go +++ b/sprite.go @@ -20,7 +20,8 @@ func (s *Sprite) Draw(c *Context) { t := s.Rectangle().Transform m := &Matrix{} - m.Concat(t.Matrix()) + tm := t.Matrix() + m.Concat(*tm) if !s.Floating { m.Concat(c.Camera.RealMatrix()) } diff --git a/transform.go b/transform.go index abb26f3..c0478b2 100644 --- a/transform.go +++ b/transform.go @@ -5,6 +5,10 @@ import ( //"math" ) +type Transformer interface { + GetTransform() *Transform +} + // The structure represents basic transformation // features: positioning, rotating and scaling. type Transform struct { @@ -16,10 +20,13 @@ type Transform struct { // The not scaled offset vector from upper left corner // which the object should be rotated around. Around Vector - // Needs to be implemented. - // Makes transform depending on the other one. - // Is the root one if Parent == nil - Parent *Transform + // If is not nil then the upper values will be relational to + // the parent ones. + Parent Transformer +} + +func (t *Transform) GetTransform() *Transform { + return t } // Returns the default Transform structure. @@ -47,9 +54,22 @@ func (t Transform) ScaledToY(y Float) Transform { return t } +func (t *Transform) SetAbsPosition(absPosition Vector) { + m := t.Matrix() + m.Invert() + t.Position = absPosition.Apply(m) +} + +func (t *Transform) AbsPosition() Vector { + return t.Position.Apply(t.Matrix()) +} + +func (t *Transform) Connect(p Transformer) { +} + // Returns the GeoM with corresponding // to the transfrom transformation. -func (t Transform)Matrix() Matrix { +func (t *Transform)Matrix() *Matrix { g := &Matrix{} // Scale first. @@ -65,6 +85,11 @@ func (t Transform)Matrix() Matrix { // And finally move to the absolute position. g.Translate(t.Position.X, t.Position.Y) - return *g + if t.Parent != nil { + m := t.Parent.GetTransform().Matrix() + g.Concat(*m) + } + + return g }