aboutsummaryrefslogtreecommitdiff
path: root/test-client/main.ts
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-06-20 14:42:25 +0200
committermetamuffin <metamuffin@disroot.org>2024-06-23 19:24:14 +0200
commit96dd53c9ea8fe124a699de3b04069102a6002128 (patch)
treeba978eae3518dd8f60e061d2c40f5e4478f987da /test-client/main.ts
parentd888bd25908ff26712232f1b296f902d37d93d40 (diff)
downloadhurrycurry-96dd53c9ea8fe124a699de3b04069102a6002128.tar
hurrycurry-96dd53c9ea8fe124a699de3b04069102a6002128.tar.bz2
hurrycurry-96dd53c9ea8fe124a699de3b04069102a6002128.tar.zst
client-side collision
Diffstat (limited to 'test-client/main.ts')
-rw-r--r--test-client/main.ts17
1 files changed, 14 insertions, 3 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
+ }
}