define sprite collision boxes in res, use in src/sprite.c

This commit is contained in:
Akbar Rahman 2022-06-09 15:51:27 +01:00
parent 32c2d32e6a
commit 3b186a3d75
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
5 changed files with 47 additions and 19 deletions

View File

@ -22,6 +22,8 @@
*/
#include "../src/sprite.h"
/* Start of tile array. */
unsigned char sprites[] =
{
@ -49,4 +51,20 @@ unsigned char sprites[] =
0x7E,0x7E,0x3C,0x3C,0xDB,0xDB,0x43,0xC3
};
// define the collision box of each sprite in the tileset
SpriteCorners sprite_offsets[] = {
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, },
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, },
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, },
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, },
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, },
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, },
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, },
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, },
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, },
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, },
{ .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }
};
/* End of SPRITES.C */

View File

@ -22,10 +22,12 @@
*/
#include "../src/sprite.h"
/* Bank of tiles. */
#define spritesBank 0
/* Start of tile array. */
extern unsigned char sprites[];
extern SpriteCorners sprite_offsets[];
/* End of SPRITES.H */

View File

@ -36,6 +36,7 @@ void main(void)
sprite.vel.y = 0;
sprite.acc.x = 0;
sprite.acc.y = 0;
sprite.collision_offset = sprite_offsets[0];
move_sprite(0, sprite.pos.x, sprite.pos.y);

View File

@ -6,16 +6,6 @@
#include "./map.h"
typedef struct Sprites {
int frames_since_last_dash;
int frames_since_last_jump;
int size;
UVec pos;
Vec vel;
Vec acc;
} Sprite;
typedef struct SpriteCorners {
UVec itl;
UVec itr;
@ -24,6 +14,16 @@ typedef struct SpriteCorners {
} SpriteCorners;
typedef struct Sprites {
int frames_since_last_dash;
int frames_since_last_jump;
SpriteCorners collision_offset;
UVec pos;
Vec vel;
Vec acc;
} Sprite;
int get_tile_index_by_coord(unsigned int x, unsigned int y);
void getSpriteCorners(Sprite *sprite, SpriteCorners *r);
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc);
@ -39,14 +39,14 @@ int get_tile_index_by_coord(unsigned int x, unsigned int y)
void getSpriteCorners(Sprite *sprite, SpriteCorners *sc)
{
sc->itl.x = sprite->pos.x-8;
sc->itl.y = sprite->pos.y-16;
sc->itr.x = sprite->pos.x-1;
sc->itr.y = sprite->pos.y-16;
sc->ibl.x = sprite->pos.x-8;
sc->ibl.y = sprite->pos.y-9 ;
sc->ibr.x = sprite->pos.x-1;
sc->ibr.y = sprite->pos.y-9;
sc->itl.x = sprite->pos.x - sprite->collision_offset.itl.x;
sc->itl.y = sprite->pos.y - sprite->collision_offset.itl.y - 8;
sc->itr.x = sprite->pos.x - sprite->collision_offset.itr.x;
sc->itr.y = sprite->pos.y - sprite->collision_offset.itr.y - 8;
sc->ibl.x = sprite->pos.x - sprite->collision_offset.ibl.x;
sc->ibl.y = sprite->pos.y - sprite->collision_offset.ibl.y - 8;
sc->ibr.x = sprite->pos.x - sprite->collision_offset.ibr.x;
sc->ibr.y = sprite->pos.y - sprite->collision_offset.ibr.y - 8;
}

View File

@ -4,10 +4,17 @@
#include "./vec.h"
#include "./map.h"
typedef struct SpriteCorners {
UVec itl;
UVec itr;
UVec ibl;
UVec ibr;
} SpriteCorners;
typedef struct Sprites {
int frames_since_last_dash;
int frames_since_last_jump;
int size;
SpriteCorners collision_offset;
UVec pos;
Vec vel;
Vec acc;