2019-04-16 02:12:25 +00:00
|
|
|
package collision
|
|
|
|
|
2019-05-07 05:57:32 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-12-23 02:21:58 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2019-05-07 05:57:32 +00:00
|
|
|
)
|
2019-04-16 02:12:25 +00:00
|
|
|
|
|
|
|
// CollisionBox holds all of the coordinate pairs to draw the collision box
|
|
|
|
// around a doodad.
|
|
|
|
type CollisionBox struct {
|
2019-05-07 05:57:32 +00:00
|
|
|
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,
|
|
|
|
})
|
2019-04-16 02:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCollisionBox returns a CollisionBox with the four coordinates.
|
|
|
|
func GetCollisionBox(box render.Rect) CollisionBox {
|
|
|
|
return CollisionBox{
|
2019-05-07 05:57:32 +00:00
|
|
|
Top: [2]render.Point{
|
2019-04-16 02:12:25 +00:00
|
|
|
{
|
|
|
|
X: box.X,
|
|
|
|
Y: box.Y,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
X: box.X + box.W,
|
|
|
|
Y: box.Y,
|
|
|
|
},
|
|
|
|
},
|
2019-05-07 05:57:32 +00:00
|
|
|
Bottom: [2]render.Point{
|
2019-04-16 02:12:25 +00:00
|
|
|
{
|
|
|
|
X: box.X,
|
|
|
|
Y: box.Y + box.H,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
X: box.X + box.W,
|
|
|
|
Y: box.Y + box.H,
|
|
|
|
},
|
|
|
|
},
|
2019-05-07 05:57:32 +00:00
|
|
|
Left: [2]render.Point{
|
2019-04-16 02:12:25 +00:00
|
|
|
{
|
|
|
|
X: box.X,
|
|
|
|
Y: box.Y + box.H - 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
X: box.X,
|
|
|
|
Y: box.Y + 1,
|
|
|
|
},
|
|
|
|
},
|
2019-05-07 05:57:32 +00:00
|
|
|
Right: [2]render.Point{
|
2019-04-16 02:12:25 +00:00
|
|
|
{
|
|
|
|
X: box.X + box.W,
|
|
|
|
Y: box.Y + box.H - 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
X: box.X + box.W,
|
|
|
|
Y: box.Y + 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 05:57:32 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|