walking and colliding
This commit is contained in:
parent
a19cdfdf6f
commit
b65e5043ab
4 changed files with 63 additions and 42 deletions
|
@ -2,14 +2,15 @@ package engine
|
|||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
)
|
||||
|
||||
// Ensure Actor satisfies interfaces.
|
||||
var _ Prepper = &Actor{}
|
||||
|
||||
var errCollision = errors.New("collision detected")
|
||||
var _ interface {
|
||||
Bounder
|
||||
Prepper
|
||||
} = &Actor{}
|
||||
|
||||
func init() {
|
||||
gob.Register(&Actor{})
|
||||
|
@ -28,14 +29,17 @@ type Actor struct {
|
|||
xRem, yRem float64
|
||||
}
|
||||
|
||||
func (a *Actor) BoundingRect() image.Rectangle { return image.Rectangle{a.Pos, a.Pos.Add(a.Size)} }
|
||||
|
||||
func (a *Actor) CollidesAt(p image.Point) bool {
|
||||
return nil != Walk(a.collisionDomain, func(c interface{}) error {
|
||||
bounds := image.Rectangle{Min: p, Max: p.Add(a.Size)}
|
||||
return nil != Walk(a.collisionDomain, func(c interface{}, _ []interface{}) error {
|
||||
coll, ok := c.(Collider)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if coll.CollidesWith(image.Rectangle{Min: p, Max: p.Add(a.Size)}) {
|
||||
return errCollision
|
||||
if coll.CollidesWith(bounds) {
|
||||
return Collision{With: coll}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
@ -91,3 +95,13 @@ func sign(m int) int {
|
|||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// Collision reports a collision occurred.
|
||||
type Collision struct {
|
||||
With Collider
|
||||
}
|
||||
|
||||
// Error is really only to implement the error interface.
|
||||
func (c Collision) Error() string {
|
||||
return fmt.Sprintf("collision with %v", c.With)
|
||||
}
|
||||
|
|
|
@ -23,8 +23,13 @@ type Game struct {
|
|||
ScreenHeight int
|
||||
Root DrawUpdater // typically a *Scene or SceneRef though
|
||||
|
||||
// Components by ID
|
||||
dbmu sync.RWMutex
|
||||
db map[string]interface{}
|
||||
|
||||
// Collision domains - all Collider subcomponents
|
||||
cdmu sync.RWMutex
|
||||
cd map[string]map[Collider]struct{}
|
||||
}
|
||||
|
||||
// Draw draws the entire thing, with default draw options.
|
||||
|
@ -93,53 +98,63 @@ func (g *Game) Component(id string) interface{} {
|
|||
// Scan implements Scanner.
|
||||
func (g *Game) Scan() []interface{} { return []interface{}{g.Root} }
|
||||
|
||||
// Walk calls v with every component reachable from c via Scan, recursively,
|
||||
// for as long as visit returns nil.
|
||||
func Walk(c interface{}, v func(interface{}) error) error {
|
||||
if err := v(c); err != nil {
|
||||
// Walk calls v with every path of components reachable from c via Scan, for as
|
||||
// long as visit returns nil.
|
||||
func Walk(c interface{}, v func(interface{}, []interface{}) error) error {
|
||||
return walk(c, nil, v)
|
||||
}
|
||||
|
||||
func walk(c interface{}, p []interface{}, v func(interface{}, []interface{}) error) error {
|
||||
if err := v(c, p); err != nil {
|
||||
return err
|
||||
}
|
||||
sc, ok := c.(Scanner)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
p = append(p, c)
|
||||
for _, c := range sc.Scan() {
|
||||
if err := Walk(c, v); err != nil {
|
||||
if err := walk(c, p, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load calls Load on all Loaders reachable via Scan (using Walk).
|
||||
// It stops on the first error.
|
||||
func (g *Game) Load(assets fs.FS) error {
|
||||
return Walk(g.Root, func(c interface{}) error {
|
||||
// LoadAndPrepare first calls Load on all Loaders. Once loading is complete,
|
||||
// it builds the component database and then calls Prepare on every Preparer.
|
||||
// You must call Prepare before any calls
|
||||
// to Component. You may call Prepare again (e.g. as an alternative to
|
||||
// fastidiously calling RegisterComponent/UnregisterComponent).
|
||||
func (g *Game) LoadAndPrepare(assets fs.FS) error {
|
||||
if err := Walk(g.Root, func(c interface{}, _ []interface{}) error {
|
||||
l, ok := c.(Loader)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return l.Load(assets)
|
||||
})
|
||||
}
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.cdmu.Lock()
|
||||
g.cd = make(map[string]map[Collider]struct{})
|
||||
g.cdmu.Unlock()
|
||||
|
||||
// Prepare builds the component database (using Walk) and then calls
|
||||
// Prepare on every Preparer. You must call Prepare before any calls
|
||||
// to Component. You may call Prepare again (e.g. as an alternative to
|
||||
// fastidiously calling RegisterComponent/UnregisterComponent).
|
||||
func (g *Game) Prepare() {
|
||||
g.dbmu.Lock()
|
||||
g.db = make(map[string]interface{})
|
||||
g.dbmu.Unlock()
|
||||
// Moment in time where db is empty... whatev.
|
||||
Walk(g.Root, func(c interface{}) error {
|
||||
|
||||
// -> here <- is the moment in time where db is empty.
|
||||
Walk(g.Root, func(c interface{}, p []interface{}) error {
|
||||
g.RegisterComponent(c)
|
||||
return nil
|
||||
})
|
||||
Walk(g.Root, func(c interface{}) error {
|
||||
Walk(g.Root, func(c interface{}, _ []interface{}) error {
|
||||
if p, ok := c.(Prepper); ok {
|
||||
p.Prepare(g)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -62,11 +62,10 @@ func (g *Game) cmdSave(dst io.Writer, argv []string) {
|
|||
func (g *Game) cmdReload(dst io.Writer, assets fs.FS) {
|
||||
g.Disable()
|
||||
g.Hide()
|
||||
if err := g.Load(assets); err != nil {
|
||||
if err := g.LoadAndPrepare(assets); err != nil {
|
||||
fmt.Fprintf(dst, "Couldn't load: %v\n", err)
|
||||
return
|
||||
}
|
||||
g.Prepare()
|
||||
g.Enable()
|
||||
g.Show()
|
||||
}
|
||||
|
@ -84,11 +83,10 @@ func (g *Game) cmdTree(dst io.Writer, argv []string) {
|
|||
return
|
||||
}
|
||||
}
|
||||
var walk func(interface{}, int)
|
||||
walk = func(c interface{}, depth int) {
|
||||
Walk(c, func(c interface{}, p []interface{}) error {
|
||||
indent := ""
|
||||
if depth > 0 {
|
||||
indent = strings.Repeat(" ", depth-1) + "↳ "
|
||||
if len(p) > 0 {
|
||||
indent = strings.Repeat(" ", len(p)-1) + "↳ "
|
||||
}
|
||||
i, ok := c.(Identifier)
|
||||
if ok {
|
||||
|
@ -96,11 +94,6 @@ func (g *Game) cmdTree(dst io.Writer, argv []string) {
|
|||
} else {
|
||||
fmt.Fprintf(dst, "%s%T\n", indent, c)
|
||||
}
|
||||
if s, ok := c.(Scanner); ok {
|
||||
for _, d := range s.Scan() {
|
||||
walk(d, depth+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
walk(c, 0)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
|
5
main.go
5
main.go
|
@ -42,10 +42,9 @@ func main() {
|
|||
},
|
||||
},
|
||||
}
|
||||
if err := g.Load(game.Assets); err != nil {
|
||||
log.Fatalf("Loading error: %v", err)
|
||||
if err := g.LoadAndPrepare(game.Assets); err != nil {
|
||||
log.Fatalf("Loading/preparing error: %v", err)
|
||||
}
|
||||
g.Prepare()
|
||||
|
||||
if runtime.GOOS != "js" {
|
||||
// Run a repl on the console.
|
||||
|
|
Loading…
Reference in a new issue