doodle/pkg/collision/debug_box.go
Noah Petherbridge a2e1bd1ccb Improve OnCollide Doodad Script Handling
* Events.OnCollide now receives a CollideEvent object, which makes
  available the .Actor who collided and the .Overlap rect which is
  zero-relative to the target actor. Doodad scripts can use the .Overlap
  to see WHERE in their own box the other actor has intruded.
  * Update the LockedDoor and ElectricDoor doodads to detect when the
    player has entered their inner rect (since their doors are narrower
    than their doodad size)
  * Update the Button doodads to only press in when the player actually
    touches them (because their sizes are shorter than their doodad
    height)
  * Update the Trapdoor to only trigger its animation when the board
    along its top has been touched, not when the empty space below was
    touched from the bottom.
* Events.OnLeave now implemented and fires when an actor who was
  previously intersecting your doodad has left.
* The engine detects when an event JS callback returns false.
  Eventually, the OnCollide can return false to signify the collision is
  not accepted and the actor should be bumped away as if they hit solid
  geometry.
2019-05-06 22:58:09 -07:00

91 lines
1.7 KiB
Go

package collision
import (
"fmt"
"git.kirsle.net/apps/doodle/lib/render"
)
// CollisionBox holds all of the coordinate pairs to draw the collision box
// around a doodad.
type CollisionBox struct {
Top [2]render.Point
Bottom [2]render.Point
Left [2]render.Point
Right [2]render.Point
}
// NewBox creates a collision box from the Top Left and Bottom Right points.
func NewBox(topLeft, bottomRight render.Point) CollisionBox {
return GetCollisionBox(render.Rect{
X: topLeft.X,
Y: topLeft.Y,
W: bottomRight.X - topLeft.X,
H: bottomRight.Y - topLeft.Y,
})
}
// GetCollisionBox returns a CollisionBox with the four coordinates.
func GetCollisionBox(box render.Rect) CollisionBox {
return CollisionBox{
Top: [2]render.Point{
{
X: box.X,
Y: box.Y,
},
{
X: box.X + box.W,
Y: box.Y,
},
},
Bottom: [2]render.Point{
{
X: box.X,
Y: box.Y + box.H,
},
{
X: box.X + box.W,
Y: box.Y + box.H,
},
},
Left: [2]render.Point{
{
X: box.X,
Y: box.Y + box.H - 1,
},
{
X: box.X,
Y: box.Y + 1,
},
},
Right: [2]render.Point{
{
X: box.X + box.W,
Y: box.Y + box.H - 1,
},
{
X: box.X + box.W,
Y: box.Y + 1,
},
},
}
}
// String prints the bounds of the collision box in absolute terms.
func (c CollisionBox) String() string {
return fmt.Sprintf("CollisionBox<%s:%s>",
c.TopLeft(),
c.BottomRight(),
)
}
// TopLeft returns the point at the top left.
func (c CollisionBox) TopLeft() render.Point {
return render.NewPoint(c.Top[0].X, c.Top[0].Y)
}
// BottomRight returns the point at the bottom right.
func (c CollisionBox) BottomRight() render.Point {
return render.NewPoint(c.Bottom[1].X, c.Bottom[1].Y)
}