diff options
author | metamuffin <metamuffin@disroot.org> | 2024-06-20 14:42:25 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-06-23 19:24:14 +0200 |
commit | 96dd53c9ea8fe124a699de3b04069102a6002128 (patch) | |
tree | ba978eae3518dd8f60e061d2c40f5e4478f987da | |
parent | d888bd25908ff26712232f1b296f902d37d93d40 (diff) | |
download | hurrycurry-96dd53c9ea8fe124a699de3b04069102a6002128.tar hurrycurry-96dd53c9ea8fe124a699de3b04069102a6002128.tar.bz2 hurrycurry-96dd53c9ea8fe124a699de3b04069102a6002128.tar.zst |
client-side collision
-rw-r--r-- | test-client/main.ts | 17 | ||||
-rw-r--r-- | test-client/protocol.ts | 2 | ||||
-rw-r--r-- | test-client/util.ts | 1 |
3 files changed, 16 insertions, 4 deletions
diff --git a/test-client/main.ts b/test-client/main.ts index 9cafa1f6..a3d5d2f2 100644 --- a/test-client/main.ts +++ b/test-client/main.ts @@ -1,7 +1,7 @@ /// <reference lib="dom" /> import { Gamedata, ItemIndex, Message, PacketC, PacketS, PlayerID, TileIndex } from "./protocol.ts"; -import { V2, add_v2, length, lerp_exp_v2_mut, normalize, aabb_circle_distance } from "./util.ts"; +import { V2, add_v2, length, lerp_exp_v2_mut, normalize, aabb_circle_distance, sub_v2 } from "./util.ts"; import { draw_ingame, draw_wait } from "./visual.ts"; export const PLAYER_SIZE = 0.4; @@ -205,7 +205,7 @@ function frame_update(dt: number) { p.vel.y += input.y * dt * 0.5 p.x += p.vel.x p.y += p.vel.y - collide_player(p) + collide_player(p, dt) lerp_exp_v2_mut(p.vel, { x: 0, y: 0 }, dt * 5.) const update_item = (item: ItemData) => { @@ -249,7 +249,7 @@ function draw() { requestAnimationFrame(draw) } -function collide_player(p: PlayerData) { +function collide_player(p: PlayerData, dt: number) { const tiles_ignored = ["floor", "door", "chair"].map(t => data.tile_names.indexOf(t)) for (const [_, tile] of tiles) { if (tiles_ignored.includes(tile.kind)) continue @@ -269,4 +269,15 @@ function collide_player(p: PlayerData) { p.vel.x -= grad_x * vdotn p.vel.y -= grad_y * vdotn } + + for (const [_, player] of players) { + const diff = sub_v2(p, player) + const d = length(diff) + if (d < 0.01) continue + if (d >= PLAYER_SIZE * 2) continue + const norm = normalize(diff); + const f = 1 / (1 + d) + p.vel.x += norm.x * f * dt + p.vel.y += norm.y * f * dt + } } diff --git a/test-client/protocol.ts b/test-client/protocol.ts index 4516d0e5..746a83e8 100644 --- a/test-client/protocol.ts +++ b/test-client/protocol.ts @@ -26,7 +26,7 @@ export type PacketC = | { type: "set_player_item", player: PlayerID, item?: ItemIndex } // A player changed their item | { type: "set_active", tile: Vec2, progress?: number } // A tile is doing something. progress goes from 0 to 1, then null when finished | { type: "update_map", pos: Vec2, tile: TileIndex, neighbors: [TileIndex | null] } // A map tile was changed - | { type: "communicate", player: PlayerID, message?: Message } // A map tile was changed + | { type: "communicate", player: PlayerID, message?: Message } // A player wants to communicate something, message is null when cleared export type Message = { item: number } diff --git a/test-client/util.ts b/test-client/util.ts index 23679974..1c431411 100644 --- a/test-client/util.ts +++ b/test-client/util.ts @@ -21,6 +21,7 @@ export function add_v2(p: V2, o: V2 | number) { if (typeof o == "number") return { x: p.x + o, y: p.y + o } else return { x: p.x + o.x, y: p.y + o.y } } +export function sub_v2(p: V2, o: V2) { return { x: p.x - o.x, y: p.y - o.y } } export function aabb_circle_distance( min_x: number, |