Add typedef for tiles, use TwoValues in functions, add weird comments.

This commit is contained in:
KikooDX 2021-03-14 00:18:11 +01:00
parent 2e84858136
commit bfa9385841
4 changed files with 41 additions and 22 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# build files are ):
build-cg

View File

@ -12,7 +12,18 @@ struct Player {
struct TwoValues spawn;
};
struct TwoValues search(int x, int level[16][16]);
int collide_pixel(int x, int y, int obj, int level[16][16]);
int collide(int x, int y, int h, int obj, int level[16][16]);
struct Player level_reset(struct Player player);
/* using a typedef is what cool kids do at least i think
* you might no remember what `int x` is :(
* if x is declared with `tile_t x` you know x is some tile :)
* knowledge is power and cool
* as bonus it looks cool in function declaration
* cool stuff told u :D */
typedef int tile_t;
struct TwoValues search(tile_t x, tile_t level[16][16]);
/* here u can see i replaced x and y arguments with your twovalues struct
* u made smthing use it
* it will be handy in the future and makes better looking code */
int collide_pixel(struct TwoValues pos, tile_t obj, tile_t level[16][16]);
int collide(struct TwoValues pos, int h, tile_t obj, tile_t level[16][16]);
struct Player level_reset(struct Player player);

View File

@ -1,4 +1,8 @@
const int level[16][16] = {
#include "main.h"
/* see use tile_t cause these stuff are tiles
* would you believe it? */
const tile_t level[16][16] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 3, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1},
@ -15,4 +19,4 @@ const int level[16][16] = {
{1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
};

View File

@ -2,7 +2,7 @@
#include <gint/keyboard.h>
#include "main.h"
struct TwoValues search(int x, int level[16][16]) {
struct TwoValues search(tile_t x, tile_t level[16][16]) {
// Search for x in a given matrix.
// If x is found, return it coordinates
struct TwoValues coordinates = {0, 0};
@ -19,9 +19,9 @@ struct TwoValues search(int x, int level[16][16]) {
return coordinates;
}
int collide_pixel(int x, int y, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) {
int collide_pixel(struct TwoValues pos, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) {
// Check if there's something in (x, y)
if(obj == level[x][y]) {
if(obj == level[pos.x][pos.y]) {
return 1;
}
else {
@ -29,15 +29,18 @@ int collide_pixel(int x, int y, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) {
}
}
int collide(int x, int y, int h, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) {
int collide(struct TwoValues pos, int h, tile_t obj, tile_t level[LEVEL_SIZE][LEVEL_SIZE]) {
const int x = pos.x;
const int y = pos.y;
// 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((x + h) / TILE_SIZE, (y + h) / TILE_SIZE, obj, level) ||
collide_pixel((x + h) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE, obj, level) ||
collide_pixel((x + TILE_SIZE - h - 1) / TILE_SIZE, (y + h) / TILE_SIZE, obj, level) ||
collide_pixel((x + TILE_SIZE - h - 1) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE, obj, level)
) {
if( collide_pixel((struct TwoValues){(x + h) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) ||
collide_pixel((struct TwoValues){(x + h) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level) ||
collide_pixel((struct TwoValues){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + h) / TILE_SIZE}, obj, level) ||
collide_pixel((struct TwoValues){(x + TILE_SIZE - h - 1) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE}, obj, level)
)
{
return 1;
}
return 0;
@ -48,13 +51,12 @@ struct Player level_reset(struct Player player) {
return player;
}
int main(void)
{
int main(void) {
extern bopti_image_t img_player;
extern bopti_image_t img_wall;
extern bopti_image_t img_spike;
extern int level[LEVEL_SIZE][LEVEL_SIZE];
extern tile_t level[LEVEL_SIZE][LEVEL_SIZE];
int running = 1;
@ -96,20 +98,20 @@ int main(void)
clearevents();
// trying to move the player >w<
if(!collide(player.pos.x + mov_x, player.pos.y, 0, 1, level)) {
if(!collide((struct TwoValues){player.pos.x + mov_x, player.pos.y}, 0, 1, level)) {
player.pos.x += mov_x;
}
if(!collide(player.pos.x, player.pos.y + mov_y, 0, 1, level)) {
if(!collide((struct TwoValues){player.pos.x, player.pos.y + mov_y}, 0, 1, level)) {
player.pos.y += mov_y;
}
// d i e
if(collide(player.pos.x + mov_x, player.pos.y + mov_y, 2, 3, level)) {
if(collide(player.pos, 2, 3, level)) {
player = level_reset(player);
}
if(keydown(KEY_EXIT)) {running = 0;}
if(keydown(KEY_EXIT)) running = 0;
}
return 1;