Make Tilemap a collider
This commit is contained in:
parent
d4dcd952a4
commit
8b3a0c78e4
4 changed files with 27 additions and 22 deletions
|
@ -39,7 +39,7 @@ type Actor struct {
|
|||
func (a *Actor) collidesAt(p image.Point) bool {
|
||||
// TODO: more efficient test?
|
||||
hit := false
|
||||
a.game.Walk(func(c interface{}) bool {
|
||||
Walk(a.game, func(c interface{}) bool {
|
||||
if coll, ok := c.(Collider); ok {
|
||||
if coll.CollidesWith(image.Rectangle{Min: p, Max: p.Add(a.Size)}) {
|
||||
hit = true
|
||||
|
|
|
@ -84,13 +84,12 @@ func (g *Game) UnregisterComponent(c interface{}) {
|
|||
// Component returns the component with a given ID, or nil if there is none.
|
||||
func (g *Game) Component(id string) interface{} { return g.componentsByID[id] }
|
||||
|
||||
// Walk calls v with every component reachable via Scan, for as long as visit
|
||||
// returns true.
|
||||
func (g *Game) Walk(v func(interface{}) bool) {
|
||||
g.walk(g.Scene, v)
|
||||
}
|
||||
// Scan implements Scanner.
|
||||
func (g *Game) Scan() []interface{} { return []interface{}{g.Scene} }
|
||||
|
||||
func (g *Game) walk(c interface{}, v func(interface{}) bool) {
|
||||
// Walk calls v with every component reachable from c via Scan, recursively,
|
||||
// for as long as visit returns true.
|
||||
func Walk(c interface{}, v func(interface{}) bool) {
|
||||
if !v(c) {
|
||||
return
|
||||
}
|
||||
|
@ -99,7 +98,7 @@ func (g *Game) walk(c interface{}, v func(interface{}) bool) {
|
|||
if !v(c) {
|
||||
return
|
||||
}
|
||||
g.walk(c, v)
|
||||
Walk(c, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -108,7 +107,7 @@ func (g *Game) walk(c interface{}, v func(interface{}) bool) {
|
|||
// reachable via Scan.
|
||||
func (g *Game) Build() {
|
||||
g.componentsByID = make(map[string]interface{})
|
||||
g.Walk(func(c interface{}) bool {
|
||||
Walk(g.Scene, func(c interface{}) bool {
|
||||
if b, ok := c.(Builder); ok {
|
||||
b.Build(g)
|
||||
}
|
||||
|
|
|
@ -19,12 +19,31 @@ type Tilemap struct {
|
|||
Hidden bool
|
||||
ID
|
||||
Map [][]Tile
|
||||
Ersatz bool // "fake wall"
|
||||
Src ImageRef
|
||||
TileSize int
|
||||
Transform GeoMDef
|
||||
ZPos
|
||||
}
|
||||
|
||||
// CollidesWith implements Collider.
|
||||
func (t *Tilemap) CollidesWith(r image.Rectangle) bool {
|
||||
if t.Ersatz {
|
||||
return false
|
||||
}
|
||||
for j, row := range t.Map {
|
||||
for i, tile := range row {
|
||||
if tile == nil {
|
||||
continue
|
||||
}
|
||||
if r.Overlaps(image.Rect(i*t.TileSize, j*t.TileSize, (i+1)*t.TileSize, (j+1)*t.TileSize)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Draw draws the tilemap.
|
||||
func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
||||
if t.Hidden {
|
||||
|
|
13
main.go
13
main.go
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/png"
|
||||
"log"
|
||||
|
@ -92,18 +91,6 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
// TODO: something better...
|
||||
for j, row := range tiles {
|
||||
for i, tile := range row {
|
||||
if tile != nil {
|
||||
level1.Components = append(level1.Components, &engine.SolidRect{
|
||||
ID: engine.ID(fmt.Sprintf("tile_%d_%d", j, i)),
|
||||
Rect: image.Rect(i*16, j*16, (i+1)*16, (j+1)*16),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
game := &engine.Game{
|
||||
ScreenHeight: screenHeight,
|
||||
ScreenWidth: screenWidth,
|
||||
|
|
Loading…
Reference in a new issue