doodle/pkg/collision/collide_actors.go
Noah Petherbridge f8a83cbad9 Detect Collision Between Actors
* Move all collision code into the pkg/collision package.
  * pkg/doodads/collision.go -> pkg/collision/collide_level.go
  * pkg/doodads/collide_actors.go for new Actor collide support
* Add initial collision detection code between actors in Play Mode.
2019-04-15 19:17:25 -07:00

35 lines
780 B
Go

package collision
import (
"git.kirsle.net/apps/doodle/lib/render"
"git.kirsle.net/apps/doodle/pkg/log"
)
// IndexTuple holds two integers used as array indexes.
type IndexTuple [2]int
// BetweenBoxes checks if there is a collision between any
// two bounding rectangles.
//
// This returns a generator that spits out indexes of the
// intersecting boxes.
func BetweenBoxes(boxes []render.Rect) chan IndexTuple {
generator := make(chan IndexTuple)
go func() {
// Outer loop: test each box for intersection with the others.
for i, box := range boxes {
for j := i + 1; j < len(boxes); j++ {
if box.Intersects(boxes[j]) {
log.Info("Actor %d intersects %d", i, j)
generator <- IndexTuple{i, j}
}
}
}
close(generator)
}()
return generator
}