diff options
Diffstat (limited to 'test-client/movement.ts')
| -rw-r--r-- | test-client/movement.ts | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/test-client/movement.ts b/test-client/movement.ts index d3f30ae0..6d8d963c 100644 --- a/test-client/movement.ts +++ b/test-client/movement.ts @@ -27,30 +27,34 @@ export const BOOST_DURATION = 0.3 export const BOOST_RESTORE = 0.5 export interface MovementBase { + input_direction: V2, + input_boost: boolean, position: V2, - vel: V2, facing: V2, - rot: number, + rotation: number, + velocity: V2, boosting: boolean, stamina: number } -export function update_movement(p: MovementBase, dt: number, direction: V2, boost: boolean) { +export function update_movement(p: MovementBase, dt: number) { + const direction = p.input_direction; + let boost = p.input_boost; if (length(direction) > 0.05) lerp_exp_v2_mut(p.facing, direction, dt * 10.) if (length(direction) < 0.5) direction.x = direction.y = 0 - p.rot = Math.atan2(p.facing.x, p.facing.y) + p.rotation = Math.atan2(p.facing.x, p.facing.y) boost &&= length(direction) > 0.1 p.boosting = boost && (p.boosting || p.stamina >= 1) && p.stamina > 0 if (p.boosting) p.stamina -= dt / BOOST_DURATION else p.stamina += dt / BOOST_RESTORE p.stamina = Math.max(Math.min(p.stamina, 1), 0) const speed = PLAYER_SPEED * (p.boosting ? BOOST_FACTOR : 1) - p.vel.x += direction.x * dt * speed - p.vel.y += direction.y * dt * speed - p.position.x += p.vel.x * dt - p.position.y += p.vel.y * dt + p.velocity.x += direction.x * dt * speed + p.velocity.y += direction.y * dt * speed + p.position.x += p.velocity.x * dt + p.position.y += p.velocity.y * dt collide_player(p, dt) - lerp_exp_v2_mut(p.vel, { x: 0, y: 0 }, dt * PLAYER_FRICTION) + lerp_exp_v2_mut(p.velocity, { x: 0, y: 0 }, dt * PLAYER_FRICTION) } function collide_player(p: MovementBase, _dt: number) { for (let xo = -1; xo <= 1; xo++) { @@ -73,9 +77,9 @@ function collide_player(p: MovementBase, _dt: number) { p.position.x += (PLAYER_SIZE - d) * grad_x p.position.y += (PLAYER_SIZE - d) * grad_y - const vdotn = (grad_x * p.vel.x) + (grad_y * p.vel.y) - p.vel.x -= grad_x * vdotn - p.vel.y -= grad_y * vdotn + const vdotn = (grad_x * p.velocity.x) + (grad_y * p.velocity.y) + p.velocity.x -= grad_x * vdotn + p.velocity.y -= grad_y * vdotn } } } @@ -86,8 +90,8 @@ export function collide_player_player(a: MovementBase, b: MovementBase, dt: numb if (d >= PLAYER_SIZE * 2) return const norm = normalize(diff); const f = 100 / (1 + d) - a.vel.x += norm.x * f * dt - a.vel.y += norm.y * f * dt + a.velocity.x += norm.x * f * dt + a.velocity.y += norm.y * f * dt } export function aabb_point_distance( |