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 {
|
func (a *Actor) collidesAt(p image.Point) bool {
|
||||||
// TODO: more efficient test?
|
// TODO: more efficient test?
|
||||||
hit := false
|
hit := false
|
||||||
a.game.Walk(func(c interface{}) bool {
|
Walk(a.game, func(c interface{}) bool {
|
||||||
if coll, ok := c.(Collider); ok {
|
if coll, ok := c.(Collider); ok {
|
||||||
if coll.CollidesWith(image.Rectangle{Min: p, Max: p.Add(a.Size)}) {
|
if coll.CollidesWith(image.Rectangle{Min: p, Max: p.Add(a.Size)}) {
|
||||||
hit = true
|
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.
|
// 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] }
|
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
|
// Scan implements Scanner.
|
||||||
// returns true.
|
func (g *Game) Scan() []interface{} { return []interface{}{g.Scene} }
|
||||||
func (g *Game) Walk(v func(interface{}) bool) {
|
|
||||||
g.walk(g.Scene, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
if !v(c) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -99,7 +98,7 @@ func (g *Game) walk(c interface{}, v func(interface{}) bool) {
|
||||||
if !v(c) {
|
if !v(c) {
|
||||||
return
|
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.
|
// reachable via Scan.
|
||||||
func (g *Game) Build() {
|
func (g *Game) Build() {
|
||||||
g.componentsByID = make(map[string]interface{})
|
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 {
|
if b, ok := c.(Builder); ok {
|
||||||
b.Build(g)
|
b.Build(g)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,12 +19,31 @@ type Tilemap struct {
|
||||||
Hidden bool
|
Hidden bool
|
||||||
ID
|
ID
|
||||||
Map [][]Tile
|
Map [][]Tile
|
||||||
|
Ersatz bool // "fake wall"
|
||||||
Src ImageRef
|
Src ImageRef
|
||||||
TileSize int
|
TileSize int
|
||||||
Transform GeoMDef
|
Transform GeoMDef
|
||||||
ZPos
|
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.
|
// Draw draws the tilemap.
|
||||||
func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
||||||
if t.Hidden {
|
if t.Hidden {
|
||||||
|
|
13
main.go
13
main.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
|
||||||
"image"
|
"image"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"log"
|
"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{
|
game := &engine.Game{
|
||||||
ScreenHeight: screenHeight,
|
ScreenHeight: screenHeight,
|
||||||
ScreenWidth: screenWidth,
|
ScreenWidth: screenWidth,
|
||||||
|
|
Loading…
Reference in a new issue