Using more Vec2 to make lazy boi happy.

This commit is contained in:
KikooDX 2021-03-14 00:40:14 +01:00
parent 8bcce2348d
commit 5c60f8d65c
2 changed files with 23 additions and 14 deletions

3
.gitignore vendored
View File

@ -1,2 +1,5 @@
# build files are ):
build-cg
# go away binary, everyone hates you >:(
*.g3a

View File

@ -21,7 +21,7 @@ Vec2 search(tile_t x, tile_t level[16][16]) {
int collide_pixel(Vec2 pos, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) {
// Check if there's something in (x, y)
if(obj == level[pos.x][pos.y]) {
if(obj == level[pos.x / TILE_SIZE][pos.y / TILE_SIZE]) {
return 1;
}
else {
@ -30,16 +30,18 @@ int collide_pixel(Vec2 pos, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) {
}
int collide(Vec2 pos, int h, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) {
const int x = pos.x;
const int y = pos.y;
/* tl = top left
* br = bottom right
* avoid repetition later on ezier to work on */
const Vec2 pos_tl = (Vec2){pos.x + h, pos.y + h};
const Vec2 pos_br = (Vec2){pos.x + TILE_SIZE - h - 1, pos.y + TILE_SIZE - h - 1};
// Check if there's something in
// the square (x + 1, y + 1, x + 11, y + 11)
// The size of the hitbox changes with h
if( collide_pixel((Vec2){(x + h) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) ||
collide_pixel((Vec2){(x + h) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level) ||
collide_pixel((Vec2){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) ||
collide_pixel((Vec2){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level)
)
if(collide_pixel(pos_tl, obj, level) ||
collide_pixel(pos_br, obj, level) ||
collide_pixel((Vec2){pos_tl.x, pos_br.y}, obj, level) ||
collide_pixel((Vec2){pos_br.x, pos_tl.y}, obj, level))
{
return 1;
}
@ -93,17 +95,21 @@ int main(void) {
dimage(player.pos.x, player.pos.y, &img_player);
dupdate();
int mov_x = keydown(KEY_RIGHT) - keydown(KEY_LEFT);
int mov_y = keydown(KEY_DOWN) - keydown(KEY_UP);
/* if something has x and y, you probably want to use your Vec2 struct */
Vec2 mov = {
.x = keydown(KEY_RIGHT) - keydown(KEY_LEFT),
.y = keydown(KEY_DOWN) - keydown(KEY_UP)
};
clearevents();
// trying to move the player >w<
if(!collide((Vec2){player.pos.x + mov_x, player.pos.y}, 0, 1, level)) {
player.pos.x += mov_x;
if(!collide((Vec2){player.pos.x + mov.x, player.pos.y}, 0, 1, level)) {
player.pos.x += mov.x;
}
if(!collide((Vec2){player.pos.x, player.pos.y + mov_y}, 0, 1, level)) {
player.pos.y += mov_y;
if(!collide((Vec2){player.pos.x, player.pos.y + mov.y}, 0, 1, level)) {
player.pos.y += mov.y;
}
// d i e