From c0dcb5b226c3ca29e8808dc85f8b0a857975bc7b Mon Sep 17 00:00:00 2001 From: Massena Date: Thu, 23 Dec 2021 17:43:44 +0100 Subject: [PATCH] colli*bonk* --- include/conf.h | 4 ++-- include/level.h | 4 +++- include/player.h | 1 + src/level.c | 17 +++++++++++++++- src/player.c | 53 ++++++++++++++++++++++++++++++++++++++++-------- 5 files changed, 66 insertions(+), 13 deletions(-) diff --git a/include/conf.h b/include/conf.h index 5525cb5..9731829 100644 --- a/include/conf.h +++ b/include/conf.h @@ -2,8 +2,8 @@ #define PLAYER_S 12 -#define PLAYER_ACC 1 -#define PLAYER_FRIC 0.2 +#define PLAYER_ACC 2 +#define PLAYER_FRIC 0.4 #define PLAYER_JUMP 7 #define PLAYER_COYOTE 3 #define GRAVITY 0.4 diff --git a/include/level.h b/include/level.h index e568f9f..4a1ee47 100644 --- a/include/level.h +++ b/include/level.h @@ -3,9 +3,11 @@ #include "type.h" struct Level { - int w, h, s; /* weigth, height, size */ + int w, h, s; /* width, height, size */ tile_t *data; }; void level_init(void); void level_draw(void); +tile_t level_get_px(int x, int y); +tile_t level_get_tile(int x, int y); diff --git a/include/player.h b/include/player.h index c84d80d..8457f22 100644 --- a/include/player.h +++ b/include/player.h @@ -7,5 +7,6 @@ struct Player { struct Vec2 spawn; }; +void player_init(void); void player_update(void); void player_draw(void); diff --git a/src/level.c b/src/level.c index 18c2f0a..593947a 100644 --- a/src/level.c +++ b/src/level.c @@ -1,5 +1,6 @@ #include "level.h" #include "conf.h" +#include "player.h" #include #include #include @@ -20,6 +21,8 @@ level_init(void) level.s = level.w * level.h; level_free(); level_load(); + + player_init(); } void @@ -27,7 +30,7 @@ level_draw(void) { for (int m = 0; m < level.h; ++m) { for (int n = 0; n < level.w; ++n) { - tile_t tile = level.data[n + m * level.w]; + tile_t tile = level_get_tile(n, m); dsubimage(n * TILE_S, m * TILE_S, &img_tileset, ((tile - 1) % TILESET_W) * TILE_S, ((tile - 1) / TILESET_W) * TILE_S, TILE_S, @@ -38,6 +41,18 @@ level_draw(void) } } +tile_t +level_get_px(int x, int y) +{ + return level.data[x / TILE_S + y / TILE_S * level.w]; +} + +tile_t +level_get_tile(int x, int y) +{ + return level.data[x + y * level.w]; +} + static void level_free(void) { diff --git a/src/player.c b/src/player.c index ee3a0aa..dd88087 100644 --- a/src/player.c +++ b/src/player.c @@ -2,18 +2,20 @@ #include "conf.h" #include "draw.h" #include "input.h" +#include "level.h" #include "tools.h" static int k_up, k_down, k_left, k_right, k_jump; static struct Player player; static void player_get_input(void); static void player_move(struct Vec2 mov); -static int player_collide(void); +static int player_collide_pixel(int x, int y, tile_t id); +static int player_collide(struct Vec2 pos, tile_t id, int h); void player_init(void) { - player.pos = (struct Vec2){0, 0}; + player.pos = (struct Vec2){160, 100}; player.spd = (struct FVec2){0.0f, 0.0f}; } @@ -30,6 +32,7 @@ void player_draw(void) { draw_rectangle(player.pos.x, player.pos.y, PLAYER_S, PLAYER_S, C_BLACK); + dprint(10, 10, C_BLACK, "%d", player_collide(player.pos, 1, 0)); } static void @@ -45,7 +48,7 @@ player_get_input(void) static void player_move(struct Vec2 mov) { - if (player_collide()) { + if (player_collide(player.pos, 1, 0)) { return; } @@ -62,9 +65,9 @@ player_move(struct Vec2 mov) const int sign_x = sign(player.spd.x); const int Ispd_x = player.spd.x; player.pos.x += Ispd_x; - if (player_collide()) { + if (player_collide(player.pos, 1, 0)) { player.spd.x = 0.0f; - while (player_collide()) { + while (player_collide(player.pos, 1, 0)) { player.pos.x -= sign_x; } } @@ -72,17 +75,49 @@ player_move(struct Vec2 mov) const int sign_y = sign(player.spd.y); const int Ispd_y = player.spd.y; player.pos.y += Ispd_y; - if (player_collide()) { + if (player_collide(player.pos, 1, 0)) { player.spd.y = 0.0f; - while (player_collide()) { + while (player_collide(player.pos, 1, 0)) { player.pos.y -= sign_y; } } } static int -player_collide(void) +player_collide_pixel(int x, int y, tile_t id) { - /* Je ne fais qu'acte de présence, rien de plus */ + const tile_t tile = level_get_px(x, y); + + if (!tile) { + return 0; + } + + if (id == 1) { + return (tile - 1) % TILESET_W < 8; + } + + return tile == id; +} + +static int +player_collide(struct Vec2 pos, tile_t id, int h) +{ + + const struct Vec2 pos_tl = (struct Vec2){pos.x + h, pos.y + h}; + const struct Vec2 pos_br = + (struct Vec2){pos.x + PLAYER_S - h - 1, pos.y + PLAYER_S - h - 1}; + const struct Vec2 middle = + (struct Vec2){pos.x + PLAYER_S / 2, pos.y + PLAYER_S / 2}; + + if (player_collide_pixel(pos_tl.x, pos_tl.y, id) || + player_collide_pixel(pos_br.x, pos_br.y, id) || + player_collide_pixel(pos_tl.x, pos_br.y, id) || + player_collide_pixel(pos_br.x, pos_tl.y, id) || + player_collide_pixel(middle.x, pos_tl.y, id) || + player_collide_pixel(middle.x, pos_br.y, id) || + player_collide_pixel(pos_tl.x, middle.y, id) || + player_collide_pixel(pos_br.x, middle.y, id)) { + return 1; + } return 0; }