From b7d7ab9bf93c24a5769f90dba99a74ee5f51abe1 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 21 Jan 2024 13:35:40 +0300 Subject: [PATCH] feat: finished incapsulating the Transform. --- cmd/test/main.go | 4 ++-- cmd/test/player.go | 46 +++++++++++++++++++------------------------- cmd/test/rect.go | 2 +- cmd/test/trianlge.go | 13 ++++++------- shader.go | 18 +---------------- transform.go | 10 ++++++++++ 6 files changed, 40 insertions(+), 53 deletions(-) diff --git a/cmd/test/main.go b/cmd/test/main.go index ad215f2..7124366 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -67,13 +67,13 @@ func main() { e.Spawn(rect) e.Spawn(tri) - circle := &gg.Circle{} + /*circle := &gg.Circle{} circle.Transform = gg.T() circle.Rotation = 300 circle.Color = gg.Rgba(1, 1, 1, 1) circle.Antialias = true circle.Layer = HighestL - circle.Visible = true + circle.Visible = true*/ //e.Spawn(circle) txt := &Text{} diff --git a/cmd/test/player.go b/cmd/test/player.go index 9f95de2..3df9925 100644 --- a/cmd/test/player.go +++ b/cmd/test/player.go @@ -48,7 +48,8 @@ func (p *Player) Draw(c *Context) []gg.EVertex { } func (p *Player) Update(c *Context) { - p.SetPosition(p.Position().Sub(gg.Y(2))) + //p.SetPosition(p.Position().Sub(gg.Y(2))) + p.Move(gg.Y(1)) if p.Spawned { return } @@ -76,51 +77,45 @@ func (p *Player) Update(c *Context) { case gg.KeyArrowRight: cam.Move(gg.X(p.MoveSpeed * dt)) case gg.KeyW: - p.Move(gg.Y(p.MoveSpeed * dt*10)) + p.Move(gg.Y(-p.MoveSpeed * dt *10)) walking = true p.Animate(Walk) case gg.KeyA: - p.Move(gg.X(-MoveSpeed * dt)) - p.SetScale(gg.V(-1, 1) + p.Move(gg.X(-p.MoveSpeed * dt)) + p.SetScale(gg.V(-1, 1)) walking = true p.Animate(Walk) case gg.KeyS: - p.Move(gg.X(-MoveSpeed * dt)) - p.Position.Y -= p.MoveSpeed * dt + p.Move(gg.X(-p.MoveSpeed * dt)) + //p.Position.Y -= p.MoveSpeed * dt walking = true p.Animate(Walk) case gg.KeyD: - p.Position.X += p.MoveSpeed * dt - p.Scale.X = 1 + p.Move(gg.X(p.MoveSpeed * dt)) + p.SetScale(gg.V(1, 1)) walking = true p.Animate(Walk) case gg.KeyR: - cam.Rotation += gg.Pi * p.ScaleSpeed * dt + cam.Rotate(gg.Pi * p.ScaleSpeed * dt) case gg.KeyT: - cam.Rotation -= gg.Pi * p.ScaleSpeed * dt + cam.Rotate(-gg.Pi * p.ScaleSpeed * dt) case gg.KeyRightBracket: if shift { - p.Rotation -= gg.Pi * 0.3 * dt + p.Rotate(-gg.Pi * 0.3 * dt) } else { - p.Rotation += gg.Pi * 0.3 * dt + p.Rotate(+gg.Pi * 0.3 * dt) } case gg.KeyF: if shift { - cam.Scale = cam.Scale.Add(gg.V2(p.ScaleSpeed * dt)) + cam.AddScale(gg.V2(p.ScaleSpeed * dt)) } else { - cam.Scale = cam.Scale.Add(gg.V2(-p.ScaleSpeed * dt)) - } - case gg.KeyG: - if shift { - cam.Scale.Y -= gg.Pi * p.ScaleSpeed * dt - } else { - cam.Scale.Y += gg.Pi * p.ScaleSpeed * dt + cam.AddScale(gg.V2(-p.ScaleSpeed * dt)) } case gg.KeyLeftBracket: if shift { - rect.Rotation -= gg.Pi * 0.3 * dt + rect.Rotate(-gg.Pi * 0.3 * dt) } else { - rect.Rotation += gg.Pi * 0.3 * dt + rect.Rotate(+gg.Pi * 0.3 * dt) } case gg.Key0: c.Del(p) @@ -153,10 +148,9 @@ func (p *Player) Update(c *Context) { if !c.IsButtoned(gg.MouseButtonRight) { break } - pos := c.Camera.Position - c.Camera.Position = pos.Sub(ec.Abs) + c.Camera.Move(ec.Abs) case *gg.WheelChange: - c.Camera.Scale = c.Camera.Scale.Add(gg.V2( + c.Camera.AddScale(gg.V2( ec.Offset.Y * dt * p.ScaleSpeed * 40, )) }} @@ -178,7 +172,7 @@ func (p *Player) Resolve(c *Context) { case gg.CollisionStaticPhysics : LOOP: for { - p.Position.Y -= 1 + p.Move(gg.Y(-1)) _, collides := gg.Collide(p, col.With) if !collides { break LOOP diff --git a/cmd/test/rect.go b/cmd/test/rect.go index b4e1542..8809b16 100644 --- a/cmd/test/rect.go +++ b/cmd/test/rect.go @@ -8,7 +8,7 @@ type Rect struct { func NewRect() *Rect { ret := &Rect{} - ret.Scale = gg.V(200, 400) + ret.SetScale(gg.V(200, 400)) ret.Color = gg.Color{ gg.MaxColorV, 0, diff --git a/cmd/test/trianlge.go b/cmd/test/trianlge.go index f6f57f6..1b3f9bb 100644 --- a/cmd/test/trianlge.go +++ b/cmd/test/trianlge.go @@ -14,8 +14,7 @@ type Tri struct { func NewTri() *Tri { ret := &Tri{} - ret.Transform.Scale = gg.V2(1) - + ret.Transform = gg.T() ret.Triangles = gg.Triangles{ gg.Triangle{ gg.V(0, 10), @@ -51,17 +50,17 @@ func (t *Tri) Update(c *Context) { for _, key := range keys { switch key { case gg.KeyM: - absPos := t.AbsPosition() - t.SetAbsPosition( + absPos := t.Position() + t.SetPosition( absPos.Add(gg.V(0, 100*dt*d)), ) case gg.KeyN: - absPos := t.AbsPosition() - t.SetAbsPosition( + absPos := t.Position() + t.SetPosition( absPos.Add(gg.V(100*dt*d, 0)), ) case gg.KeyV: - t.Rotation += d * gg.Pi * 0.3 * dt + t.Rotate(d * gg.Pi * 0.3 * dt) case gg.Key2 : if t.Spawned { break diff --git a/shader.go b/shader.go index 8834cf2..f1fcacb 100644 --- a/shader.go +++ b/shader.go @@ -21,23 +21,7 @@ var ( func Fragment(dst vec4, src vec2, _ vec4) vec4 { c := imageSrc0UnsafeAt(src) - size := imageSrc0Size() - //ret := vec4(0.1) * src.x/size.x - //ret := c * src.x/size.x - //ret.r = 1 - //ret.a = c.a - //c.a = 0 - //c.rg = vec2(0) - //c.a = c.b - //ret := vec4(1, 1, 1, 0) - //ret.a /= 5 - //ret.a = src.x - //c.a /= 5 - //c = vec4(c.a) - orig := imageSrc0Origin() - src -= orig - c.a *= (size.x-src.x)/size.x - return c + return c * sin(dst.x) } `)) ) diff --git a/transform.go b/transform.go index b20f947..54c43d2 100644 --- a/transform.go +++ b/transform.go @@ -77,6 +77,11 @@ func (t *Transform) SetScale(scale Vector) { t.scale = scale } +func (t *Transform) AddScale(add ...Vector) { + t.dirty = true + t.scale = t.scale.Add(add...) +} + func (t *Transform) SetAround(around Vector) { t.dirty = true t.around = around @@ -134,6 +139,11 @@ func (t *Transform) Rotation() Float { } return t.rotation + t.parent.Rotation() } + +func (t *Transform) Rotate(add Float) { + t.rotation += add + t.dirty = true +} func (t *Transform) Around() Vector { return t.around }