Let a program say what part of a sprite collides
Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m20s
akbasic CI Build / sanitizers (push) Failing after 4m31s
akbasic CI Build / coverage (push) Failing after 3m40s
akbasic CI Build / akgl_build (push) Failing after 22s
akbasic CI Build / mutation_test (push) Failing after 3m28s
Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m20s
akbasic CI Build / sanitizers (push) Failing after 4m31s
akbasic CI Build / coverage (push) Failing after 3m40s
akbasic CI Build / akgl_build (push) Failing after 22s
akbasic CI Build / mutation_test (push) Failing after 3m28s
`SPRHIT n, kind [,x1, y1, x2, y2]` gives a sprite a collision shape: a box, a circle inscribed in it, or a capsule. `RSPHIT(n, f)` reads it back in SPRHIT's own argument order, the way RSPRITE and RSPPOS already do, and needs no device because it answers from interpreter state. The rectangle is two corners measured from the sprite's top-left, in device pixels -- the same `x1, y1, x2, y2` that `BOX` and `SSHAPE` take. A dialect with two spellings for a rectangle is one nobody can write from memory. Omit it and the shape fits whatever the picture turned out to be, which is what a sprite loaded from a file needs: `SPRSAV "ship.png", 1` takes the image's own size and the program never learns what that was. **A sprite nobody has shaped collides with its whole frame, expansion bits included, exactly as before.** That is a promise rather than a convenience, and it has its own test: the same two sprites in the same two places, once with no SPRHIT and once with a four-pixel box, reporting a collision and then not. Named SPRHIT rather than SPRSHAPE because "shape" already means "a region SSHAPE saved" in this dialect, in this very chapter -- `SPRSAV A$, 1` takes one -- and a reader who typed `SPRSHAPE A$, 1` would have had every reason to. Both names, and RSPHIT, were grepped against every label in docs/, examples/ and both corpora first: a bare word is a label here, so a verb and a label share one namespace and taking a name a checked-in listing already uses would break it silently. `SPRHIT n, 0` takes a sprite out of collision while leaving it on the screen -- the ghost, the flashing invulnerable player, the pickup already taken. Hiding it with `SPRITE n, 0` stops it colliding too, and is what you want when it should not be seen either. The circle answers the complaint chapter 8 already ships a figure of. That figure shows two discs whose *boxes* touch at a corner while the artwork is nowhere near, and `BUMP(1)` reporting a collision; two `SPRHIT n, 2` and it stops. The test asserts both halves so the figure's caption stays true. `tests/verbs_table.c` caught RSPHIT filed after RSPPOS rather than before it, which is the sorted-table test doing exactly the job it exists for. Docs: a new section in chapter 8, rows in the verb and function references in alphabetical order, and chapter 13's "collision is by bounding box" becomes "collision is by shape" with the addition named. 111 with akgl, 110 without, and the artwork breakout still runs clean. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EwxGB6TdoVvZ11KQQME9cL
This commit is contained in:
@@ -491,7 +491,8 @@ static bool slot_collidable(akbasic_AkglSprites *state, int i)
|
||||
{
|
||||
return (state->actors[i] != NULL
|
||||
&& state->sprites[i] != NULL
|
||||
&& state->actors[i]->visible);
|
||||
&& state->actors[i]->visible
|
||||
&& state->shapekind[i] != AKBASIC_SHAPE_NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -509,6 +510,55 @@ static bool slot_collidable(akbasic_AkglSprites *state, int i)
|
||||
* sprite-against-sprite first, and taking the library's default would make
|
||||
* BUMP(1) report nothing at all, silently.
|
||||
*/
|
||||
/**
|
||||
* @brief Record the shape SPRHIT asked for. The next scan builds it.
|
||||
*
|
||||
* Nothing is built here, and the reason is worth stating: a frame-fitting shape
|
||||
* depends on the picture and the expansion bits, both of which may change after
|
||||
* this call and before the next scan, so deriving it now would be deriving it
|
||||
* from the wrong numbers. Zeroing the synced rectangle is what makes the next
|
||||
* scan rebuild rather than recognise the position as unchanged and skip.
|
||||
*/
|
||||
static akerr_ErrorContext *spr_shape(akbasic_SpriteBackend *self, int n, int kind,
|
||||
double x1, double y1, double x2, double y2)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_AkglSprites *state = NULL;
|
||||
int i = 0;
|
||||
|
||||
PASS(errctx, slot_of(self, n, &state, &i));
|
||||
state->shapekind[i] = kind;
|
||||
state->shapex1[i] = (float32_t)x1;
|
||||
state->shapey1[i] = (float32_t)y1;
|
||||
state->shapex2[i] = (float32_t)x2;
|
||||
state->shapey2[i] = (float32_t)y2;
|
||||
state->shapeexplicit[i] = (x2 > x1 && y2 > y1);
|
||||
memset(&state->syncedbox[i], 0, sizeof(state->syncedbox[i]));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The rectangle slot @p i collides with, in device pixels.
|
||||
*
|
||||
* Either what SPRHIT was given, offset to where the sprite is, or the whole
|
||||
* drawn frame -- which is what a sprite nobody has shaped has always collided
|
||||
* with, and is the default this must keep reproducing exactly.
|
||||
*/
|
||||
static void collision_box(akbasic_AkglSprites *state, int i, SDL_FRect *dest)
|
||||
{
|
||||
dest->x = state->actors[i]->x;
|
||||
dest->y = state->actors[i]->y;
|
||||
if ( state->shapeexplicit[i] ) {
|
||||
dest->x += state->shapex1[i];
|
||||
dest->y += state->shapey1[i];
|
||||
dest->w = state->shapex2[i] - state->shapex1[i];
|
||||
dest->h = state->shapey2[i] - state->shapey1[i];
|
||||
return;
|
||||
}
|
||||
dest->w = (float32_t)state->sprites[i]->width * (state->xexpand[i] ? 2.0f : 1.0f);
|
||||
dest->h = (float32_t)state->sprites[i]->height * (state->yexpand[i] ? 2.0f : 1.0f);
|
||||
}
|
||||
|
||||
static akerr_ErrorContext AKERR_NOIGNORE *sync_proxy(akbasic_AkglSprites *state, int i,
|
||||
SDL_FRect *box)
|
||||
{
|
||||
@@ -546,7 +596,28 @@ static akerr_ErrorContext AKERR_NOIGNORE *sync_proxy(akbasic_AkglSprites *state,
|
||||
if ( body.w <= 0.0f || body.h <= 0.0f ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
PASS(errctx, akgl_collision_shape_box(&state->shapes[i], &body, 0.0f));
|
||||
switch ( state->shapekind[i] ) {
|
||||
case AKBASIC_SHAPE_CIRCLE:
|
||||
/*
|
||||
* Inscribed, and in the *smaller* of the two half-extents. A circle that
|
||||
* fitted the larger one would poke out of the rectangle the program
|
||||
* asked for, which is the opposite of what "give this sprite a circle"
|
||||
* means to somebody looking at a disc drawn inside a square frame.
|
||||
*/
|
||||
PASS(errctx, akgl_collision_shape_circle(&state->shapes[i],
|
||||
body.w / 2.0f, body.h / 2.0f,
|
||||
(body.w < body.h ? body.w : body.h) / 2.0f,
|
||||
0.0f));
|
||||
break;
|
||||
case AKBASIC_SHAPE_CAPSULE_X:
|
||||
case AKBASIC_SHAPE_CAPSULE_Y:
|
||||
PASS(errctx, akgl_collision_shape_capsule(&state->shapes[i], &body,
|
||||
(uint8_t)state->shapekind[i], 0.0f));
|
||||
break;
|
||||
default:
|
||||
PASS(errctx, akgl_collision_shape_box(&state->shapes[i], &body, 0.0f));
|
||||
break;
|
||||
}
|
||||
state->shapes[i].collidemask = AKGL_COLLISION_LAYER_ACTOR | AKGL_COLLISION_LAYER_STATIC;
|
||||
PASS(errctx, akgl_collision_proxy_sync(state->proxies[i], &state->shapes[i],
|
||||
box->x, box->y, 0.0f));
|
||||
@@ -576,10 +647,7 @@ static akerr_ErrorContext *spr_collisions(akbasic_SpriteBackend *self, uint16_t
|
||||
if ( !live[i] ) {
|
||||
continue;
|
||||
}
|
||||
box[i].x = state->actors[i]->x;
|
||||
box[i].y = state->actors[i]->y;
|
||||
box[i].w = (float32_t)state->sprites[i]->width * (state->xexpand[i] ? 2.0f : 1.0f);
|
||||
box[i].h = (float32_t)state->sprites[i]->height * (state->yexpand[i] ? 2.0f : 1.0f);
|
||||
collision_box(state, i, &box[i]);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -687,6 +755,13 @@ akerr_ErrorContext *akbasic_sprite_init_akgl(akbasic_SpriteBackend *obj, akbasic
|
||||
for ( i = 0; i < AKBASIC_MAX_SPRITES; i++ ) {
|
||||
SDL_FRect placeholder = { 0.0f, 0.0f, 1.0f, 1.0f };
|
||||
|
||||
/*
|
||||
* A box fitting the frame, which is what every sprite collided with
|
||||
* before SPRHIT existed and is what one nobody has shaped must go on
|
||||
* collided with.
|
||||
*/
|
||||
state->shapekind[i] = AKBASIC_SHAPE_BOX;
|
||||
|
||||
PASS(errctx, akgl_collision_shape_box(&state->shapes[i], &placeholder, 0.0f));
|
||||
/*
|
||||
* Acquire and initialize adjacent, with nothing between them: the slot
|
||||
@@ -708,6 +783,7 @@ akerr_ErrorContext *akbasic_sprite_init_akgl(akbasic_SpriteBackend *obj, akbasic
|
||||
obj->configure = spr_configure;
|
||||
obj->shared_colors = spr_shared_colors;
|
||||
obj->collisions = spr_collisions;
|
||||
obj->shape = spr_shape;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user