progress on isometric

This commit is contained in:
Josh Deprez 2021-09-02 15:55:38 +10:00
parent 4970326114
commit abdb975979
14 changed files with 161 additions and 22 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -41,8 +41,9 @@ func (d *DebugToast) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions) {
ebitenutil.DebugPrintAt(screen, d.Text, d.Pos.X, d.Pos.Y) ebitenutil.DebugPrintAt(screen, d.Text, d.Pos.X, d.Pos.Y)
} }
func (d *DebugToast) DrawOrder() float64 { func (d *DebugToast) DrawOrder() (int, int) {
return math.MaxFloat64 // Always draw on top
return math.MaxInt, 0
} }
func (d *DebugToast) Toast(text string) { func (d *DebugToast) Toast(text string) {
@ -67,7 +68,7 @@ func (p PerfDisplay) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions) {
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS())) ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
} }
func (PerfDisplay) DrawOrder() float64 { func (PerfDisplay) DrawOrder() (int, int) {
// Always draw on top // Always draw on top
return math.MaxFloat64 return math.MaxInt, 0
} }

View file

@ -424,11 +424,19 @@ type tombstone struct{}
func (tombstone) Draw(*ebiten.Image, *ebiten.DrawImageOptions) {} func (tombstone) Draw(*ebiten.Image, *ebiten.DrawImageOptions) {}
func (tombstone) DrawOrder() float64 { return math.Inf(1) } func (tombstone) DrawOrder() (int, int) { return math.MaxInt, math.MaxInt }
type drawList []Drawer type drawList []Drawer
func (d drawList) Less(i, j int) bool { return d[i].DrawOrder() < d[j].DrawOrder() } func (d drawList) Less(i, j int) bool {
a0, a1 := d[i].DrawOrder()
b0, b1 := d[i].DrawOrder()
if a0 == b0 {
return a1 < b1
}
return a0 < b0
}
func (d drawList) Len() int { return len(d) } func (d drawList) Len() int { return len(d) }
func (d drawList) Swap(i, j int) { d[i], d[j] = d[j], d[i] } func (d drawList) Swap(i, j int) { d[i], d[j] = d[j], d[i] }

View file

@ -64,7 +64,7 @@ type Disabler interface {
// passed to Game.Register or returned from Scan). // passed to Game.Register or returned from Scan).
type Drawer interface { type Drawer interface {
Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions)
DrawOrder() float64 DrawOrder() (z, bias int)
} }
// Hider components can be hidden. // Hider components can be hidden.

View file

@ -3,6 +3,26 @@ package engine
import ( import (
"image" "image"
"strconv" "strconv"
"github.com/hajimehoshi/ebiten/v2"
)
var (
_ interface {
Prepper
Scanner
} = &IsoVoxmap{}
_ interface {
Prepper
Scanner
Transformer
} = &IsoVoxel{}
_ interface {
Drawer
Transformer
} = &IsoVoxelSide{}
) )
// Point3 is a an element of int^3. // Point3 is a an element of int^3.
@ -20,6 +40,11 @@ func (p Point3) String() string {
return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + "," + strconv.Itoa(p.Z) + ")" return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + "," + strconv.Itoa(p.Z) + ")"
} }
// XY applies the Z-forgetting projection. (It returns just X and Y.)
func (p Point3) XY() image.Point {
return image.Point{p.X, p.Y}
}
// Add performs vector addition. // Add performs vector addition.
func (p Point3) Add(q Point3) Point3 { func (p Point3) Add(q Point3) Point3 {
return Point3{p.X + q.X, p.Y + q.Y, p.Z + q.Z} return Point3{p.X + q.X, p.Y + q.Y, p.Z + q.Z}
@ -117,3 +142,104 @@ func (b Box) Overlaps(c Box) bool {
func (b Box) Size() Point3 { func (b Box) Size() Point3 {
return b.Max.Sub(b.Min) return b.Max.Sub(b.Min)
} }
// 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
OffsetBack image.Point // offsets the image drawn for the back
OffsetFront image.Point // offsets the image drawn for the front
Proj image.Point // IsoProjection parameter
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
}
// 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 the transform for drawing this voxel.
func (v *IsoVoxel) Transform() (opts ebiten.DrawImageOptions) {
p3 := v.pos.CMul(v.ivm.VoxSize)
p2 := p3.IsoProject(v.ivm.Proj)
opts.GeoM.Translate(cfloat(p2))
return opts
}
// 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
}
return z, dot(v.vox.pos.XY(), v.vox.ivm.DrawOrderBias)
}
// Transform offsets the image by either OffsetBack or OffsetFront.
func (v *IsoVoxelSide) Transform() (opts ebiten.DrawImageOptions) {
if v.front {
opts.GeoM.Translate(cfloat(v.vox.ivm.OffsetFront))
} else {
opts.GeoM.Translate(cfloat(v.vox.ivm.OffsetBack))
}
return opts
}

