Noah Petherbridge
08e65c32b5
* Player character now experiences acceleration and friction when walking around the map! * Actor position and movement had to be converted from int's (render.Point) to float64's to support fine-grained acceleration steps. * Added "physics" package and physics.Vector to be a float64 counterpart for render.Point. Vector is used for uix.Actor.Position() for the sake of movement math. Vector is flattened back to a render.Point for collision purposes, since the levels and hitboxes are pixel-bound. * Refactor the uix.Actor to no longer extend the doodads.Drawing (so it can have a Position that's a Vector instead of a Point). This broke some code that expected `.Doodad` to directly reference the Drawing.Doodad: now you had to refer to it as `a.Drawing.Doodad` which was ugly. Added convenience method .Doodad() for a shortcut. * Moved functions like GetBoundingRect() from doodads package to collision, where it uses its own slimmer Actor interface for just the relevant methods it needs.
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package collision
|
|
|
|
import "git.kirsle.net/go/render"
|
|
|
|
// GetBoundingRect computes the full pairs of points for the bounding box of
|
|
// the actor.
|
|
//
|
|
// The X,Y coordinates are the position in the level of the actor,
|
|
// The W,H are the size of the actor's drawn box.
|
|
func GetBoundingRect(a Actor) render.Rect {
|
|
var (
|
|
P = a.Position()
|
|
S = a.Size()
|
|
)
|
|
return render.Rect{
|
|
X: P.X,
|
|
Y: P.Y,
|
|
W: S.W,
|
|
H: S.H,
|
|
}
|
|
}
|
|
|
|
// GetBoundingRectHitbox returns the bounding rect of the Actor taking into
|
|
// account their self-declared collision hitbox.
|
|
//
|
|
// The rect returned has the X,Y coordinate set to the actor's position, plus
|
|
// the X,Y of their hitbox, if any.
|
|
//
|
|
// The W,H of the rect is the W,H of their declared hitbox.
|
|
//
|
|
// If the actor has NOT declared its hitbox, this function returns exactly the
|
|
// same way as GetBoundingRect() does.
|
|
func GetBoundingRectHitbox(a Actor, hitbox render.Rect) render.Rect {
|
|
rect := GetBoundingRect(a)
|
|
if !hitbox.IsZero() {
|
|
rect.X += hitbox.X
|
|
rect.Y += hitbox.Y
|
|
rect.W = hitbox.W
|
|
rect.H = hitbox.H
|
|
}
|
|
return rect
|
|
}
|