doodle/pkg/collision/actor_offset.go
Noah Petherbridge 1f00af5741 Collision Detection Fix + Doodad CLI Fixes
Fix collision detection when an actor's hitbox is offset from 0,0:

* Actors having a hitbox that didn't begin at X,Y 0,0 used to experience
  clipping issues with level geometry: because the game tracks their Position
  (top left corner of their graphical sprite) and their target position wasn't
  being correctly offset by their hitbox offset.
* To resolve the issue, an "ActorOffset" struct is added: you give it the
  original game's Actor (with its offset hitbox) and it will record the offset
  and give a mocked Actor for collision detection purposes: where the Position
  and Target can be offset and where its Hitbox claims to begin at 0,0 matching
  its offsetted Position.
* The translation between your original Actor and Offset Actor is handled at the
  boundary of the CollidesWithGrid function, so the main algorithm didn't need
  to be messed with and the game itself doesn't need to care about the offset.

Make some fixes to the doodad CLI tool:

* Fix palette colors being duplicated/doubled when converting from an image.
* The --palette flag in `doodad convert` now actually functions: so you can
  supply an initial palette.json with colors and attributes to e.g. mark which
  colors should be solid or fire and give them names. The palette.json doesn't
  need to be comprehensive: it will be extended with new distinct colors as
  needed during the conversion.
2024-05-27 15:14:00 -07:00

82 lines
2.2 KiB
Go

package collision
import "git.kirsle.net/go/render"
// ActorOffset helps normalize an actor's Position and Hitbox for collision detection.
//
// It allows for an actor to have a Hitbox which is offset from the 0,0 coordinate
// in the top left corner. During gameplay, the actor's Position (top left corner of
// its *sprite*) is what the game tracks for movement, but if the actor's declared hitbox
// doesn't encompass the point 0,0 it used to lead to collision bugs.
//
// ActorOffset will take your original Actor, compute the offset between its Position
// and its Hitbox, and return a simplified Actor that pretends the hitbox begins at 0,0.
type ActorOffset struct {
d Actor
offset render.Point
}
// NewActorOffset consumes the game's original Actor and returns one that simplifies
// the Hitbox boundary.
func NewActorOffset(d Actor) *ActorOffset {
// Compute the offset from the actor's Position to its Hitbox.
var (
position = d.Position()
hitbox = d.Hitbox()
delta = render.Point{
X: position.X + hitbox.X,
Y: position.Y + hitbox.Y,
}
offset = render.Point{
X: delta.X - position.X,
Y: delta.Y - position.Y,
}
)
return &ActorOffset{
d: d,
offset: offset,
}
}
// Offset returns the offset from the source actor's Position to their new one.
func (ao *ActorOffset) Offset() render.Point {
return ao.offset
}
// Position will be the actor's original world position (of its sprite) plus the
// hitbox offset coordinate.
func (ao *ActorOffset) Position() render.Point {
var P = ao.d.Position()
return render.Point{
X: P.X + ao.offset.X,
Y: P.Y + ao.offset.Y,
}
}
// Size is the same as your original Actor.
func (ao *ActorOffset) Size() render.Rect {
return ao.d.Size()
}
// Hitbox returns the actor's original Hitbox but where the X,Y are locked to 0,0.
// The W,H of the hitbox is the same as original.
func (ao *ActorOffset) Hitbox() render.Rect {
var HB = ao.d.Hitbox()
return render.Rect{
X: 0,
Y: 0,
H: HB.H,
W: HB.W,
}
}
// Grounded returns your original actor's value.
func (ao *ActorOffset) Grounded() bool {
return ao.d.Grounded()
}
// SetGrounded sets the grounded state of your original actor.
func (ao *ActorOffset) SetGrounded(v bool) {
ao.d.SetGrounded(v)
}