View file

@ -38,11 +38,11 @@ func (h *Hidden) Hide() { *h = true }
// Show sets h to false. // Show sets h to false.
func (h *Hidden) Show() { *h = false } func (h *Hidden) Show() { *h = false }
// ZOrder implements DrawOrder (in Drawer) directly (as a float64 value). // ZOrder implements DrawOrder (in Drawer) directly (as an int value).
type ZOrder float64 type ZOrder int
// DrawOrder returns z as a float64. // DrawOrder returns z as a int with 0 bias.
func (z ZOrder) DrawOrder() float64 { return float64(z) } func (z ZOrder) DrawOrder() (int, int) { return int(z), 0 }
// ---------- Some helpers for image.Point ---------- // ---------- Some helpers for image.Point ----------
@ -54,10 +54,14 @@ func cmul(p, q image.Point) image.Point {
// cdiv performs componentwise division of two image.Points. // cdiv performs componentwise division of two image.Points.
func cdiv(p, q image.Point) image.Point { func cdiv(p, q image.Point) image.Point {
return image.Point{p.X / q.X, p.Y / q.Y} return image.Point{p.X / q.X, p.Y / q.Y}
} }
// cfloat returns the components of an image.Point as two floats. // cfloat returns the components of an image.Point as two floats.
func cfloat(p image.Point) (x, y float64) { func cfloat(p image.Point) (x, y float64) {
return float64(p.X), float64(p.Y) return float64(p.X), float64(p.Y)
} }
// dot returns the dot product of two image.Points.
func dot(p, q image.Point) int {
return p.X*q.X + p.Y*q.Y
}

View file

@ -69,11 +69,12 @@ func (w *Wall) Scan() []interface{} {
return c return c
} }
// Prepare makes sure all WallUnits know about Wall. // Prepare makes sure all WallUnits know about Wall and where they are, for
// drawing.
func (w *Wall) Prepare(*Game) error { func (w *Wall) Prepare(*Game) error {
// Ensure all child units know about wall, which houses common attributes // Ensure all child units know about wall, which houses common attributes
for _, u := range w.Units { for p, u := range w.Units {
u.wall = w u.pos, u.wall = p, w
} }
return nil return nil
} }
@ -89,11 +90,10 @@ func (w *Wall) Transform() (opts ebiten.DrawImageOptions) {
type WallUnit struct { type WallUnit struct {
Disabled Disabled
Hidden Hidden
Pos image.Point // tilespace coordinates
Tile Tile // chooses which cell in wall.Sheet to draw Tile Tile // chooses which cell in wall.Sheet to draw
WallID string
ZOrder ZOrder
pos image.Point // tilespace coordinates
wall *Wall wall *Wall
} }
@ -106,6 +106,6 @@ func (u *WallUnit) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
func (u *WallUnit) Scan() []interface{} { return []interface{}{u.Tile} } func (u *WallUnit) Scan() []interface{} { return []interface{}{u.Tile} }
func (u *WallUnit) Transform() (opts ebiten.DrawImageOptions) { func (u *WallUnit) Transform() (opts ebiten.DrawImageOptions) {
opts.GeoM.Translate(cfloat(cmul(u.Pos, u.wall.UnitSize).Add(u.wall.UnitOffset))) opts.GeoM.Translate(cfloat(cmul(u.pos, u.wall.UnitSize).Add(u.wall.UnitOffset)))
return opts return opts
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

View file

@ -16,7 +16,7 @@ import (
const ( const (
enableCPUProfile = false enableCPUProfile = false
rewriteLevel1 = false rewriteLevel1 = true
) )
func main() { func main() {