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

View file

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

View file

@ -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

View file

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

View file

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

View file

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

20
rect.go
View file

@ -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,
}

View file

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

View file

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