prism map!

This commit is contained in:
Josh Deprez 2021-09-07 14:46:05 +10:00
parent da7d988383
commit 88fdaf18ac
3 changed files with 53 additions and 14 deletions

View file

@ -49,12 +49,12 @@ func (a IntMatrix3x4) Apply(v Int3) Int3 {
} }
// IntMatrix2x3 implements a 2 row, 3 column matrix (as two row vectors). // IntMatrix2x3 implements a 2 row, 3 column matrix (as two row vectors).
type IntMatrix2x3 [2]Int3 type IntMatrix2x3 struct{ X, Y Int3 }
// Apply applies the matrix to a vector to obtain a transformed vector. // Apply applies the matrix to a vector to obtain a transformed vector.
func (a IntMatrix2x3) Apply(v Int3) image.Point { func (a IntMatrix2x3) Apply(v Int3) image.Point {
return image.Point{ return image.Point{
X: v.Dot(a[0]), X: v.Dot(a.X),
Y: v.Dot(a[1]), Y: v.Dot(a.Y),
} }
} }

View file

@ -28,6 +28,7 @@ func init() {
gob.Register(&Prism{}) gob.Register(&Prism{})
} }
// PrismMap
type PrismMap struct { type PrismMap struct {
ID ID
Disabled Disabled
@ -36,10 +37,11 @@ type PrismMap struct {
Map map[Int3]*Prism // pos -> prism Map map[Int3]*Prism // pos -> prism
DrawOrderBias image.Point // dot with pos.XY() = bias value DrawOrderBias image.Point // dot with pos.XY() = bias value
DrawOffset image.Point // offset applies to whole map DrawOffset image.Point // offset applies to whole map
PosToDraw IntMatrix2x3 // p.pos -> drawspace (before offset and camera and ...) PosToWorld IntMatrix3x4 // p.pos -> voxelspace
PosToWorld IntMatrix3x4 // p.pos -> worldspace PrismSize Int3 // in voxelspace
PrismSize Int3 // in worldspace
Sheet Sheet Sheet Sheet
game *Game
} }
func (m *PrismMap) CollidesWith(b Box) bool { func (m *PrismMap) CollidesWith(b Box) bool {
@ -49,14 +51,24 @@ func (m *PrismMap) CollidesWith(b Box) bool {
return false return false
} }
func (m *PrismMap) Prepare(*Game) error { func (m *PrismMap) Prepare(g *Game) error {
m.game = g
for v, p := range m.Map { for v, p := range m.Map {
p.pos = v p.pos = v
p.pm = m p.m = m
} }
return nil return nil
} }
func (m *PrismMap) Scan() []interface{} {
c := make([]interface{}, 1, len(m.Map)+1)
c[0] = &m.Sheet
for _, prism := range m.Map {
c = append(c, prism)
}
return c
}
func (m *PrismMap) Transform() (opts ebiten.DrawImageOptions) { func (m *PrismMap) Transform() (opts ebiten.DrawImageOptions) {
opts.GeoM.Translate(cfloat(m.DrawOffset)) opts.GeoM.Translate(cfloat(m.DrawOffset))
return opts return opts
@ -66,21 +78,21 @@ type Prism struct {
Cell int Cell int
pos Int3 pos Int3
pm *PrismMap m *PrismMap
} }
func (p *Prism) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) { func (p *Prism) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
screen.DrawImage(p.pm.Sheet.SubImage(p.Cell), opts) screen.DrawImage(p.m.Sheet.SubImage(p.Cell), opts)
} }
func (p *Prism) DrawOrder() (int, int) { func (p *Prism) DrawOrder() (int, int) {
return p.pm.PosToWorld.Apply(p.pos).Z, return p.m.PosToWorld.Apply(p.pos).Z,
dot(p.pos.XY(), p.pm.DrawOrderBias) dot(p.pos.XY(), p.m.DrawOrderBias)
} }
func (p *Prism) Transform() (opts ebiten.DrawImageOptions) { func (p *Prism) Transform() (opts ebiten.DrawImageOptions) {
opts.GeoM.Translate(cfloat( opts.GeoM.Translate(cfloat(
p.pm.PosToDraw.Apply(p.pos), p.m.game.Projection.Project(p.m.PosToWorld.Apply(p.pos)),
)) ))
return opts return opts
} }

29
main.go
View file

@ -53,7 +53,7 @@ func main() {
g := &engine.Game{ g := &engine.Game{
ScreenSize: image.Pt(320, 240), // Window interior is this many pixels. ScreenSize: image.Pt(320, 240), // Window interior is this many pixels.
Projection: engine.IntProjection{ Projection: engine.IntProjection{
// Each 1 voxel step in Z is projected as 1 in Y. // Each 1 voxel step in Z is projected into 1 pixel in Y.
X: 0, X: 0,
Y: 1, Y: 1,
}, },
@ -137,6 +137,33 @@ func level1() *engine.Scene {
}, },
Factor: 0.5, Factor: 0.5,
}, },
&engine.PrismMap{
ID: "hexagons",
DrawOrderBias: image.Pt(0, -1), // draw higher Y after lower Y
DrawOffset: image.Pt(-8, 0),
PosToWorld: engine.IntMatrix3x4{
0: [4]int{24, 0, 0, 0},
1: [4]int{0, 16, 0, 0},
2: [4]int{8, 0, 16, 0},
},
Sheet: engine.Sheet{
CellSize: image.Pt(32, 32),
Src: engine.ImageRef{Path: "assets/hexprism32.png"},
},
Map: map[engine.Int3]*engine.Prism{
engine.Pt3(0, 0, 0): {},
engine.Pt3(1, 0, 0): {},
engine.Pt3(2, 0, 0): {},
engine.Pt3(4, -2, 0): {},
engine.Pt3(4, -1, 0): {},
engine.Pt3(4, 0, 0): {},
engine.Pt3(6, 0, -4): {},
engine.Pt3(6, 0, -3): {},
engine.Pt3(6, 0, -2): {},
},
},
&engine.Tilemap{ &engine.Tilemap{
ID: "terrain", ID: "terrain",
ZOrder: -1, ZOrder: -1,