2021-08-31 15:16:56 +10:00
|
|
|
package engine
|
|
|
|
|
2021-09-02 14:05:20 +10:00
|
|
|
import (
|
2021-09-02 16:14:30 +10:00
|
|
|
"encoding/gob"
|
2021-09-02 14:05:20 +10:00
|
|
|
"image"
|
2021-09-02 15:55:38 +10:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ interface {
|
|
|
|
Prepper
|
|
|
|
Scanner
|
2021-09-03 09:42:50 +10:00
|
|
|
Transformer
|
2021-09-02 15:55:38 +10:00
|
|
|
} = &IsoVoxmap{}
|
|
|
|
|
|
|
|
_ interface {
|
|
|
|
Prepper
|
|
|
|
Scanner
|
2021-09-03 10:08:56 +10:00
|
|
|
Transformer
|
2021-09-02 15:55:38 +10:00
|
|
|
} = &IsoVoxel{}
|
|
|
|
|
2021-09-02 20:23:44 +10:00
|
|
|
_ Drawer = &IsoVoxelSide{}
|
2021-09-02 14:05:20 +10:00
|
|
|
)
|
2021-09-02 13:42:44 +10:00
|
|
|
|
2021-09-02 16:14:30 +10:00
|
|
|
func init() {
|
|
|
|
gob.Register(&IsoVoxmap{})
|
|
|
|
gob.Register(&IsoVoxel{})
|
|
|
|
gob.Register(&IsoVoxelSide{})
|
|
|
|
}
|
|
|
|
|
2021-09-02 15:55:38 +10:00
|
|
|
// IsoVoxmap implements a voxel map, painted using flat images in 2D.
|
|
|
|
type IsoVoxmap struct {
|
|
|
|
ID
|
|
|
|
Disabled
|
|
|
|
Hidden
|
|
|
|
Map map[Point3]*IsoVoxel
|
|
|
|
DrawOrderBias image.Point // so boxes overdraw correctly
|
2021-09-02 20:23:44 +10:00
|
|
|
DrawOffset image.Point
|
2021-09-02 15:55:38 +10:00
|
|
|
Sheet Sheet
|
|
|
|
VoxSize Point3 // size of each voxel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare makes sure all voxels know about the map and where they are, for
|
|
|
|
// drawing.
|
|
|
|
func (m *IsoVoxmap) Prepare(*Game) error {
|
|
|
|
// Ensure all child units know about wall, which houses common attributes
|
|
|
|
for p, u := range m.Map {
|
|
|
|
u.pos, u.ivm = p, m
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan returns the Sheet and all voxels in the map.
|
|
|
|
func (m *IsoVoxmap) Scan() []interface{} {
|
|
|
|
c := make([]interface{}, 1, len(m.Map)+1)
|
|
|
|
c[0] = &m.Sheet
|
|
|
|
for _, voxel := range m.Map {
|
|
|
|
c = append(c, voxel)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-09-03 09:42:50 +10:00
|
|
|
// Transform returns a translation by DrawOffset.
|
2021-09-03 10:08:56 +10:00
|
|
|
func (m *IsoVoxmap) Transform(pt Transform) (tf Transform) {
|
2021-09-03 09:42:50 +10:00
|
|
|
tf.Opts.GeoM.Translate(cfloat(m.DrawOffset))
|
2021-09-03 10:08:56 +10:00
|
|
|
return tf.Concat(pt)
|
2021-09-03 09:42:50 +10:00
|
|
|
}
|
|
|
|
|
2021-09-02 15:55:38 +10:00
|
|
|
// IsoVoxel is a voxel in an IsoVoxmap.
|
|
|
|
type IsoVoxel struct {
|
|
|
|
CellBack int // cell to draw for back side
|
|
|
|
CellFront int // cell to draw for front side
|
|
|
|
|
|
|
|
back IsoVoxelSide
|
|
|
|
front IsoVoxelSide
|
|
|
|
ivm *IsoVoxmap
|
|
|
|
pos Point3
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare tells the front and back about the voxel.
|
|
|
|
func (v *IsoVoxel) Prepare(*Game) error {
|
|
|
|
v.back.vox = v
|
|
|
|
v.front.vox = v
|
|
|
|
v.front.front = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan returns the back and front of the voxel.
|
|
|
|
func (v *IsoVoxel) Scan() []interface{} {
|
|
|
|
return []interface{}{&v.back, &v.front}
|
|
|
|
}
|
|
|
|
|
2021-09-03 10:08:56 +10:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-09-02 15:55:38 +10:00
|
|
|
// IsoVoxelSide is a side of a voxel.
|
|
|
|
type IsoVoxelSide struct {
|
|
|
|
front bool
|
|
|
|
vox *IsoVoxel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws this side.
|
|
|
|
func (v *IsoVoxelSide) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
|
|
|
|
cell := v.vox.CellBack
|
|
|
|
if v.front {
|
|
|
|
cell = v.vox.CellFront
|
|
|
|
}
|
|
|
|
screen.DrawImage(v.vox.ivm.Sheet.SubImage(cell), opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DrawOrder returns the Z of the nearest or farthest vertex of the voxel,
|
|
|
|
// with a bias equal to the dot product of the bias vector with pos.XY().
|
|
|
|
func (v *IsoVoxelSide) DrawOrder() (int, int) {
|
|
|
|
z := v.vox.pos.Z * v.vox.ivm.VoxSize.Z
|
|
|
|
if v.front {
|
|
|
|
z += v.vox.ivm.VoxSize.Z - 1
|
|
|
|
}
|
2021-09-02 20:17:45 +10:00
|
|
|
bias := dot(v.vox.pos.XY(), v.vox.ivm.DrawOrderBias)
|
|
|
|
return z, bias
|
2021-09-02 15:55:38 +10:00
|
|
|
}
|