diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bf4933..7e95709 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(SOURCES src/entity/init.c src/entity/move.c src/wall/init.c + src/player/update.c src/player/init.c src/util.c ) diff --git a/include/player.h b/include/player.h index 4a85313..98e504c 100644 --- a/include/player.h +++ b/include/player.h @@ -9,4 +9,5 @@ struct Player { float rem_y; }; -void player_init(struct Player *restrict, int x, int y); +void player_init(struct Player *, int x, int y); +void player_update(struct Player *); diff --git a/include/wall.h b/include/wall.h index 402735c..9d1da51 100644 --- a/include/wall.h +++ b/include/wall.h @@ -5,4 +5,4 @@ struct Wall { struct Entity; }; -void wall_init(struct Wall *restrict, int x, int y, int width, int height); +void wall_init(struct Wall *, int x, int y, int width, int height); diff --git a/src/main.c b/src/main.c index 803e56f..d91f334 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,4 @@ #include "entity.h" -#include "layers.h" #include "player.h" #include "wall.h" #include @@ -11,7 +10,6 @@ main(void) struct Player player; struct Player newb; struct Wall walls[5]; - int collided = 0; player_init(&player, 16, 32); player_init(&newb, 64, 128); @@ -24,12 +22,9 @@ main(void) do { dclear(C_BLACK); entity_grid_draw_hitboxes(); - dprint(2, 2, C_WHITE, "%d", collided); dupdate(); clearevents(); - collided = entity_move(&player, L_SOLID, - keydown(KEY_RIGHT) - keydown(KEY_LEFT), - keydown(KEY_DOWN) - keydown(KEY_UP)); + player_update(&player); } while (!keydown(KEY_EXIT)); return; diff --git a/src/player/init.c b/src/player/init.c index ec1143c..369750a 100644 --- a/src/player/init.c +++ b/src/player/init.c @@ -3,7 +3,7 @@ #include void -player_init(struct Player *restrict p, int x, int y) +player_init(struct Player *p, int x, int y) { entity_init(p, x, y, 2, 2, 12, 12, L_PUSHABLE, C_BLUE); p->spd_x = 0.0; diff --git a/src/player/update.c b/src/player/update.c new file mode 100644 index 0000000..142ef6c --- /dev/null +++ b/src/player/update.c @@ -0,0 +1,34 @@ +#include "entity.h" +#include "layers.h" +#include "player.h" +#include + +void +player_update(struct Player *p) +{ + int collided; + const int dir_x = keydown(KEY_RIGHT) - keydown(KEY_LEFT); + const int dir_y = keydown(KEY_DOWN) - keydown(KEY_UP); + + p->spd_x += dir_x * 0.1; + p->spd_y += dir_y * 0.1; + + /* rem */ + const float spd_n_rem_x = p->spd_x + p->rem_x; + const int spd_x = spd_n_rem_x; + p->rem_x = spd_n_rem_x - spd_x; + const float spd_n_rem_y = p->spd_y + p->rem_y; + const int spd_y = spd_n_rem_y; + p->rem_y = spd_n_rem_y - spd_y; + + /* move */ + collided = entity_move(p, L_SOLID, spd_x, spd_y); + if (collided & 1) { + p->spd_x = 0.0; + p->rem_x = 0.0; + } + if (collided & 2) { + p->spd_y = 0.0; + p->rem_y = 0.0; + } +} diff --git a/src/wall/init.c b/src/wall/init.c index 2919467..4bb93fe 100644 --- a/src/wall/init.c +++ b/src/wall/init.c @@ -3,7 +3,7 @@ #include void -wall_init(struct Wall *restrict w, int x, int y, int width, int height) +wall_init(struct Wall *w, int x, int y, int width, int height) { entity_init(w, x, y, 0, 0, width, height, L_SOLID, C_DARK); }