Use embedding for Transform instead of the T name.

This commit is contained in:
Andrey Parhomenko 2023-05-30 13:24:14 +03:00
parent ab32a830e6
commit 308123f3d2
5 changed files with 42 additions and 48 deletions

View file

@ -26,9 +26,9 @@ type Rect struct {
func NewRect() *Rect { func NewRect() *Rect {
return &Rect{&gx.DrawableRectangle{ return &Rect{&gx.DrawableRectangle{
Rectangle: gx.Rectangle{ Rectangle: gx.Rectangle{
Transform: gx.T(),
W: 200, W: 200,
H: 400, H: 400,
T: gx.T(),
}, },
Color: gx.Color{ Color: gx.Color{
gx.MaxColorV, gx.MaxColorV,
@ -60,14 +60,16 @@ var (
func NewPlayer() *Player { func NewPlayer() *Player {
ret := &Player{ ret := &Player{
Sprite: &gx.Sprite{ Sprite: &gx.Sprite{
T: gx.Transform { Transform: gx.Transform {
S: gx.Vector{5, 5}, S: gx.Vector{5, 5},
RA: gx.Vector{320, 240}, RA: gx.Vector{320, 240},
}, },
Visible: true, Visible: true,
ShaderOptions: gx.ShaderOptions {
Shader: gx.SolidWhiteColorShader, Shader: gx.SolidWhiteColorShader,
Uniforms: make(map[string] any), Uniforms: make(map[string] any),
}, },
},
MoveSpeed: 90., MoveSpeed: 90.,
ScaleSpeed: .2, ScaleSpeed: .2,
} }
@ -80,7 +82,7 @@ func NewPlayer() *Player {
func (p *Player) Start(e *gx.Engine, v ...any) { func (p *Player) Start(e *gx.Engine, v ...any) {
fmt.Println("starting") fmt.Println("starting")
c := e.Camera() c := e.Camera()
c.T.RA = gx.V(360, -240) c.RA = gx.V(360, -240)
} }
func (p *Player) Update(e *gx.Engine) error { func (p *Player) Update(e *gx.Engine) error {
@ -91,51 +93,51 @@ func (p *Player) Update(e *gx.Engine) error {
p.Uniforms["Random"] = any(rand.Float32()) p.Uniforms["Random"] = any(rand.Float32())
for _, v := range keys {switch v { for _, v := range keys {switch v {
case ebiten.KeyArrowUp : case ebiten.KeyArrowUp :
c.T.P.Y += p.MoveSpeed * dt c.P.Y += p.MoveSpeed * dt
case ebiten.KeyArrowLeft : case ebiten.KeyArrowLeft :
c.T.P.X -= p.MoveSpeed * dt c.P.X -= p.MoveSpeed * dt
case ebiten.KeyArrowDown : case ebiten.KeyArrowDown :
c.T.P.Y -= p.MoveSpeed * dt c.P.Y -= p.MoveSpeed * dt
case ebiten.KeyArrowRight : case ebiten.KeyArrowRight :
c.T.P.X += p.MoveSpeed * dt c.P.X += p.MoveSpeed * dt
case ebiten.KeyW : case ebiten.KeyW :
p.T.P.Y += p.MoveSpeed * dt p.P.Y += p.MoveSpeed * dt
case ebiten.KeyA : case ebiten.KeyA :
p.T.P.X -= p.MoveSpeed * dt p.P.X -= p.MoveSpeed * dt
case ebiten.KeyS : case ebiten.KeyS :
p.T.P.Y -= p.MoveSpeed * dt p.P.Y -= p.MoveSpeed * dt
case ebiten.KeyD : case ebiten.KeyD :
p.T.P.X += p.MoveSpeed * dt p.P.X += p.MoveSpeed * dt
case ebiten.KeyR : case ebiten.KeyR :
c.T.R += gx.Pi * p.ScaleSpeed * dt c.R += gx.Pi * p.ScaleSpeed * dt
case ebiten.KeyT : case ebiten.KeyT :
c.T.R -= gx.Pi * p.ScaleSpeed * dt c.R -= gx.Pi * p.ScaleSpeed * dt
case ebiten.KeyF : case ebiten.KeyF :
if e.KeyIsPressed(ebiten.KeyShift) { if e.KeyIsPressed(ebiten.KeyShift) {
c.T.S.X -= gx.Pi * p.ScaleSpeed * dt c.S.X -= gx.Pi * p.ScaleSpeed * dt
} else { } else {
c.T.S.X += gx.Pi * p.ScaleSpeed * dt c.S.X += gx.Pi * p.ScaleSpeed * dt
} }
case ebiten.KeyG : case ebiten.KeyG :
if e.KeyIsPressed(ebiten.KeyShift) { if e.KeyIsPressed(ebiten.KeyShift) {
c.T.S.Y -= gx.Pi * p.ScaleSpeed * dt c.S.Y -= gx.Pi * p.ScaleSpeed * dt
} else { } else {
c.T.S.Y += gx.Pi * p.ScaleSpeed * dt c.S.Y += gx.Pi * p.ScaleSpeed * dt
} }
case ebiten.KeyZ : case ebiten.KeyZ :
if e.KeyIsPressed(ebiten.KeyShift) { if e.KeyIsPressed(ebiten.KeyShift) {
c.T.RA.X -= gx.Pi * p.MoveSpeed * dt c.RA.X -= gx.Pi * p.MoveSpeed * dt
} else { } else {
c.T.RA.X += gx.Pi * p.MoveSpeed * dt c.RA.X += gx.Pi * p.MoveSpeed * dt
} }
log.Println(c.T.RA.X) log.Println(c.RA.X)
case ebiten.KeyX : case ebiten.KeyX :
if e.KeyIsPressed(ebiten.KeyShift) { if e.KeyIsPressed(ebiten.KeyShift) {
c.T.RA.Y -= gx.Pi * p.MoveSpeed * dt c.RA.Y -= gx.Pi * p.MoveSpeed * dt
} else { } else {
c.T.RA.Y += gx.Pi * p.MoveSpeed * dt c.RA.Y += gx.Pi * p.MoveSpeed * dt
} }
log.Println(c.T.RA.Y) log.Println(c.RA.Y)
case ebiten.Key0 : case ebiten.Key0 :
e.Del(p) e.Del(p)
}} }}

View file

@ -3,7 +3,7 @@ package gx
// Implements the camera component // Implements the camera component
// for the main window. // for the main window.
type Camera struct { type Camera struct {
T Transform Transform
} }
// Returns the matrix satysfying camera // Returns the matrix satysfying camera
@ -21,20 +21,20 @@ func (c *Camera)RealMatrix(
if scale { if scale {
g.Scale( g.Scale(
c.T.S.X, c.S.X,
c.T.S.Y, c.S.Y,
) )
} }
g.Translate( g.Translate(
-c.T.P.X, -c.P.X,
c.T.P.Y, c.P.Y,
) )
g.Rotate(-c.T.R) g.Rotate(-c.R)
g.Translate( g.Translate(
c.T.RA.X, c.RA.X,
-c.T.RA.Y, -c.RA.Y,
) )
return *g return *g

View file

@ -63,7 +63,7 @@ func New(
*poolx.Pool[Drawer], *poolx.Pool[Drawer],
](true), ](true),
camera: &Camera{ camera: &Camera{
T: Transform{ Transform: Transform{
S: Vector{1, 1}, S: Vector{1, 1},
}, },
}, },

View file

@ -12,7 +12,7 @@ type Rectangle struct {
// Position of up left corner // Position of up left corner
// and the point to // and the point to
// rotate around(relatively of position, not absolute). // rotate around(relatively of position, not absolute).
T Transform Transform
// Width and height. // Width and height.
W, H Float W, H Float
@ -21,19 +21,13 @@ type Rectangle struct {
// The type describes rectangle that can be drawn. // The type describes rectangle that can be drawn.
type DrawableRectangle struct { type DrawableRectangle struct {
Rectangle Rectangle
ShaderOptions
// Solid color of the rectangle. // Solid color of the rectangle.
// Will be ignored if the Shader // Will be ignored if the Shader
// field is not nil. // field is not nil.
Color Color Color Color
// Shader to be applied
Shader *Shader
// Shader variables.
Uniforms map[string] any
// Shader images
Images [4]*Image
// Should be draw or not. // Should be draw or not.
Visible bool Visible bool
} }
@ -73,7 +67,7 @@ func (r *DrawableRectangle) Draw(
e *Engine, e *Engine,
i *Image, i *Image,
) { ) {
t := r.T t := r.Transform
// Draw solid color if no shader. // Draw solid color if no shader.
if r.Shader == nil { if r.Shader == nil {

View file

@ -5,10 +5,8 @@ import (
) )
type Sprite struct { type Sprite struct {
T Transform Transform
Shader *Shader ShaderOptions
Images [4]*Image
Uniforms map[string] any
Floating, Visible bool Floating, Visible bool
} }
@ -23,7 +21,7 @@ func (s *Sprite) Draw(
m := &Matrix{} m := &Matrix{}
m.Concat(s.T.Matrix(e)) m.Concat(s.Matrix(e))
if !s.Floating { if !s.Floating {
m.Concat(e.Camera().RealMatrix( m.Concat(e.Camera().RealMatrix(
e, e,