faster collisions
Squashed commit of the following: commit053c06ea29
Author: Alvie Rahman <alvierahman90@gmail.com> Date: Sat Dec 3 16:09:51 2022 +0000 begin documentation on codebase: sprite metadata commit435f0c6274
Author: Alvie Rahman <alvierahman90@gmail.com> Date: Sat Dec 3 16:05:37 2022 +0000 formatting, mostly commitf338476aaf
Author: Alvie Rahman <alvierahman90@gmail.com> Date: Fri Dec 2 23:18:27 2022 +0000 way faster collisions
This commit is contained in:
parent
ea60bdeede
commit
6e1690ceea
9
docs/res/sprites_metadata.c.txt
Normal file
9
docs/res/sprites_metadata.c.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
sprite.c:
|
||||||
|
|
||||||
|
struct Sprites
|
||||||
|
|
||||||
|
collision_offset = {
|
||||||
|
quad1x1, quad1y1, quad1x2, quad1y2,
|
||||||
|
quad2x1, quad2y1, quad2x2, quad2y2,
|
||||||
|
quad3x1, quad3y1, quad3x2, quad3y2
|
||||||
|
}
|
@ -16,59 +16,51 @@ Sprite sprites_info[] = {
|
|||||||
|
|
||||||
.bitmap_indexes[SI_UP] = 12, .bitmap_prop[SI_UP] = 0,
|
.bitmap_indexes[SI_UP] = 12, .bitmap_prop[SI_UP] = 0,
|
||||||
.collision_offset[SI_UP] = {
|
.collision_offset[SI_UP] = {
|
||||||
.itl = { .x = 3, .y = 8},
|
3, 8, 1, 7,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 3, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_DOWN] = 12, .bitmap_prop[SI_DOWN] = S_FLIPY,
|
.bitmap_indexes[SI_DOWN] = 12, .bitmap_prop[SI_DOWN] = S_FLIPY,
|
||||||
.collision_offset[SI_DOWN] = {
|
.collision_offset[SI_DOWN] = {
|
||||||
.itl = { .x = 3, .y = 8},
|
3, 2, 1, 1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 3, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_LEFT] = 11, .bitmap_prop[SI_LEFT] = 0,
|
.bitmap_indexes[SI_LEFT] = 11, .bitmap_prop[SI_LEFT] = 0,
|
||||||
.collision_offset[SI_LEFT] = {
|
.collision_offset[SI_LEFT] = {
|
||||||
.itl = { .x = 8, .y = 3},
|
8, 3, 7, 1,
|
||||||
.itr = { .x = 1, .y = 3},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_RIGHT] = 11, .bitmap_prop[SI_RIGHT] = S_FLIPX,
|
.bitmap_indexes[SI_RIGHT] = 11, .bitmap_prop[SI_RIGHT] = S_FLIPX,
|
||||||
.collision_offset[SI_RIGHT] = {
|
.collision_offset[SI_RIGHT] = {
|
||||||
.itl = { .x = 8, .y = 3},
|
2, 3, 1, 1,
|
||||||
.itr = { .x = 1, .y = 3},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_UP_RIGHT] = 13, .bitmap_prop[SI_UP_RIGHT] = 0,
|
.bitmap_indexes[SI_UP_RIGHT] = 13, .bitmap_prop[SI_UP_RIGHT] = 0,
|
||||||
.collision_offset[SI_UP_RIGHT] = {
|
.collision_offset[SI_UP_RIGHT] = {
|
||||||
.itl = { .x = 7, .y = 8},
|
2, 8, 1, 7,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 7, .y = 2},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 2},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_UP_LEFT] = 13, .bitmap_prop[SI_UP_LEFT] = S_FLIPX,
|
.bitmap_indexes[SI_UP_LEFT] = 13, .bitmap_prop[SI_UP_LEFT] = S_FLIPX,
|
||||||
.collision_offset[SI_UP_LEFT] = {
|
.collision_offset[SI_UP_LEFT] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8, 8, 7, 7,
|
||||||
.itr = { .x = 2, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 2},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 2, .y = 2},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_DOWN_LEFT] = 13, .bitmap_prop[SI_DOWN_LEFT] = S_FLIPX | S_FLIPY,
|
.bitmap_indexes[SI_DOWN_LEFT] = 13, .bitmap_prop[SI_DOWN_LEFT] = S_FLIPX | S_FLIPY,
|
||||||
.collision_offset[SI_DOWN_LEFT] = {
|
.collision_offset[SI_DOWN_LEFT] = {
|
||||||
.itl = { .x = 8, .y = 7},
|
8, 2, 7, 1,
|
||||||
.itr = { .x = 2, .y = 7},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 2, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_DOWN_RIGHT] = 13, .bitmap_prop[SI_DOWN_RIGHT] = S_FLIPY,
|
.bitmap_indexes[SI_DOWN_RIGHT] = 13, .bitmap_prop[SI_DOWN_RIGHT] = S_FLIPY,
|
||||||
.collision_offset[SI_DOWN_RIGHT] = {
|
.collision_offset[SI_DOWN_RIGHT] = {
|
||||||
.itl = { .x = 7, .y = 7},
|
2, 2, 1, 1,
|
||||||
.itr = { .x = 1, .y = 7},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 7, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -84,59 +76,51 @@ Sprite sprites_info[] = {
|
|||||||
|
|
||||||
.bitmap_indexes[SI_UP] = 14, .bitmap_prop[SI_UP] = 0,
|
.bitmap_indexes[SI_UP] = 14, .bitmap_prop[SI_UP] = 0,
|
||||||
.collision_offset[SI_UP] = {
|
.collision_offset[SI_UP] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_DOWN] = 15, .bitmap_prop[SI_DOWN] = 0,
|
.bitmap_indexes[SI_DOWN] = 15, .bitmap_prop[SI_DOWN] = 0,
|
||||||
.collision_offset[SI_DOWN] = {
|
.collision_offset[SI_DOWN] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_LEFT] = 14, .bitmap_prop[SI_LEFT] = 0,
|
.bitmap_indexes[SI_LEFT] = 14, .bitmap_prop[SI_LEFT] = 0,
|
||||||
.collision_offset[SI_LEFT] = {
|
.collision_offset[SI_LEFT] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_RIGHT] = 14, .bitmap_prop[SI_RIGHT] = S_FLIPX,
|
.bitmap_indexes[SI_RIGHT] = 14, .bitmap_prop[SI_RIGHT] = S_FLIPX,
|
||||||
.collision_offset[SI_RIGHT] = {
|
.collision_offset[SI_RIGHT] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_UP_RIGHT] = 14, .bitmap_prop[SI_UP_RIGHT] = 0,
|
.bitmap_indexes[SI_UP_RIGHT] = 14, .bitmap_prop[SI_UP_RIGHT] = 0,
|
||||||
.collision_offset[SI_UP_RIGHT] = {
|
.collision_offset[SI_UP_RIGHT] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_UP_LEFT] = 14, .bitmap_prop[SI_UP_LEFT] = S_FLIPX,
|
.bitmap_indexes[SI_UP_LEFT] = 14, .bitmap_prop[SI_UP_LEFT] = S_FLIPX,
|
||||||
.collision_offset[SI_UP_LEFT] = {
|
.collision_offset[SI_UP_LEFT] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_DOWN_LEFT] = 15, .bitmap_prop[SI_DOWN_LEFT] = 0,
|
.bitmap_indexes[SI_DOWN_LEFT] = 15, .bitmap_prop[SI_DOWN_LEFT] = 0,
|
||||||
.collision_offset[SI_DOWN_LEFT] = {
|
.collision_offset[SI_DOWN_LEFT] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_DOWN_RIGHT] = 15, .bitmap_prop[SI_DOWN_RIGHT] = S_FLIPX,
|
.bitmap_indexes[SI_DOWN_RIGHT] = 15, .bitmap_prop[SI_DOWN_RIGHT] = S_FLIPX,
|
||||||
.collision_offset[SI_DOWN_RIGHT] = {
|
.collision_offset[SI_DOWN_RIGHT] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -152,59 +136,51 @@ Sprite sprites_info[] = {
|
|||||||
|
|
||||||
.bitmap_indexes[SI_UP] = 25, .bitmap_prop[SI_UP] = 0,
|
.bitmap_indexes[SI_UP] = 25, .bitmap_prop[SI_UP] = 0,
|
||||||
.collision_offset[SI_UP] = {
|
.collision_offset[SI_UP] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_DOWN] = 22, .bitmap_prop[SI_DOWN] = 0,
|
.bitmap_indexes[SI_DOWN] = 22, .bitmap_prop[SI_DOWN] = 0,
|
||||||
.collision_offset[SI_DOWN] = {
|
.collision_offset[SI_DOWN] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_LEFT] = 21, .bitmap_prop[SI_LEFT] = 0,
|
.bitmap_indexes[SI_LEFT] = 21, .bitmap_prop[SI_LEFT] = 0,
|
||||||
.collision_offset[SI_LEFT] = {
|
.collision_offset[SI_LEFT] = {
|
||||||
.itl = { .x = 8, .y = 5},
|
8,5, 1,1,
|
||||||
.itr = { .x = 1, .y = 5},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_RIGHT] = 21, .bitmap_prop[SI_RIGHT] = S_FLIPX,
|
.bitmap_indexes[SI_RIGHT] = 21, .bitmap_prop[SI_RIGHT] = S_FLIPX,
|
||||||
.collision_offset[SI_RIGHT] = {
|
.collision_offset[SI_RIGHT] = {
|
||||||
.itl = { .x = 8, .y = 5},
|
8,5, 1,1,
|
||||||
.itr = { .x = 1, .y = 5},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_UP_RIGHT] = 23, .bitmap_prop[SI_UP_RIGHT] = 0,
|
.bitmap_indexes[SI_UP_RIGHT] = 23, .bitmap_prop[SI_UP_RIGHT] = 0,
|
||||||
.collision_offset[SI_UP_RIGHT] = {
|
.collision_offset[SI_UP_RIGHT] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_UP_LEFT] = 23, .bitmap_prop[SI_UP_LEFT] = S_FLIPX,
|
.bitmap_indexes[SI_UP_LEFT] = 23, .bitmap_prop[SI_UP_LEFT] = S_FLIPX,
|
||||||
.collision_offset[SI_UP_LEFT] = {
|
.collision_offset[SI_UP_LEFT] = {
|
||||||
.itl = { .x = 8, .y = 8},
|
8,8, 1,1,
|
||||||
.itr = { .x = 1, .y = 8},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_DOWN_LEFT] = 24, .bitmap_prop[SI_DOWN_LEFT] = S_FLIPX,
|
.bitmap_indexes[SI_DOWN_LEFT] = 24, .bitmap_prop[SI_DOWN_LEFT] = S_FLIPX,
|
||||||
.collision_offset[SI_DOWN_LEFT] = {
|
.collision_offset[SI_DOWN_LEFT] = {
|
||||||
.itl = { .x = 8, .y = 7},
|
8,7, 1,1,
|
||||||
.itr = { .x = 1, .y = 7},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
},
|
},
|
||||||
.bitmap_indexes[SI_DOWN_RIGHT] = 24, .bitmap_prop[SI_DOWN_RIGHT] = 0,
|
.bitmap_indexes[SI_DOWN_RIGHT] = 24, .bitmap_prop[SI_DOWN_RIGHT] = 0,
|
||||||
.collision_offset[SI_DOWN_RIGHT] = {
|
.collision_offset[SI_DOWN_RIGHT] = {
|
||||||
.itl = { .x = 8, .y = 7},
|
8,7, 1,1,
|
||||||
.itr = { .x = 1, .y = 7},
|
255, 255, 255, 255,
|
||||||
.ibl = { .x = 8, .y = 1},
|
255, 255, 255, 255
|
||||||
.ibr = { .x = 1, .y = 1},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -4,15 +4,15 @@
|
|||||||
#define MAX_PLAYER_COUNT 1
|
#define MAX_PLAYER_COUNT 1
|
||||||
|
|
||||||
#define GRAV_ACC 1
|
#define GRAV_ACC 1
|
||||||
#define TERM_VELY 3
|
#define TERM_VELY 2
|
||||||
#define TERM_VELX 2
|
#define TERM_VELX 2
|
||||||
#define TERM_VELY_DIVISOR 3
|
#define TERM_VELY_DIVISOR 5
|
||||||
#define TERM_VELX_DIVISOR 3
|
#define TERM_VELX_DIVISOR 3
|
||||||
|
|
||||||
#define JUMP_VEL 4
|
#define JUMP_VEL 4
|
||||||
#define JUMP_TIMEOUT_FRAMES 18
|
#define JUMP_TIMEOUT_FRAMES 18
|
||||||
|
|
||||||
#define DASH_VELL 7
|
#define DASH_VELL 4
|
||||||
// mfw when pythagoras irl :scream_cat:
|
// mfw when pythagoras irl :scream_cat:
|
||||||
#define DASH_VELD 5
|
#define DASH_VELD 5
|
||||||
#define DASH_TIMEOUT_FRAMES 30
|
#define DASH_TIMEOUT_FRAMES 30
|
||||||
|
10
src/main.c
10
src/main.c
@ -35,12 +35,10 @@ void main(void)
|
|||||||
|
|
||||||
|
|
||||||
memcpy(&(screen_sprites[0]), &(sprites_info[2]), sizeof(Sprite));
|
memcpy(&(screen_sprites[0]), &(sprites_info[2]), sizeof(Sprite));
|
||||||
memcpy(&(screen_sprites[1]), &(sprites_info[0]), sizeof(Sprite));
|
memcpy(&(screen_sprites[1]), &(sprites_info[1]), sizeof(Sprite));
|
||||||
memcpy(&(screen_sprites[2]), &(sprites_info[0]), sizeof(Sprite));
|
for (i = 2; i < NO_SCREEN_SPRITES; i++) memcpy(&(screen_sprites[i]), &(sprites_info[0]), sizeof(Sprite));
|
||||||
memcpy(&(screen_sprites[3]), &(sprites_info[0]), sizeof(Sprite));
|
|
||||||
memcpy(&(screen_sprites[4]), &(sprites_info[0]), sizeof(Sprite));
|
screen_sprites[4].has_joypad = 1;
|
||||||
memcpy(&(screen_sprites[5]), &(sprites_info[0]), sizeof(Sprite));
|
|
||||||
screen_sprites[1].has_joypad = 1;
|
|
||||||
|
|
||||||
for (i = 0; i < NO_SCREEN_SPRITES; i++) {
|
for (i = 0; i < NO_SCREEN_SPRITES; i++) {
|
||||||
screen_sprites[i].bitmap_index_prev = -1;
|
screen_sprites[i].bitmap_index_prev = -1;
|
||||||
|
145
src/sprite.c
145
src/sprite.c
@ -1,31 +1,15 @@
|
|||||||
#include <gb/gb.h>
|
#include <gb/gb.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "./vec.h"
|
#include "./vec.h"
|
||||||
#include "./constants.h"
|
#include "./constants.h"
|
||||||
#include "./flags.h"
|
#include "./flags.h"
|
||||||
#include "./map.h"
|
#include "./map.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define LYO 8
|
||||||
#define GET_TILE_INDEX_BY_COORD(x, y) ((((y) / PIXELS_PER_TILE) * 20) + ((x) / PIXELS_PER_TILE))
|
#define GET_TILE_INDEX_BY_COORD(x, y) ((((y) / PIXELS_PER_TILE) * 20) + ((x) / PIXELS_PER_TILE))
|
||||||
|
|
||||||
#define GET_SPRITE_CORNERS(sprite, sc) \
|
|
||||||
sc.itl.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].itl.x; \
|
|
||||||
sc.itl.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].itl.y - 8; \
|
|
||||||
sc.itr.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].itr.x; \
|
|
||||||
sc.itr.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].itr.y - 8; \
|
|
||||||
sc.ibl.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].ibl.x; \
|
|
||||||
sc.ibl.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].ibl.y - 8; \
|
|
||||||
sc.ibr.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].ibr.x; \
|
|
||||||
sc.ibr.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].ibr.y - 8;
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct SpriteCorners {
|
|
||||||
UVec itl;
|
|
||||||
UVec itr;
|
|
||||||
UVec ibl;
|
|
||||||
UVec ibr;
|
|
||||||
} SpriteCorners;
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct Sprites {
|
typedef struct Sprites {
|
||||||
int gb_sprite_index;
|
int gb_sprite_index;
|
||||||
int frames_since_last_dash;
|
int frames_since_last_dash;
|
||||||
@ -37,17 +21,14 @@ typedef struct Sprites {
|
|||||||
int has_diag_sprites;
|
int has_diag_sprites;
|
||||||
int has_joypad;
|
int has_joypad;
|
||||||
char name[20];
|
char name[20];
|
||||||
SpriteCorners collision_offset[8];
|
int collision_offset[8][12];
|
||||||
UVec pos;
|
UVec pos;
|
||||||
Vec vel;
|
Vec vel;
|
||||||
Vec acc;
|
Vec acc;
|
||||||
} Sprite;
|
} Sprite;
|
||||||
|
|
||||||
|
|
||||||
int get_tile_index_by_coord(unsigned int x, unsigned int y);
|
|
||||||
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc);
|
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc);
|
||||||
unsigned int sprite_internal_collision(Map *map, Sprite *sprite);
|
|
||||||
unsigned int sprite_collision(Map *map, Sprite *sprite);
|
|
||||||
void sprite_draw_to_screen(Sprite *sprite);
|
void sprite_draw_to_screen(Sprite *sprite);
|
||||||
void sprite_update_bitmap(Sprite *sprite);
|
void sprite_update_bitmap(Sprite *sprite);
|
||||||
|
|
||||||
@ -55,11 +36,10 @@ void sprite_update_bitmap(Sprite *sprite);
|
|||||||
// update a sprite for stuff that changes every frame
|
// update a sprite for stuff that changes every frame
|
||||||
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
|
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
|
||||||
{
|
{
|
||||||
int c, collision_check_steps, absvelx = abs(sprite->vel.x), absvely = abs(sprite->vel.y);
|
unsigned int colx, coly;
|
||||||
|
UVec original_position;
|
||||||
|
|
||||||
// target displacement is sprite->vel but may collide, giving actual displacment
|
// target displacement is sprite->vel but may collide, giving actual displacment
|
||||||
Vec displacement = {.x = 0, .y = 0};
|
|
||||||
UVec original_position;
|
|
||||||
|
|
||||||
if (sprite->frames_since_last_jump <= JUMP_TIMEOUT_FRAMES) sprite->frames_since_last_jump++;
|
if (sprite->frames_since_last_jump <= JUMP_TIMEOUT_FRAMES) sprite->frames_since_last_jump++;
|
||||||
if (sprite->frames_since_last_dash <= DASH_TIMEOUT_FRAMES) sprite->frames_since_last_dash++;
|
if (sprite->frames_since_last_dash <= DASH_TIMEOUT_FRAMES) sprite->frames_since_last_dash++;
|
||||||
@ -72,7 +52,7 @@ void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
|
|||||||
// player move left/right
|
// player move left/right
|
||||||
if ((joypad & J_RIGHT) && (sprite->vel.x < TERM_VELX)) sprite->acc.x = 1;
|
if ((joypad & J_RIGHT) && (sprite->vel.x < TERM_VELX)) sprite->acc.x = 1;
|
||||||
else if ((joypad & J_LEFT) && (sprite->vel.x > -TERM_VELX)) sprite->acc.x = -1;
|
else if ((joypad & J_LEFT) && (sprite->vel.x > -TERM_VELX)) sprite->acc.x = -1;
|
||||||
else sprite->acc.x = -sprite->vel.x/absvelx;
|
else sprite->acc.x = -sprite->vel.x/abs(sprite->vel.x);
|
||||||
sprite->vel.x += sprite->acc.x;
|
sprite->vel.x += sprite->acc.x;
|
||||||
|
|
||||||
// player dash
|
// player dash
|
||||||
@ -110,84 +90,59 @@ void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc)
|
|||||||
// slow down sprite in kind of realistic way
|
// slow down sprite in kind of realistic way
|
||||||
// (in real life i think it's drag is proportional to sprite->vel square but not
|
// (in real life i think it's drag is proportional to sprite->vel square but not
|
||||||
// sure if that is worth the compute yet cause everythign else is shit)
|
// sure if that is worth the compute yet cause everythign else is shit)
|
||||||
//if (sprite->vel.y > TERM_VELY) sprite->vel.y -= (absvely-TERM_VELY)/TERM_VELY_DIVISOR;
|
if (sprite->vel.y > TERM_VELY) sprite->vel.y = TERM_VELY;
|
||||||
//if (sprite->vel.y < -TERM_VELY) sprite->vel.y += (absvely-TERM_VELY)/TERM_VELY_DIVISOR;
|
if (sprite->vel.y < -TERM_VELY) sprite->vel.y += (abs(sprite->vel.y)-TERM_VELY)/TERM_VELY_DIVISOR;
|
||||||
//if (sprite->vel.x > TERM_VELX) sprite->vel.x -= (absvelx-TERM_VELX)/TERM_VELX_DIVISOR;
|
|
||||||
//if (sprite->vel.x < -TERM_VELX) sprite->vel.x += (absvelx-TERM_VELX)/TERM_VELX_DIVISOR;
|
|
||||||
//
|
|
||||||
//absvelx = abs(sprite->vel.x);
|
|
||||||
//absvely = abs(sprite->vel.y);
|
|
||||||
|
|
||||||
// if sprite not moving, no more logic needs to be done --- right?
|
int *offsets = sprite->collision_offset[sprite->bitmap_index];
|
||||||
if ((absvelx == 0) && (absvely == 0)) return;
|
// jump if on platform
|
||||||
|
if ((joypad & J_A) && (sprite->frames_since_last_jump > JUMP_TIMEOUT_FRAMES)) {
|
||||||
c = sprite_collision(map, sprite);
|
coly = map->data[GET_TILE_INDEX_BY_COORD(sprite->pos.x - offsets[0], sprite->pos.y - offsets[3]-LYO+1)];
|
||||||
|
if (!coly) coly += map->data[GET_TILE_INDEX_BY_COORD(sprite->pos.x - offsets[2], sprite->pos.y - offsets[3]-LYO+1)];
|
||||||
// don't let player move in a direction which they are already touching something
|
if(coly) {
|
||||||
if (((c & SC_TL) | (c & SC_TR)) && (sprite->vel.y < 0)) { sprite->vel.y = 0; }
|
|
||||||
if (((c & SC_BL) | (c & SC_BR)) && (sprite->vel.y > 0)) {
|
|
||||||
// player jump
|
|
||||||
if ((joypad & J_A) && (sprite->frames_since_last_jump > JUMP_TIMEOUT_FRAMES)) {
|
|
||||||
sprite->vel.y = -JUMP_VEL;
|
sprite->vel.y = -JUMP_VEL;
|
||||||
sprite->frames_since_last_jump = 0;
|
sprite->frames_since_last_jump = 0;
|
||||||
}
|
}
|
||||||
else sprite->vel.y = 0;
|
|
||||||
}
|
}
|
||||||
if (((c & SC_RT) | (c & SC_RB)) && (sprite->vel.x > 0)) { sprite->vel.x = 0; }
|
|
||||||
if (((c & SC_LT) | (c & SC_LB)) && (sprite->vel.x < 0)) { sprite->vel.x = 0; }
|
|
||||||
|
|
||||||
collision_check_steps = (absvelx > absvely) ? absvelx : absvely;
|
|
||||||
|
|
||||||
original_position.x = sprite->pos.x;
|
original_position.x = sprite->pos.x;
|
||||||
original_position.y = sprite->pos.y;
|
original_position.y = sprite->pos.y;
|
||||||
// check if following the path by the velocity will lead to a collision
|
// check if following the path by the velocity will lead to a collision
|
||||||
for (int step = collision_check_steps; step >= 0; step--) {
|
|
||||||
sprite->pos.x = original_position.x + ((step * sprite->vel.x)/collision_check_steps);
|
|
||||||
sprite->pos.y = original_position.y + ((step * sprite->vel.y)/collision_check_steps);
|
|
||||||
|
|
||||||
if (!sprite_internal_collision(map, sprite)) break;
|
// if no velocity component, pretend sprite has already collided
|
||||||
|
colx = sprite->vel.x == 0;
|
||||||
|
coly = sprite->vel.y == 0;
|
||||||
|
|
||||||
|
//for (int step = collision_check_steps; step >= 0; step--) {
|
||||||
|
if(sprite->vel.y != 0){
|
||||||
|
sprite->pos.y = original_position.y + sprite->vel.y;
|
||||||
|
|
||||||
|
if (sprite->vel.y < 0) {
|
||||||
|
coly += map->data[GET_TILE_INDEX_BY_COORD(sprite->pos.x - offsets[0], sprite->pos.y - offsets[1]-LYO)];
|
||||||
|
if (!coly) coly += map->data[GET_TILE_INDEX_BY_COORD(sprite->pos.x - offsets[2], sprite->pos.y - offsets[1]-LYO)];
|
||||||
|
} else if (sprite->vel.y > 0) {
|
||||||
|
coly += map->data[GET_TILE_INDEX_BY_COORD(sprite->pos.x - offsets[0], sprite->pos.y - offsets[3]-LYO)];
|
||||||
|
if (!coly) coly += map->data[GET_TILE_INDEX_BY_COORD(sprite->pos.x - offsets[2], sprite->pos.y - offsets[3]-LYO)];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (coly) sprite->pos.y = original_position.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(sprite->vel.x != 0) {
|
||||||
|
sprite->pos.x = original_position.x + sprite->vel.x;
|
||||||
|
|
||||||
|
if (sprite->vel.x < 0) {
|
||||||
|
colx += map->data[GET_TILE_INDEX_BY_COORD(sprite->pos.x - offsets[0], sprite->pos.y - offsets[1]-LYO)];
|
||||||
|
if(!colx) colx += map->data[GET_TILE_INDEX_BY_COORD(sprite->pos.x - offsets[0], sprite->pos.y - offsets[3]-LYO)];
|
||||||
|
} else if (sprite->vel.x > 0) {
|
||||||
|
colx += map->data[GET_TILE_INDEX_BY_COORD(sprite->pos.x - offsets[2], sprite->pos.y - offsets[1]-LYO)];
|
||||||
|
if(!colx) colx += map->data[GET_TILE_INDEX_BY_COORD(sprite->pos.x - offsets[2], sprite->pos.y - offsets[3]-LYO)];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (colx) sprite->pos.x = original_position.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprite_update_bitmap(sprite);
|
sprite_update_bitmap(sprite);
|
||||||
sprite_draw_to_screen(sprite);
|
sprite_draw_to_screen(sprite);
|
||||||
move_sprite(sprite->gb_sprite_index, sprite->pos.x, sprite->pos.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// calculate internal collisions
|
|
||||||
unsigned int sprite_internal_collision(Map *map, Sprite *sprite)
|
|
||||||
{
|
|
||||||
unsigned int rv = 0;
|
|
||||||
SpriteCorners sc;
|
|
||||||
GET_SPRITE_CORNERS(sprite, sc);
|
|
||||||
// (((y / PIXELS_PER_TILE) * 20) + (x / PIXELS_PER_TILE))
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.itl.x, sc.itl.y)]) rv = rv | SC_ITL;
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.itr.x, sc.itr.y)]) rv = rv | SC_ITR;
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibl.x, sc.ibl.y)]) rv = rv | SC_IBL;
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibr.x, sc.ibr.y)]) rv = rv | SC_IBR;
|
|
||||||
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// check for external collisions (if rectangle is at least touchign something)
|
|
||||||
unsigned int sprite_collision(Map *map, Sprite *sprite)
|
|
||||||
{
|
|
||||||
unsigned int rv = 0;
|
|
||||||
SpriteCorners sc;
|
|
||||||
GET_SPRITE_CORNERS(sprite, sc);
|
|
||||||
|
|
||||||
// check if corners are in a non 0 tile
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.itl.x, sc.itl.y-1)]) rv = rv | SC_TL;
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.itr.x, sc.itr.y-1)]) rv = rv | SC_TR;
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibl.x, sc.ibl.y+1)]) rv = rv | SC_BL;
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibr.x, sc.ibr.y+1)]) rv = rv | SC_BR;
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.itl.x-1, sc.itl.y )]) rv = rv | SC_LT;
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibl.x-1, sc.ibl.y )]) rv = rv | SC_LB;
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.itr.x+1, sc.itr.y )]) rv = rv | SC_RT;
|
|
||||||
if (map->data[GET_TILE_INDEX_BY_COORD(sc.ibr.x+1, sc.ibr.y )]) rv = rv | SC_RB;
|
|
||||||
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sprite_update_bitmap(Sprite *sprite)
|
void sprite_update_bitmap(Sprite *sprite)
|
||||||
@ -208,9 +163,11 @@ void sprite_update_bitmap(Sprite *sprite)
|
|||||||
|
|
||||||
void sprite_draw_to_screen(Sprite *sprite)
|
void sprite_draw_to_screen(Sprite *sprite)
|
||||||
{
|
{
|
||||||
if (sprite->bitmap_index_prev != sprite->bitmap_index) {
|
int bmi = sprite->bitmap_index;
|
||||||
set_sprite_tile(sprite->gb_sprite_index, sprite->bitmap_indexes[sprite->bitmap_index]);
|
|
||||||
set_sprite_prop(sprite->gb_sprite_index, sprite->bitmap_prop[sprite->bitmap_index]);
|
if (sprite->bitmap_index_prev != bmi) {
|
||||||
|
set_sprite_tile(sprite->gb_sprite_index, sprite->bitmap_indexes[bmi]);
|
||||||
|
set_sprite_prop(sprite->gb_sprite_index, sprite->bitmap_prop[bmi]);
|
||||||
}
|
}
|
||||||
move_sprite(sprite->gb_sprite_index, sprite->pos.x, sprite->pos.y);
|
move_sprite(sprite->gb_sprite_index, sprite->pos.x, sprite->pos.y);
|
||||||
}
|
}
|
||||||
|
@ -22,15 +22,13 @@ typedef struct Sprites {
|
|||||||
int has_diag_sprites;
|
int has_diag_sprites;
|
||||||
int has_joypad;
|
int has_joypad;
|
||||||
char name[20];
|
char name[20];
|
||||||
SpriteCorners collision_offset[8];
|
int collision_offset[8][12];
|
||||||
UVec pos;
|
UVec pos;
|
||||||
Vec vel;
|
Vec vel;
|
||||||
Vec acc;
|
Vec acc;
|
||||||
} Sprite;
|
} Sprite;
|
||||||
|
|
||||||
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc);
|
void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc);
|
||||||
unsigned int sprite_internal_collision(Map *map, Sprite *sprite);
|
|
||||||
unsigned int sprite_collision(Map *map, Sprite *sprite);
|
|
||||||
void sprite_draw_to_screen(Sprite *sprite);
|
void sprite_draw_to_screen(Sprite *sprite);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user