From 6bf8f60a455b7ca019f6168d698955dbca8a30cb Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Wed, 10 Apr 2024 00:37:18 +0200 Subject: [PATCH] refactor --- server/components/world.ts | 11 +++++++++-- server/engine.ts | 38 +------------------------------------- server/entities/player.ts | 6 ++---- server/main.ts | 13 ++----------- server/network.ts | 10 +--------- 5 files changed, 15 insertions(+), 63 deletions(-) diff --git a/server/components/world.ts b/server/components/world.ts index 2c55f57..230c628 100644 --- a/server/components/world.ts +++ b/server/components/world.ts @@ -1,5 +1,5 @@ -import { spiral } from "../../common/utils.ts"; -import { chunks, enumerate, log_from, Vec2 } from "../../common/utils.ts"; +import { assertEquals } from "https://deno.land/std@0.221.0/assert/mod.ts"; +import { chunks, enumerate, log_from, spiral, Vec2 } from "../../common/utils.ts"; import { Engine, Entity, Query } from "../engine.ts"; import { CompDisplay } from "./display.ts"; const log = log_from(import.meta); @@ -53,6 +53,13 @@ function* displacement_steps(position: Vec2, displacement: Vec2) { } } +Deno.test("test_displacement", () => { + assertEquals( + [...displacement_steps(new Vec2(1, 1), new Vec2(4, 6))], + [new Vec2(2, 2), new Vec2(3, 3), new Vec2(4, 4), new Vec2(5, 5), new Vec2(5, 6), new Vec2(5, 7)], + ); +}); + export function query_at(pos: Vec2) { return Query.filter(CompPos, (c) => c.position.overlaps(pos)); } diff --git a/server/engine.ts b/server/engine.ts index 2d5b3b4..c257905 100644 --- a/server/engine.ts +++ b/server/engine.ts @@ -1,9 +1,4 @@ -import { assertEquals } from "https://deno.land/std@0.221.0/assert/mod.ts"; -import { Awaitable, log_from, Prototyped, Vec2 } from "../common/utils.ts"; -import { ClassMap } from "../common/utils.ts"; -import { wait } from "../common/utils.ts"; -import { Constructible } from "../common/utils.ts"; -import { first } from "../common/utils.ts"; +import { Awaitable, ClassMap, Constructible, first, log_from, Prototyped, wait } from "../common/utils.ts"; const log = log_from(import.meta); export class Entity { @@ -90,37 +85,6 @@ export class Engine { } } -function* displacement_steps(position: Vec2, displacement: Vec2) { - let pos = position; - let left = displacement; - while (left.len() >= 1) { - if (left.x() >= 1) { - pos = pos.add(new Vec2(1, 0)); - left = left.sub(new Vec2(1, 0)); - } - if (left.x() <= -1) { - pos = pos.add(new Vec2(-1, 0)); - left = left.sub(new Vec2(-1, 0)); - } - if (left.y() >= 1) { - pos = pos.add(new Vec2(0, 1)); - left = left.sub(new Vec2(0, 1)); - } - if (left.y() <= -1) { - pos = pos.add(new Vec2(0, -1)); - left = left.sub(new Vec2(0, -1)); - } - yield pos; - } -} - -Deno.test("test_displacement", () => { - assertEquals( - [...displacement_steps(new Vec2(1, 1), new Vec2(4, 6))], - [new Vec2(2, 2), new Vec2(3, 3), new Vec2(4, 4), new Vec2(5, 5), new Vec2(5, 6), new Vec2(5, 7)], - ); -}); - export class Query { private test; diff --git a/server/entities/player.ts b/server/entities/player.ts index a8ffebb..5f4d300 100644 --- a/server/entities/player.ts +++ b/server/entities/player.ts @@ -1,9 +1,7 @@ import { mts } from "../../common/mod.ts"; -import { range } from "../../common/utils.ts"; -import { log_from, v2, Vec2 } from "../../common/utils.ts"; +import { log_from, range, v2, Vec2 } from "../../common/utils.ts"; import { CompDisplay } from "../components/display.ts"; -import { sys_find_free_pos } from "../components/world.ts"; -import { CompPos, query_in_rect } from "../components/world.ts"; +import { CompPos, query_in_rect, sys_find_free_pos } from "../components/world.ts"; import { Engine, Entity } from "../engine.ts"; import { ClientInterface } from "../network.ts"; const log = log_from(import.meta); diff --git a/server/main.ts b/server/main.ts index 18357df..b848ce7 100755 --- a/server/main.ts +++ b/server/main.ts @@ -1,12 +1,11 @@ #!/bin/env -S deno run --allow-net --allow-read -import { log_from, v2, wait } from "../common/utils.ts"; +import { log_from, v2 } from "../common/utils.ts"; import { Engine } from "./engine.ts"; import { Session } from "./entities/player.ts"; -import { sys_spawn_obstacle, sys_spawn_structure } from "./components/world.ts"; +import { sys_spawn_structure } from "./components/world.ts"; import { Gateway } from "./network.ts"; import { sys_spawn_enemy } from "./entities/enemy.ts"; -import { spiral } from "../common/utils.ts"; const log = log_from(import.meta); async function main() { @@ -16,14 +15,6 @@ async function main() { engine.run(await sys_spawn_structure("../data/structures/houses.txt", v2(2, 2))); engine.run(sys_spawn_enemy(v2(1, 1))); - (async () => { - await wait(5_000); - for (const pos of spiral(v2(-8, -8))) { - await wait(500); - engine.run(sys_spawn_obstacle(pos, "@@")); - } - })(); - const port = 9999; const gateway = new Gateway(port); log("Awaiting connection."); diff --git a/server/network.ts b/server/network.ts index 719eb3a..b23fe7d 100644 --- a/server/network.ts +++ b/server/network.ts @@ -1,15 +1,7 @@ #!/bin/env -S deno run --allow-net import { MsgToClient, MsgToServer, mts } from "../common/mod.ts"; -import { - channel, - launch_caught, - log_from, - parsed_stream, - Receiver, - Sender, - serialized_stream, -} from "../common/utils.ts"; +import { channel, log_from, parsed_stream, Receiver, Sender, serialized_stream } from "../common/utils.ts"; const log = log_from(import.meta); export class Gateway {