Transform now responsible for concat

This commit is contained in:
Josh Deprez 2021-09-03 10:08:56 +10:00
parent e065bad1a7
commit 88ded18d50
9 changed files with 40 additions and 21 deletions

View file

@ -36,7 +36,7 @@ func (b *Billboard) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
// Scan returns a slice containing Src.
func (b *Billboard) Scan() []interface{} { return []interface{}{&b.Src} }
func (b *Billboard) Transform() (tf Transform) {
func (b *Billboard) Transform(pt Transform) (tf Transform) {
tf.Opts.GeoM.Translate(cfloat(b.Pos))
return tf
return tf.Concat(pt)
}

View file

@ -85,11 +85,11 @@ func (c *Camera) Prepare(game *Game) error {
func (c *Camera) Scan() []interface{} { return []interface{}{c.Child} }
// Transform returns the camera transform.
func (c *Camera) Transform() (tf Transform) {
func (c *Camera) Transform(pt Transform) (tf Transform) {
tf.IsoProjection = c.IsoProjection
tf.Opts.GeoM.Translate(cfloat(c.Centre.Mul(-1)))
tf.Opts.GeoM.Scale(c.Zoom, c.Zoom)
tf.Opts.GeoM.Rotate(c.Rotation)
tf.Opts.GeoM.Translate(cfloat(c.game.ScreenSize.Div(2)))
return tf
return tf.Concat(pt)
}

View file

@ -96,8 +96,8 @@ func (g *Game) Draw(screen *ebiten.Image) {
continue
}
// p is not hidden, so compute its cumulative transform.
if t, ok := p.(Transformer); ok {
st.transform = t.Transform().Concat(st.transform)
if tf, ok := p.(Transformer); ok {
st.transform = tf.Transform(st.transform)
}
accum[p] = st
}

View file

@ -105,9 +105,17 @@ type Saver interface {
Save() error
}
// Transformer components can transform their child components.
// Transformer components can provide a transform to apply to themselves and any
// child components, based on the cumulative parent transform. An
// example implementation:
//
// func (f Foo) Transform(pt Transform) Transform {
// var tf Transform
// tf.Opts.GeoM.Translate(-2, 3) // or your own transform
// return tf.Concat(pt)
// }
type Transformer interface {
Transform() Transform
Transform(Transform) Transform
}
// Updater components can update themselves. Update is called repeatedly. Each

View file

@ -17,6 +17,7 @@ var (
_ interface {
Prepper
Scanner
Transformer
} = &IsoVoxel{}
_ Drawer = &IsoVoxelSide{}
@ -61,9 +62,9 @@ func (m *IsoVoxmap) Scan() []interface{} {
}
// Transform returns a translation by DrawOffset.
func (m *IsoVoxmap) Transform() (tf Transform) {
func (m *IsoVoxmap) Transform(pt Transform) (tf Transform) {
tf.Opts.GeoM.Translate(cfloat(m.DrawOffset))
return tf
return tf.Concat(pt)
}
// IsoVoxel is a voxel in an IsoVoxmap.
@ -90,6 +91,14 @@ func (v *IsoVoxel) Scan() []interface{} {
return []interface{}{&v.back, &v.front}
}
// Transform returns a translation the isoprojected coordinate of this voxel.
func (v *IsoVoxel) Transform(pt Transform) (tf Transform) {
tf.Opts.GeoM.Translate(cfloat(
v.pos.CMul(v.ivm.VoxSize).IsoProject(pt.IsoProjection),
))
return tf.Concat(pt)
}
// IsoVoxelSide is a side of a voxel.
type IsoVoxelSide struct {
front bool

View file

@ -39,8 +39,8 @@ func (p *Parallax) Prepare(game *Game) error {
func (p *Parallax) Scan() []interface{} { return []interface{}{p.Child} }
// Transform returns a GeoM translation of Factor * camera.Centre.
func (p *Parallax) Transform() (tf Transform) {
func (p *Parallax) Transform(pt Transform) (tf Transform) {
x, y := cfloat(p.camera.Centre)
tf.Opts.GeoM.Translate(x*p.Factor, y*p.Factor)
return tf
return tf.Concat(pt)
}

View file

@ -57,9 +57,9 @@ func (s *Sprite) SetAnim(a *Anim) {
}
// Transform returns a translation by the FrameOffset.
func (s *Sprite) Transform() (tf Transform) {
func (s *Sprite) Transform(pt Transform) (tf Transform) {
tf.Opts.GeoM.Translate(cfloat(s.Actor.Pos.XY().Add(s.FrameOffset)))
return tf
return tf.Concat(pt)
}
// Update updates the Sprite's anim. anim can change a bit so we don't tell Game

View file

@ -110,9 +110,9 @@ func (t *Tilemap) Scan() []interface{} {
}
// Transform returns a translation by t.Offset.
func (t *Tilemap) Transform() (tf Transform) {
func (t *Tilemap) Transform(pt Transform) (tf Transform) {
tf.Opts.GeoM.Translate(cfloat(t.Offset))
return tf
return tf.Concat(pt)
}
// TileAt returns the tile present at the given world coordinate.

View file

@ -80,9 +80,9 @@ func (w *Wall) Prepare(*Game) error {
}
// Transform returns a GeoM translation by Offset.
func (w *Wall) Transform() (tf Transform) {
func (w *Wall) Transform(pt Transform) (tf Transform) {
tf.Opts.GeoM.Translate(cfloat(w.Offset))
return tf
return tf.Concat(pt)
}
// WallUnit is a unit in a wall. Unlike a tile in a tilemap, WallUnit is
@ -105,7 +105,9 @@ func (u *WallUnit) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
// Scan returns the Tile.
func (u *WallUnit) Scan() []interface{} { return []interface{}{u.Tile} }
func (u *WallUnit) Transform() (tf Transform) {
tf.Opts.GeoM.Translate(cfloat(cmul(u.pos, u.wall.UnitSize).Add(u.wall.UnitOffset)))
return tf
func (u *WallUnit) Transform(pt Transform) (tf Transform) {
tf.Opts.GeoM.Translate(cfloat(
cmul(u.pos, u.wall.UnitSize).Add(u.wall.UnitOffset),
))
return tf.Concat(pt)
}