From db1db1e519e284910a2ffb9de1d81ff5fca7befb Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Wed, 10 Apr 2024 03:38:55 +0200 Subject: [PATCH 1/2] refactor enemy update --- server/entities/enemy.ts | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/server/entities/enemy.ts b/server/entities/enemy.ts index 494826d..a1decc7 100644 --- a/server/entities/enemy.ts +++ b/server/entities/enemy.ts @@ -17,20 +17,26 @@ export class CompEnemy { this.life = 10; } - get_target_pos(engine: Engine) { - if (this.target === null) return null; - const result = engine.one(Query.with(CompId).filter(([c]) => c.id === this.target).with(CompPos)); - if (result === null) return null; - const [_, pos] = result; - return pos.pos; - } - find_target_from(engine: Engine, pos: Vec2, range: number) { const radius = v2(range, range); const found = engine.one(Query.with(CompPlayer).with(CompId).and(query_in_rect(pos.sub(radius), pos.add(radius)))); - if (found === null) return this.target = null; + if (found === null) { + this.target = null; + return; + } const [_, id, __] = found; - return this.target = id.id; + this.target = id.id; + } + + get_target_pos(engine: Engine) { + if (this.target === null) return null; + const result = engine.one(Query.with(CompId).filter(([c]) => c.id === this.target).with(CompPos)); + if (result === null) { + this.target = null; + return null; + } + const [_, pos] = result; + return pos.pos; } } @@ -47,14 +53,15 @@ export function sys_spawn_enemy(pos: Vec2) { } export function sys_update_enemy() { + const trigger_range = 5; + const forget_range = 10; return (engine: Engine) => { for (const [enemy, enemy_pos] of engine.all(Query.with(CompEnemy).with(CompPos))) { - if (enemy.target === null) enemy.find_target_from(engine, enemy_pos.pos, 3); - if (enemy.target === null) continue; + if (enemy.target === null) enemy.find_target_from(engine, enemy_pos.pos, trigger_range); const pos = enemy.get_target_pos(engine); - assert(pos !== null); + if (pos === null) continue; const direction = pos.sub(enemy_pos.pos); - if (direction.len() <= 5) { + if (direction.len() <= forget_range) { const displacement = direction.normalize(); const moved = enemy_pos.move_collide(engine, displacement); if (!moved) { From 66c80fac5d223bb18b8002aec3b6a854190c46b5 Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Wed, 10 Apr 2024 03:42:26 +0200 Subject: [PATCH 2/2] add custom port through args --- client/main.ts | 15 ++++++++++++--- server/main.ts | 12 ++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/client/main.ts b/client/main.ts index 999bcc1..1ca93fc 100755 --- a/client/main.ts +++ b/client/main.ts @@ -14,18 +14,27 @@ import { const log = log_from(import.meta); async function main() { + const args = parse_args(Deno.args); + log("Client starting."); - const interface_ = await ServerInterface.connect(9999); - new InputHandler(interface_.outputs).spin(); + const interface_ = await ServerInterface.connect(args.port); const display = new DisplayHandler(interface_.outputs); - display.spin(); new MsgHandler(interface_.inputs, display).spin(); + new InputHandler(interface_.outputs).spin(); + display.spin(); log("Connected."); interface_.outputs.send({ kind: "ping", content: { message: "Machin." } }); log("Sent ping."); } +function parse_args(args: string[]) { + const [port_] = args; + let port = 9999; + if (port_ !== undefined) port = parseInt(port_); + return { port }; +} + class MsgHandler { receiver; display; diff --git a/server/main.ts b/server/main.ts index 1a67098..5e88b52 100755 --- a/server/main.ts +++ b/server/main.ts @@ -10,6 +10,8 @@ import { enemy_plugin } from "./entities/enemy.ts"; const log = log_from(import.meta); async function main() { + const args = parse_args(Deno.args); + log("Starting."); const engine = new Engine(); enemy_plugin(engine); @@ -18,12 +20,18 @@ async function main() { engine.run(await sys_spawn_structure("../data/structures/houses.txt", v2(2, 2))); engine.run(sys_spawn_enemy(v2(1, 1))); - const port = 9999; - const gateway = new Gateway(port); + const gateway = new Gateway(args.port); log("Awaiting connection."); for await (const client of gateway.accept()) { Session.init(client, engine).start(); } } +function parse_args(args: string[]) { + const [port_] = args; + let port = 9999; + if (port_ !== undefined) port = parseInt(port_); + return { port }; +} + if (import.meta.main) await main();