feat: finished incapsulating the Transform.

This commit is contained in:
Andrey Parhomenko 2024-01-21 13:35:40 +03:00
parent b479bf1988
commit b7d7ab9bf9
6 changed files with 40 additions and 53 deletions

View file

@ -67,13 +67,13 @@ func main() {
e.Spawn(rect) e.Spawn(rect)
e.Spawn(tri) e.Spawn(tri)
circle := &gg.Circle{} /*circle := &gg.Circle{}
circle.Transform = gg.T() circle.Transform = gg.T()
circle.Rotation = 300 circle.Rotation = 300
circle.Color = gg.Rgba(1, 1, 1, 1) circle.Color = gg.Rgba(1, 1, 1, 1)
circle.Antialias = true circle.Antialias = true
circle.Layer = HighestL circle.Layer = HighestL
circle.Visible = true circle.Visible = true*/
//e.Spawn(circle) //e.Spawn(circle)
txt := &Text{} txt := &Text{}

View file

@ -48,7 +48,8 @@ func (p *Player) Draw(c *Context) []gg.EVertex {
} }
func (p *Player) Update(c *Context) { 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 { if p.Spawned {
return return
} }
@ -76,51 +77,45 @@ func (p *Player) Update(c *Context) {
case gg.KeyArrowRight: case gg.KeyArrowRight:
cam.Move(gg.X(p.MoveSpeed * dt)) cam.Move(gg.X(p.MoveSpeed * dt))
case gg.KeyW: case gg.KeyW:
p.Move(gg.Y(p.MoveSpeed * dt*10)) p.Move(gg.Y(-p.MoveSpeed * dt *10))
walking = true walking = true
p.Animate(Walk) p.Animate(Walk)
case gg.KeyA: case gg.KeyA:
p.Move(gg.X(-MoveSpeed * dt)) p.Move(gg.X(-p.MoveSpeed * dt))
p.SetScale(gg.V(-1, 1) p.SetScale(gg.V(-1, 1))
walking = true walking = true
p.Animate(Walk) p.Animate(Walk)
case gg.KeyS: case gg.KeyS:
p.Move(gg.X(-MoveSpeed * dt)) p.Move(gg.X(-p.MoveSpeed * dt))
p.Position.Y -= p.MoveSpeed * dt //p.Position.Y -= p.MoveSpeed * dt
walking = true walking = true
p.Animate(Walk) p.Animate(Walk)
case gg.KeyD: case gg.KeyD:
p.Position.X += p.MoveSpeed * dt p.Move(gg.X(p.MoveSpeed * dt))
p.Scale.X = 1 p.SetScale(gg.V(1, 1))
walking = true walking = true
p.Animate(Walk) p.Animate(Walk)
case gg.KeyR: case gg.KeyR:
cam.Rotation += gg.Pi * p.ScaleSpeed * dt cam.Rotate(gg.Pi * p.ScaleSpeed * dt)
case gg.KeyT: case gg.KeyT:
cam.Rotation -= gg.Pi * p.ScaleSpeed * dt cam.Rotate(-gg.Pi * p.ScaleSpeed * dt)
case gg.KeyRightBracket: case gg.KeyRightBracket:
if shift { if shift {
p.Rotation -= gg.Pi * 0.3 * dt p.Rotate(-gg.Pi * 0.3 * dt)
} else { } else {
p.Rotation += gg.Pi * 0.3 * dt p.Rotate(+gg.Pi * 0.3 * dt)
} }
case gg.KeyF: case gg.KeyF:
if shift { if shift {
cam.Scale = cam.Scale.Add(gg.V2(p.ScaleSpeed * dt)) cam.AddScale(gg.V2(p.ScaleSpeed * dt))
} else { } else {
cam.Scale = cam.Scale.Add(gg.V2(-p.ScaleSpeed * dt)) cam.AddScale(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
} }
case gg.KeyLeftBracket: case gg.KeyLeftBracket:
if shift { if shift {
rect.Rotation -= gg.Pi * 0.3 * dt rect.Rotate(-gg.Pi * 0.3 * dt)
} else { } else {
rect.Rotation += gg.Pi * 0.3 * dt rect.Rotate(+gg.Pi * 0.3 * dt)
} }
case gg.Key0: case gg.Key0:
c.Del(p) c.Del(p)
@ -153,10 +148,9 @@ func (p *Player) Update(c *Context) {
if !c.IsButtoned(gg.MouseButtonRight) { if !c.IsButtoned(gg.MouseButtonRight) {
break break
} }
pos := c.Camera.Position c.Camera.Move(ec.Abs)
c.Camera.Position = pos.Sub(ec.Abs)
case *gg.WheelChange: case *gg.WheelChange:
c.Camera.Scale = c.Camera.Scale.Add(gg.V2( c.Camera.AddScale(gg.V2(
ec.Offset.Y * dt * p.ScaleSpeed * 40, ec.Offset.Y * dt * p.ScaleSpeed * 40,
)) ))
}} }}
@ -178,7 +172,7 @@ func (p *Player) Resolve(c *Context) {
case gg.CollisionStaticPhysics : case gg.CollisionStaticPhysics :
LOOP: LOOP:
for { for {
p.Position.Y -= 1 p.Move(gg.Y(-1))
_, collides := gg.Collide(p, col.With) _, collides := gg.Collide(p, col.With)
if !collides { if !collides {
break LOOP break LOOP

View file

@ -8,7 +8,7 @@ type Rect struct {
func NewRect() *Rect { func NewRect() *Rect {
ret := &Rect{} ret := &Rect{}
ret.Scale = gg.V(200, 400) ret.SetScale(gg.V(200, 400))
ret.Color = gg.Color{ ret.Color = gg.Color{
gg.MaxColorV, gg.MaxColorV,
0, 0,

View file

@ -14,8 +14,7 @@ type Tri struct {
func NewTri() *Tri { func NewTri() *Tri {
ret := &Tri{} ret := &Tri{}
ret.Transform.Scale = gg.V2(1) ret.Transform = gg.T()
ret.Triangles = gg.Triangles{ ret.Triangles = gg.Triangles{
gg.Triangle{ gg.Triangle{
gg.V(0, 10), gg.V(0, 10),
@ -51,17 +50,17 @@ func (t *Tri) Update(c *Context) {
for _, key := range keys { switch key { for _, key := range keys { switch key {
case gg.KeyM: case gg.KeyM:
absPos := t.AbsPosition() absPos := t.Position()
t.SetAbsPosition( t.SetPosition(
absPos.Add(gg.V(0, 100*dt*d)), absPos.Add(gg.V(0, 100*dt*d)),
) )
case gg.KeyN: case gg.KeyN:
absPos := t.AbsPosition() absPos := t.Position()
t.SetAbsPosition( t.SetPosition(
absPos.Add(gg.V(100*dt*d, 0)), absPos.Add(gg.V(100*dt*d, 0)),
) )
case gg.KeyV: case gg.KeyV:
t.Rotation += d * gg.Pi * 0.3 * dt t.Rotate(d * gg.Pi * 0.3 * dt)
case gg.Key2 : case gg.Key2 :
if t.Spawned { if t.Spawned {
break break

View file

@ -21,23 +21,7 @@ var (
func Fragment(dst vec4, src vec2, _ vec4) vec4 { func Fragment(dst vec4, src vec2, _ vec4) vec4 {
c := imageSrc0UnsafeAt(src) c := imageSrc0UnsafeAt(src)
size := imageSrc0Size() return c * sin(dst.x)
//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
} }
`)) `))
) )

View file

@ -77,6 +77,11 @@ func (t *Transform) SetScale(scale Vector) {
t.scale = scale t.scale = scale
} }
func (t *Transform) AddScale(add ...Vector) {
t.dirty = true
t.scale = t.scale.Add(add...)
}
func (t *Transform) SetAround(around Vector) { func (t *Transform) SetAround(around Vector) {
t.dirty = true t.dirty = true
t.around = around t.around = around
@ -134,6 +139,11 @@ func (t *Transform) Rotation() Float {
} }
return t.rotation + t.parent.Rotation() return t.rotation + t.parent.Rotation()
} }
func (t *Transform) Rotate(add Float) {
t.rotation += add
t.dirty = true
}
func (t *Transform) Around() Vector { func (t *Transform) Around() Vector {
return t.around return t.around
} }