diff options
Diffstat (limited to 'test-client')
-rw-r--r-- | test-client/index.html | 2 | ||||
-rw-r--r-- | test-client/main.ts | 17 | ||||
-rw-r--r-- | test-client/movement.ts | 36 | ||||
-rw-r--r-- | test-client/protocol.ts | 2 |
4 files changed, 31 insertions, 26 deletions
diff --git a/test-client/index.html b/test-client/index.html index 5b0fb206..dfabbf59 100644 --- a/test-client/index.html +++ b/test-client/index.html @@ -20,7 +20,7 @@ <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> - <title>Hurry Curry - Test Client</title> + <title>Hurry Curry! Test Client</title> <script src="./main.js" type="module"></script> <style> body { diff --git a/test-client/main.ts b/test-client/main.ts index 30e67f67..d56cf7e6 100644 --- a/test-client/main.ts +++ b/test-client/main.ts @@ -17,7 +17,7 @@ */ /// <reference lib="dom" /> -import { MovementBase, update_movement } from "./movement.ts"; +import { MovementBase, collide_player_player, update_movement } from "./movement.ts"; import { Gamedata, ItemIndex, ItemLocation, Message, PacketC, PacketS, PlayerID, TileIndex } from "./protocol.ts"; import { V2, lerp_exp_v2_mut, normalize, lerp_exp } from "./util.ts"; import { draw_ingame, draw_wait } from "./visual.ts"; @@ -76,6 +76,7 @@ export interface PlayerData extends MovementBase { id: number, name: string, item?: ItemData, + direction: V2, character: number, anim_position: V2, message?: MessageData, @@ -145,6 +146,7 @@ function packet(p: PacketC) { rot: 0, facing: { x: 0, y: 1 }, vel: { x: 0, y: 0 }, + direction: { x: 0, y: 0 }, stamina: 0, boosting: false, }) @@ -294,7 +296,7 @@ function set_interact(edge: boolean) { function tick_update() { const p = players.get(my_id) if (!p) return - send({ type: "position", pos: [p.position.x, p.position.y], rot: p.rot, boosting: p.boosting }) + send({ type: "movement", pos: [p.position.x, p.position.y], direction: [p.direction.x, p.direction.y], boosting: p.boosting }) } function frame_update(dt: number) { @@ -303,12 +305,17 @@ function frame_update(dt: number) { if (time_remaining != null) time_remaining -= dt - const input = normalize({ + const direction = normalize({ x: (+keys_down.has(KEY_RIGHT) - +keys_down.has(KEY_LEFT)), y: (+keys_down.has(KEY_DOWN) - +keys_down.has(KEY_UP)) }) - if (interacting) input.x *= 0, input.y *= 0 - update_movement(p, dt, input, keys_down.has("KeyK")) + if (interacting) direction.x *= 0, direction.y *= 0 + p.direction = direction + update_movement(p, dt, direction, keys_down.has("KeyK")) + + for (const [_, a] of players) + for (const [_, b] of players) + collide_player_player(a, b, dt) const update_item = (item: ItemData) => { if (item.tracking) lerp_exp_v2_mut(item, item.tracking, dt * 10.) diff --git a/test-client/movement.ts b/test-client/movement.ts index 9102d7c2..1bbb9569 100644 --- a/test-client/movement.ts +++ b/test-client/movement.ts @@ -17,7 +17,7 @@ */ import { data } from "./main.ts"; -import { tiles, players } from "./main.ts"; +import { tiles } from "./main.ts"; import { V2, normalize, length, sub_v2, lerp_exp_v2_mut } from "./util.ts"; export const PLAYER_SIZE = 0.4 @@ -36,24 +36,23 @@ export interface MovementBase { 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.) +export function update_movement(p: MovementBase, dt: number, direction: V2, boost: boolean) { + if (length(direction) > 0.1) lerp_exp_v2_mut(p.facing, direction, dt * 10.) p.rot = Math.atan2(p.facing.x, p.facing.y) - boost &&= length(input) > 0.1 + 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 += input.x * dt * speed - p.vel.y += input.y * dt * speed + 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 collide_player(p, dt) lerp_exp_v2_mut(p.vel, { x: 0, y: 0 }, dt * PLAYER_FRICTION) } - -function collide_player(p: MovementBase, 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.position.x) + xo @@ -79,17 +78,16 @@ function collide_player(p: MovementBase, dt: number) { p.vel.y -= grad_y * vdotn } } - - for (const [_, player] of players) { - const diff = sub_v2(p.position, player.position) - const d = length(diff) - if (d < 0.01) continue - if (d >= PLAYER_SIZE * 2) continue - const norm = normalize(diff); - const f = 100 / (1 + d) - p.vel.x += norm.x * f * dt - p.vel.y += norm.y * f * dt - } +} +export function collide_player_player(a: MovementBase, b: MovementBase, dt: number) { + const diff = sub_v2(a.position, b.position) + const d = length(diff) + if (d < 0.01) return + 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 } export function aabb_point_distance( diff --git a/test-client/protocol.ts b/test-client/protocol.ts index f8241854..2a0f2b87 100644 --- a/test-client/protocol.ts +++ b/test-client/protocol.ts @@ -38,7 +38,7 @@ export interface Gamedata { export type PacketS = { type: "join", name: string, character: number } // Spawns your character. Dont send it to spectate. | { type: "leave" } // Despawns your character - | { type: "position", pos: Vec2, rot: number, boosting: boolean } // Update your position and rotation in radians (0 is -y) + | { type: "movement", pos: Vec2, direction: Vec2, boosting: boolean } | { type: "interact", pos?: Vec2 } // Interact with some tile. pos is a position when pressing and null when releasing interact button | { type: "communicate", message?: Message, persist: boolean } // Send a message | { type: "collide", player: PlayerID, force: Vec2 } // Apply force to another player as a result of a collision |