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