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 {
return &Rect{&gx.DrawableRectangle{
Rectangle: gx.Rectangle{
Transform: gx.T(),
W: 200,
H: 400,
T: gx.T(),
},
Color: gx.Color{
gx.MaxColorV,
@ -60,13 +60,15 @@ var (
func NewPlayer() *Player {
ret := &Player{
Sprite: &gx.Sprite{
T: gx.Transform {
Transform: gx.Transform {
S: gx.Vector{5, 5},
RA: gx.Vector{320, 240},
},
Visible: true,
Shader: gx.SolidWhiteColorShader,
Uniforms: make(map[string] any),
ShaderOptions: gx.ShaderOptions {
Shader: gx.SolidWhiteColorShader,
Uniforms: make(map[string] any),
},
},
MoveSpeed: 90.,
ScaleSpeed: .2,
@ -80,7 +82,7 @@ func NewPlayer() *Player {
func (p *Player) Start(e *gx.Engine, v ...any) {
fmt.Println("starting")
c := e.Camera()
c.T.RA = gx.V(360, -240)
c.RA = gx.V(360, -240)
}
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())
for _, v := range keys {switch v {
case ebiten.KeyArrowUp :
c.T.P.Y += p.MoveSpeed * dt
c.P.Y += p.MoveSpeed * dt
case ebiten.KeyArrowLeft :
c.T.P.X -= p.MoveSpeed * dt
c.P.X -= p.MoveSpeed * dt
case ebiten.KeyArrowDown :
c.T.P.Y -= p.MoveSpeed * dt
c.P.Y -= p.MoveSpeed * dt
case ebiten.KeyArrowRight :
c.T.P.X += p.MoveSpeed * dt
c.P.X += p.MoveSpeed * dt
case ebiten.KeyW :
p.T.P.Y += p.MoveSpeed * dt
p.P.Y += p.MoveSpeed * dt
case ebiten.KeyA :
p.T.P.X -= p.MoveSpeed * dt
p.P.X -= p.MoveSpeed * dt
case ebiten.KeyS :
p.T.P.Y -= p.MoveSpeed * dt
p.P.Y -= p.MoveSpeed * dt
case ebiten.KeyD :
p.T.P.X += p.MoveSpeed * dt
p.P.X += p.MoveSpeed * dt
case ebiten.KeyR :
c.T.R += gx.Pi * p.ScaleSpeed * dt
c.R += gx.Pi * p.ScaleSpeed * dt
case ebiten.KeyT :
c.T.R -= gx.Pi * p.ScaleSpeed * dt
c.R -= gx.Pi * p.ScaleSpeed * dt
case ebiten.KeyF :
if e.KeyIsPressed(ebiten.KeyShift) {
c.T.S.X -= gx.Pi * p.ScaleSpeed * dt
c.S.X -= gx.Pi * p.ScaleSpeed * dt
} else {
c.T.S.X += gx.Pi * p.ScaleSpeed * dt
c.S.X += gx.Pi * p.ScaleSpeed * dt
}
case ebiten.KeyG :
if e.KeyIsPressed(ebiten.KeyShift) {
c.T.S.Y -= gx.Pi * p.ScaleSpeed * dt
c.S.Y -= gx.Pi * p.ScaleSpeed * dt
} else {
c.T.S.Y += gx.Pi * p.ScaleSpeed * dt
c.S.Y += gx.Pi * p.ScaleSpeed * dt
}
case ebiten.KeyZ :
if e.KeyIsPressed(ebiten.KeyShift) {
c.T.RA.X -= gx.Pi * p.MoveSpeed * dt
c.RA.X -= gx.Pi * p.MoveSpeed * dt
} 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 :
if e.KeyIsPressed(ebiten.KeyShift) {
c.T.RA.Y -= gx.Pi * p.MoveSpeed * dt
c.RA.Y -= gx.Pi * p.MoveSpeed * dt
} 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 :
e.Del(p)
}}

View file

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

View file

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

View file

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

View file

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