Noah Petherbridge
1f00af5741
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.
57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
package collision_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/collision"
|
|
"git.kirsle.net/go/render"
|
|
)
|
|
|
|
func TestActorOffset(t *testing.T) {
|
|
type testCase struct {
|
|
Actor *collision.MockActor
|
|
Offset render.Point
|
|
ExpectPoint render.Point
|
|
}
|
|
|
|
var tests = []testCase{
|
|
// Simple case where the hitbox == the size.
|
|
{
|
|
Actor: &collision.MockActor{
|
|
P: render.NewPoint(10, 10),
|
|
S: render.NewRect(32, 32),
|
|
HB: render.NewRect(32, 32),
|
|
},
|
|
ExpectPoint: render.NewPoint(10, 10),
|
|
},
|
|
|
|
// Bottom heavy actor
|
|
{
|
|
Actor: &collision.MockActor{
|
|
P: render.NewPoint(11, 22),
|
|
S: render.NewRect(32, 64),
|
|
HB: render.Rect{
|
|
X: 0,
|
|
Y: 32,
|
|
W: 32,
|
|
H: 32,
|
|
},
|
|
},
|
|
ExpectPoint: render.NewPoint(11, 22+32),
|
|
},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
offset := collision.NewActorOffset(test.Actor)
|
|
|
|
actualPoint := offset.Position()
|
|
if actualPoint != test.ExpectPoint {
|
|
t.Errorf("Test #%d: Position() expected to be %s but was %s",
|
|
i,
|
|
test.ExpectPoint,
|
|
actualPoint,
|
|
)
|
|
}
|
|
}
|
|
}
|