doodle/pkg/collision/actors_test.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

126 lines
1.9 KiB
Go

package collision_test
import (
"testing"
"git.kirsle.net/SketchyMaze/doodle/pkg/collision"
"git.kirsle.net/go/render"
)
func TestActorCollision(t *testing.T) {
boxes := []render.Rect{
// 0: intersects with 1
// Expected intersection rect would be
// X,Y = 90,10
// X2,Y2 = 100,99
{
X: 0,
Y: 0,
W: 100,
H: 100,
},
// 1: intersects with 0
// Expected intersection rect would be
// X,Y = 90,10
// X2,Y2 = 100,99
{
X: 90,
Y: 10,
W: 100,
H: 100,
},
// 2: no intersection
{
X: 200,
Y: 200,
W: 32,
H: 32,
},
// 3: intersects with 4
// Expected intersection rect would be
// X,Y = 240,200
// X2,Y2 = 264,231
{
X: 233,
Y: 200,
W: 32,
H: 32,
},
// 4: intersects with 3
{
X: 240,
Y: 200,
W: 32,
H: 32,
},
// 5: completely contains 6 and intersects 7.
{
X: 300,
Y: 300,
W: 1000,
H: 600,
},
{
X: 450,
Y: 500,
W: 42,
H: 42,
},
{
X: 1200,
Y: 350,
W: 512,
H: 512,
},
}
assert := func(i int, result collision.BoxCollision, expectA, expectB int) {
if result.A != expectA || result.B != expectB {
t.Errorf(
"unexpected collision at index %d of BetweenBoxes() generator\n"+
"expected: (%d,%d)\n"+
" but got: (%d,%d)",
i,
expectA, expectB,
result.A, result.B,
)
}
}
var i int
for overlap := range collision.BetweenBoxes(boxes) {
a, b := overlap.A, overlap.B
// Ensure expected collisions happened.
switch i {
case 0:
assert(i, overlap, 0, 1)
case 1:
assert(i, overlap, 1, 0)
case 2:
assert(i, overlap, 3, 4)
case 3:
assert(i, overlap, 4, 3)
case 4:
assert(i, overlap, 5, 6)
case 5:
assert(i, overlap, 5, 7)
case 6:
assert(i, overlap, 6, 5)
case 7:
assert(i, overlap, 7, 5)
default:
t.Errorf("got unexpected collision result, index %d, tuple (%d,%d)",
i, a, b,
)
}
i++
}
}