comments, interfaces
This commit is contained in:
parent
073eff4a61
commit
25f4f29e1b
3 changed files with 18 additions and 131 deletions
BIN
asset_src/hexprism32.aseprite
Normal file
BIN
asset_src/hexprism32.aseprite
Normal file
Binary file not shown.
126
engine/iso.go
126
engine/iso.go
|
@ -1,126 +0,0 @@
|
||||||
package engine
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/gob"
|
|
||||||
"image"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
_ interface {
|
|
||||||
Prepper
|
|
||||||
Scanner
|
|
||||||
Transformer
|
|
||||||
} = &IsoVoxmap{}
|
|
||||||
|
|
||||||
_ interface {
|
|
||||||
Prepper
|
|
||||||
Scanner
|
|
||||||
Transformer
|
|
||||||
} = &IsoVoxel{}
|
|
||||||
|
|
||||||
_ Drawer = &IsoVoxelSide{}
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
gob.Register(&IsoVoxmap{})
|
|
||||||
gob.Register(&IsoVoxel{})
|
|
||||||
gob.Register(&IsoVoxelSide{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
DrawOffset image.Point
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transform returns a translation by DrawOffset.
|
|
||||||
func (m *IsoVoxmap) Transform(pt Transform) (tf Transform) {
|
|
||||||
tf.Opts.GeoM.Translate(cfloat(m.DrawOffset))
|
|
||||||
return tf.Concat(pt)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
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
|
|
||||||
}
|
|
||||||
bias := dot(v.vox.pos.XY(), v.vox.ivm.DrawOrderBias)
|
|
||||||
return z, bias
|
|
||||||
}
|
|
|
@ -9,6 +9,10 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ interface {
|
_ interface {
|
||||||
|
Identifier
|
||||||
|
Collider
|
||||||
|
Disabler
|
||||||
|
Hider
|
||||||
Prepper
|
Prepper
|
||||||
Transformer
|
Transformer
|
||||||
} = &PrismMap{}
|
} = &PrismMap{}
|
||||||
|
@ -25,14 +29,23 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type PrismMap struct {
|
type PrismMap struct {
|
||||||
Map map[Point3]*Prism
|
ID
|
||||||
DrawOrderBias image.Point // dot with (X,Y) = bias
|
Disabled
|
||||||
DrawOffset image.Point // offset to apply to whole map
|
Hidden
|
||||||
DrawZStride image.Point // draw offset for each unit in Z
|
|
||||||
PrismSize Point3 // cmul map key = world pos
|
Map map[Point3]*Prism // pos -> prism
|
||||||
|
DrawOrderBias image.Point // dot with pos.XY() = bias value
|
||||||
|
DrawOffset image.Point // offset applies to whole map
|
||||||
|
DrawZStride image.Point // draw offset for each pos unit in Z
|
||||||
|
PrismSize Point3 // (prismsize cmul pos) = world position
|
||||||
Sheet Sheet
|
Sheet Sheet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *PrismMap) CollidesWith(b Box) bool {
|
||||||
|
// TODO
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (m *PrismMap) Prepare(*Game) error {
|
func (m *PrismMap) Prepare(*Game) error {
|
||||||
for v, p := range m.Map {
|
for v, p := range m.Map {
|
||||||
p.pos = v
|
p.pos = v
|
||||||
|
|
Loading…
Reference in a new issue