diff --git a/include/vec2.h b/include/vec2.h index 5d135a2..7325a71 100644 --- a/include/vec2.h +++ b/include/vec2.h @@ -20,6 +20,7 @@ struct Vec2f { struct Vec2 vec2_add(struct Vec2, struct Vec2); struct Vec2f vec2f_add(struct Vec2f, struct Vec2f); struct Vec2 vec2_sub(struct Vec2, struct Vec2); +struct Vec2f vec2f_sub(struct Vec2f, struct Vec2f); struct Vec2 vec2_mul(struct Vec2, int scale); struct Vec2f vec2f_mul(struct Vec2f, int scale); struct Vec2 vec2f_vec2(struct Vec2f); diff --git a/src/camera.c b/src/camera.c index 8d8e203..bcd3ab2 100644 --- a/src/camera.c +++ b/src/camera.c @@ -11,6 +11,6 @@ struct Camera camera_new(struct Vec2f *target) { } void camera_update(struct Camera *c) { - c->pos = vec2f_lerp(c->pos, *c->target, 0.2); + c->pos = vec2f_lerp(c->pos, *c->target, 0.05); c->offset = vec2f_vec2(c->pos); } diff --git a/src/engine.c b/src/engine.c index 5729781..b4f1e9d 100644 --- a/src/engine.c +++ b/src/engine.c @@ -58,22 +58,25 @@ void engine_draw_map(struct Game const *game) { } /*draw the player*/ -void engine_draw_player(struct Game const *game) { - if(is_map_larger(game->map)) { - dframe(game->player->show_x * TILE_SIZE, - game->player->show_y * TILE_SIZE - 5, - game->player->anim.img); //draw the player 5 pixel up +void engine_draw_player(struct Game const *g) { + struct Vec2f draw_offset = vec2f_sub(g->player->pos_visual, vec2_vec2f(g->camera.offset)); + draw_offset.x += g->player->anim.dx * 3; + draw_offset.y += g->player->anim.dy * 3; + if(is_map_larger(g->map)) { + dframe(g->player->show_x * TILE_SIZE + draw_offset.x, + g->player->show_y * TILE_SIZE - 5 + draw_offset.y, + g->player->anim.img); //draw the player 5 pixel up } else { - const int offset_map_x = (DWIDTH / TILE_SIZE - game->map->w + 1)/2; - const int offset_map_y = (DHEIGHT / TILE_SIZE - game->map->h + 1)/2; + const int offset_map_x = (DWIDTH / TILE_SIZE - g->map->w + 1)/2; + const int offset_map_y = (DHEIGHT / TILE_SIZE - g->map->h + 1)/2; dframe( - (game->player->pos.x + offset_map_x) * TILE_SIZE + game->player->anim.dx*3, - (game->player->pos.y + offset_map_y) * TILE_SIZE - 5 + game->player->anim.dy*3, - game->player->anim.img); //draw the player 5 pixel up + (g->player->pos.x + offset_map_x) * TILE_SIZE + g->player->anim.dx*3, + (g->player->pos.y + offset_map_y) * TILE_SIZE - 5 + g->player->anim.dy*3, + g->player->anim.img); //draw the player 5 pixel up } - dprint(1,1,C_BLACK,"%d:%d",game->player->pos.x, game->player->pos.y); + dprint(1,1,C_BLACK,"%d:%d",g->player->pos.x, g->player->pos.y); } /*move the player to the direction*/ diff --git a/src/vec2.c b/src/vec2.c index b6f5bcb..9fc4459 100644 --- a/src/vec2.c +++ b/src/vec2.c @@ -18,6 +18,12 @@ vec2_sub(struct Vec2 v1, struct Vec2 v2) return VEC2(v1.x - v2.x, v1.y - v2.y); } +struct Vec2f +vec2f_sub(struct Vec2f v1, struct Vec2f v2) +{ + return VEC2F(v1.x - v2.x, v1.y - v2.y); +} + struct Vec2 vec2_mul(struct Vec2 v, int scale) {