diff --git a/src/comp/mechanical.c b/src/comp/mechanical.c index 4558357..546cb89 100644 --- a/src/comp/mechanical.c +++ b/src/comp/mechanical.c @@ -45,6 +45,15 @@ void mechanical_move(entity_t *e, vec2 direction, fixed_t dt, map_t const *map) fixed_t friction_x = fmul(fmul(-target.x, limits->friction), dt); fixed_t friction_y = fmul(fmul(-target.y, limits->friction), dt); + /* Limit maximum disruption (knockback) when it reaches extremes */ + fixed_t dv2 = fmul(m->vdx, m->vdx) + fmul(m->vdy, m->vdy); + fixed_t max_dv = limits->max_disruption_speed; + if(dv2 > fmul(max_dv, max_dv)) { + fixed_t dv = fsqrt(dv2); + m->vdx = fmul(fdiv(m->vdx, dv), max_dv); + m->vdy = fmul(fdiv(m->vdy, dv), max_dv); + } + /* Get there exponentially fast */ m->vx = target.x + friction_x + m->vdx; m->vy = target.y + friction_y + m->vdy; diff --git a/src/comp/mechanical.h b/src/comp/mechanical.h index 43aea25..bb24da2 100644 --- a/src/comp/mechanical.h +++ b/src/comp/mechanical.h @@ -25,6 +25,10 @@ typedef struct /* Dash duration (s) */ fixed_t dash_duration; + /* Maximum disruption speed, mainly avoids extreme knockback on players + when attacked for multiple sides at once */ + fixed_t max_disruption_speed; + } mechanical_limits_t; typedef struct diff --git a/src/enemies.c b/src/enemies.c index 43a1d44..4d19eb7 100644 --- a/src/enemies.c +++ b/src/enemies.c @@ -24,6 +24,7 @@ static enemy_t const slime_1 = { .limits = { .max_speed = fix(1), .friction = fix(0.6), + .max_disruption_speed = fix(999.0), }, .stats = { .HP = fix(1.4), @@ -44,6 +45,7 @@ static enemy_t const bat_2 = { .limits = { .max_speed = fix(1.8), .friction = fix(0.8), + .max_disruption_speed = fix(999.0), }, .stats = { .HP = fix(1.5), @@ -64,6 +66,7 @@ static enemy_t const fire_slime_4 = { .limits = { .max_speed = fix(1), .friction = fix(0.6), + .max_disruption_speed = fix(999.0), }, .stats = { .HP = fix(1.4), @@ -84,6 +87,7 @@ static enemy_t const gunslinger_8 = { .limits = { .max_speed = fix(1.8), .friction = fix(0.8), + .max_disruption_speed = fix(999.0), }, .stats = { .HP = fix(1.3), diff --git a/src/main.c b/src/main.c index 0509e9b..77b9313 100644 --- a/src/main.c +++ b/src/main.c @@ -93,6 +93,7 @@ int main(void) .friction = fix(0.7), .dash_speed = fix(20), .dash_duration = fix(1) / 8, + .max_disruption_speed = fix(3.0), }, .stat_model = { .HP = fix(1.5),