feat: implemented the basic parenting for objects.

This commit is contained in:
Andrey Parhomenko 2023-12-26 23:31:04 +03:00
parent e66882f9d5
commit 373086a709
9 changed files with 83 additions and 34 deletions

View file

@ -15,6 +15,18 @@ 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(
"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{} keys := []string{}
for _, k := range e.Keys() { for _, k := range e.Keys() {
@ -30,7 +42,6 @@ func (d *Debug) Draw(c *Context) {
keyStrs = append(keyStrs, fmt.Sprintf( keyStrs = append(keyStrs, fmt.Sprintf(
"wheel: %v", c.Wheel(), "wheel: %v", c.Wheel(),
)) ))
/*if rectMove.ContainsPoint(e.AbsCursorPosition()) { /*if rectMove.ContainsPoint(e.AbsCursorPosition()) {
keyStrs = append(keyStrs, "contains cursor") keyStrs = append(keyStrs, "contains cursor")
} }

View file

@ -42,8 +42,8 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
player = NewPlayer()
rect = NewRect() rect = NewRect()
player = NewPlayer()
tri = NewTri() tri = NewTri()
e.Add(&Debug{}) e.Add(&Debug{})

View file

@ -2,7 +2,7 @@ package main
import ( import (
//"math/rand" //"math/rand"
//"fmt" "fmt"
) )
import "github.com/di4f/gg" import "github.com/di4f/gg"
@ -17,9 +17,11 @@ type Player struct {
func NewPlayer() *Player { func NewPlayer() *Player {
ret := &Player{} ret := &Player{}
ret.Transform = gg.T() ret.Transform = gg.T()
fmt.Println("transform:", ret.Transform)
//ret.Parent = rect
ret.Scale = gg.V(5, 5) ret.Scale = gg.V(5, 5)
// Around center. // Around center.
ret.Around = gg.V(.5, .5) ret.Around = gg.V2(.5)
ret.MoveSpeed = 90. ret.MoveSpeed = 90.
ret.ScaleSpeed = .2 ret.ScaleSpeed = .2

View file

@ -4,13 +4,13 @@ import "github.com/di4f/gg"
//import "fmt" //import "fmt"
type Tri struct { type Tri struct {
*gg.DrawablePolygon gg.DrawablePolygon
gg.Layer gg.Layer
} }
func NewTri() *Tri { func NewTri() *Tri {
ret := &Tri{} ret := &Tri{}
ret.DrawablePolygon = &gg.DrawablePolygon{} ret.Parent = player
ret.Transform.Scale = gg.V2(1) ret.Transform.Scale = gg.V2(1)
ret.Triangles = gg.Triangles{ ret.Triangles = gg.Triangles{
@ -38,4 +38,21 @@ func (t *Tri) Update(c *Context) {
} else { } else {
t.Color = gg.Rgba(1, 0, 1, 1) 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)),
)
}
} }

View file

@ -358,6 +358,13 @@ func (e *engine) Layout(ow, oh int) (int, int) {
func (e *Engine) DT() Float { func (e *Engine) DT() Float {
return e.dt return e.dt
} }
func (e *Engine) FPS() float64 {
return ebiten.ActualFPS()
}
func (e *Engine) TPS() float64 {
return ebiten.ActualTPS()
}
func (e *Engine) Run() error { func (e *Engine) Run() error {
ebiten.ReadDebugInfo(&e.wcfg.DebugInfo) ebiten.ReadDebugInfo(&e.wcfg.DebugInfo)

View file

@ -3,10 +3,6 @@ package gg
import ( import (
) )
type PolygonTriangle struct {
T, S int
}
// Grouped triangles type. // Grouped triangles type.
type Polygon struct { type Polygon struct {
Transform Transform
@ -27,9 +23,7 @@ type DrawablePolygon struct {
} }
func (p *Polygon) MakeTriangles() Triangles { func (p *Polygon) MakeTriangles() Triangles {
mv := p.Matrix() m := p.Matrix()
m := &mv
ret := make(Triangles, len(p.Triangles)) ret := make(Triangles, len(p.Triangles))
for i, t := range p.Triangles { for i, t := range p.Triangles {
ret[i] = Triangle{ ret[i] = Triangle{

20
rect.go
View file

@ -29,10 +29,10 @@ type DrawableRectangle struct {
// Return points of vertices of the rectangle. // Return points of vertices of the rectangle.
func (r Rectangle) Vertices() Points { func (r Rectangle) Vertices() Points {
m := r.Matrix() m := r.Matrix()
p1 := V(0, 0).Apply(&m) p1 := V(0, 0).Apply(m)
p2 := V(1, 0).Apply(&m) p2 := V(1, 0).Apply(m)
p3 := V(1, 1).Apply(&m) p3 := V(1, 1).Apply(m)
p4 := V(0, 1).Apply(&m) p4 := V(0, 1).Apply(m)
return Points{p1, p2, p3, p4} return Points{p1, p2, p3, p4}
} }
@ -81,7 +81,7 @@ func (r *DrawableRectangle) Draw(c *Context) {
opts := &ebiten.DrawImageOptions{ opts := &ebiten.DrawImageOptions{
GeoM: m, GeoM: *m,
} }
c.DrawImage(img, opts) c.DrawImage(img, opts)
return return
@ -93,21 +93,13 @@ func (r *DrawableRectangle) Draw(c *Context) {
if r.Images[0] == nil { if r.Images[0] == nil {
r.Images[0] = NewImage(1, 1) r.Images[0] = NewImage(1, 1)
r.Images[0].Set(0, 0, r.Color) r.Images[0].Set(0, 0, r.Color)
//did = true
} }
w, h := r.Images[0].Size() 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. // Drawing with shader.
opts := &ebiten.DrawRectShaderOptions{ opts := &ebiten.DrawRectShaderOptions{
GeoM: m, GeoM: *m,
Images: r.Images, Images: r.Images,
Uniforms: r.Uniforms, Uniforms: r.Uniforms,
} }

View file

@ -20,7 +20,8 @@ func (s *Sprite) Draw(c *Context) {
t := s.Rectangle().Transform t := s.Rectangle().Transform
m := &Matrix{} m := &Matrix{}
m.Concat(t.Matrix()) tm := t.Matrix()
m.Concat(*tm)
if !s.Floating { if !s.Floating {
m.Concat(c.Camera.RealMatrix()) m.Concat(c.Camera.RealMatrix())
} }

View file

@ -5,6 +5,10 @@ import (
//"math" //"math"
) )
type Transformer interface {
GetTransform() *Transform
}
// The structure represents basic transformation // The structure represents basic transformation
// features: positioning, rotating and scaling. // features: positioning, rotating and scaling.
type Transform struct { type Transform struct {
@ -16,10 +20,13 @@ type Transform struct {
// The not scaled offset vector from upper left corner // The not scaled offset vector from upper left corner
// which the object should be rotated around. // which the object should be rotated around.
Around Vector Around Vector
// Needs to be implemented. // If is not nil then the upper values will be relational to
// Makes transform depending on the other one. // the parent ones.
// Is the root one if Parent == nil Parent Transformer
Parent *Transform }
func (t *Transform) GetTransform() *Transform {
return t
} }
// Returns the default Transform structure. // Returns the default Transform structure.
@ -47,9 +54,22 @@ func (t Transform) ScaledToY(y Float) Transform {
return t 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 // Returns the GeoM with corresponding
// to the transfrom transformation. // to the transfrom transformation.
func (t Transform)Matrix() Matrix { func (t *Transform)Matrix() *Matrix {
g := &Matrix{} g := &Matrix{}
// Scale first. // Scale first.
@ -65,6 +85,11 @@ func (t Transform)Matrix() Matrix {
// And finally move to the absolute position. // And finally move to the absolute position.
g.Translate(t.Position.X, t.Position.Y) g.Translate(t.Position.X, t.Position.Y)
return *g if t.Parent != nil {
m := t.Parent.GetTransform().Matrix()
g.Concat(*m)
}
return g
} }