diff options
Diffstat (limited to 'test-client/movement.ts')
-rw-r--r-- | test-client/movement.ts | 55 |
1 files changed, 37 insertions, 18 deletions
diff --git a/test-client/movement.ts b/test-client/movement.ts index 38f4b47b..933b6af5 100644 --- a/test-client/movement.ts +++ b/test-client/movement.ts @@ -16,43 +16,62 @@ */ import { data } from "./main.ts"; -import { tiles, players, PlayerData } from "./main.ts"; +import { tiles, players } from "./main.ts"; import { V2, normalize, length, sub_v2, lerp_exp_v2_mut } from "./util.ts"; -export const PLAYER_SIZE = 0.4; -export const PLAYER_SPEED = 65; +export const PLAYER_SIZE = 0.4 +export const PLAYER_FRICTION = 10 +export const PLAYER_SPEED = 40 +export const BOOST_FACTOR = 3 +export const BOOST_DURATION = 0.3 +export const BOOST_RESTORE = 0.5 -export function player_movement_update(p: PlayerData, dt: number, input: V2) { +export interface MovementBase { + position: V2, + vel: V2, + facing: V2, + rot: number, + boosting: boolean, + stamina: number +} + +export function update_movement(p: MovementBase, dt: number, input: V2, boost: boolean) { if (length(input) > 0.1) lerp_exp_v2_mut(p.facing, input, dt * 10.) p.rot = Math.atan2(p.facing.x, p.facing.y) - p.vel.x += input.x * dt * PLAYER_SPEED - p.vel.y += input.y * dt * PLAYER_SPEED - p.x += p.vel.x * dt - p.y += p.vel.y * dt + boost &&= length(input) > 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 += input.x * dt * speed + p.vel.y += input.y * dt * speed + p.position.x += p.vel.x * dt + p.position.y += p.vel.y * dt collide_player(p, dt) - lerp_exp_v2_mut(p.vel, { x: 0, y: 0 }, dt * 15.) + lerp_exp_v2_mut(p.vel, { x: 0, y: 0 }, dt * PLAYER_FRICTION) } -function collide_player(p: PlayerData, dt: number) { +function collide_player(p: MovementBase, dt: number) { for (let xo = -1; xo <= 1; xo++) { for (let yo = -1; yo <= 1; yo++) { - const x = Math.floor(p.x) + xo - const y = Math.floor(p.y) + yo + const x = Math.floor(p.position.x) + xo + const y = Math.floor(p.position.y) + yo const tile = tiles.get([x, y].toString()) if (tile && !data.tile_collide[tile.kind]) continue - const d = aabb_point_distance(x, y, x + 1, y + 1, p.x, p.y) + const d = aabb_point_distance(x, y, x + 1, y + 1, p.position.x, p.position.y) if (d > PLAYER_SIZE) continue const h = 0.01 - const d_sample_x = aabb_point_distance(x, y, x + 1, y + 1, p.x + h, p.y) - const d_sample_y = aabb_point_distance(x, y, x + 1, y + 1, p.x, p.y + h) + const d_sample_x = aabb_point_distance(x, y, x + 1, y + 1, p.position.x + h, p.position.y) + const d_sample_y = aabb_point_distance(x, y, x + 1, y + 1, p.position.x, p.position.y + h) const grad_x = (d_sample_x - d) / h const grad_y = (d_sample_y - d) / h - p.x += (PLAYER_SIZE - d) * grad_x - p.y += (PLAYER_SIZE - d) * grad_y + 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 @@ -61,7 +80,7 @@ function collide_player(p: PlayerData, dt: number) { } for (const [_, player] of players) { - const diff = sub_v2(p, player) + const diff = sub_v2(p.position, player.position) const d = length(diff) if (d < 0.01) continue if (d >= PLAYER_SIZE * 2) continue |