diff --git a/game/actor.ts b/game/actor.ts index cdf1d894..b2a5ca8c 100644 --- a/game/actor.ts +++ b/game/actor.ts @@ -71,50 +71,56 @@ export class Actor { let cx = props.cx; let cy = props.cy; - // Adjust xr with velocity, check for collisions, etc. + let xr = props.xr; + let yr = props.yr; - // TODO: This code is somewhat wrong because it kinda assumes that we're - // the same size as our cell size, which is incorrect. - let xr = props.xr + velocity.x; - do { - // TODO: Cap velocity to 1 tile/frame? Then we wouldn't need this loop... - if (xr >= 0.7 && has_collision(level, cx + 1, cy)) { - xr = 0.7; - velocity.x = 0; - } - if (xr <= 0.3 && has_collision(level, cx - 1, cy)) { - xr = 0.3; - velocity.x = 0; - } - if (xr > 1) { - cx += 1; - xr -= 1; - } - if (xr < 0) { - cx -= 1; - xr += 1; - } - } while (xr > 1 || xr < 0); + const steps = Math.ceil( + (Math.abs(velocity.x) + Math.abs(velocity.y)) / 0.33 + ); + if (steps > 0) { + for (let n = 0; n < steps; n++) { + xr += velocity.x / steps; + if (velocity.x != 0) { + // Physics and whatnot. + if (xr >= 0.7 && has_collision(level, cx + 1, cy)) { + xr = 0.7; + velocity.x = 0; + } + if (xr <= 0.3 && has_collision(level, cx - 1, cy)) { + xr = 0.3; + velocity.x = 0; + } + } + while (xr > 1) { + xr--; + cx++; + } + while (xr < 0) { + xr++; + cx--; + } - let yr = props.yr + velocity.y; - do { - if (yr >= 0.4 && has_collision(level, cx, cy + 1)) { - yr = 0.4; - velocity.y = 0; + yr += velocity.y / steps; + if (velocity.y != 0) { + if (yr >= 0.4 && has_collision(level, cx, cy + 1)) { + yr = 0.4; + velocity.y = 0; + } + if (yr <= 0.1 && has_collision(level, cx, cy - 1)) { + yr = 0.1; + velocity.y = 0; + } + } + while (yr > 1) { + cy += 1; + yr -= 1; + } + while (yr < 0) { + cy -= 1; + yr += 1; + } } - if (yr <= 0.1 && has_collision(level, cx, cy - 1)) { - yr = 0.1; - velocity.y = 0; - } - if (yr > 1) { - cy += 1; - yr -= 1; - } - if (yr < 0) { - cy -= 1; - yr += 1; - } - } while (yr > 1 || yr < 0); + } // TODO: Entity collision detection