Compare commits

..

No commits in common. "66c80fac5d223bb18b8002aec3b6a854190c46b5" and "5da4bcc64e41b33a823ffeb397f211baac9367b1" have entirely different histories.

3 changed files with 18 additions and 42 deletions

View file

@ -14,27 +14,18 @@ import {
const log = log_from(import.meta); const log = log_from(import.meta);
async function main() { async function main() {
const args = parse_args(Deno.args);
log("Client starting."); log("Client starting.");
const interface_ = await ServerInterface.connect(args.port); const interface_ = await ServerInterface.connect(9999);
const display = new DisplayHandler(interface_.outputs);
new MsgHandler(interface_.inputs, display).spin();
new InputHandler(interface_.outputs).spin(); new InputHandler(interface_.outputs).spin();
const display = new DisplayHandler(interface_.outputs);
display.spin(); display.spin();
new MsgHandler(interface_.inputs, display).spin();
log("Connected."); log("Connected.");
interface_.outputs.send({ kind: "ping", content: { message: "Machin." } }); interface_.outputs.send({ kind: "ping", content: { message: "Machin." } });
log("Sent ping."); log("Sent ping.");
} }
function parse_args(args: string[]) {
const [port_] = args;
let port = 9999;
if (port_ !== undefined) port = parseInt(port_);
return { port };
}
class MsgHandler { class MsgHandler {
receiver; receiver;
display; display;

View file

@ -17,27 +17,21 @@ export class CompEnemy {
this.life = 10; this.life = 10;
} }
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) {
this.target = null;
return;
}
const [_, id, __] = found;
this.target = id.id;
}
get_target_pos(engine: Engine) { get_target_pos(engine: Engine) {
if (this.target === null) return null; if (this.target === null) return null;
const result = engine.one(Query.with(CompId).filter(([c]) => c.id === this.target).with(CompPos)); const result = engine.one(Query.with(CompId).filter(([c]) => c.id === this.target).with(CompPos));
if (result === null) { if (result === null) return null;
this.target = null;
return null;
}
const [_, pos] = result; const [_, pos] = result;
return pos.pos; 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;
const [_, id, __] = found;
return this.target = id.id;
}
} }
export function sys_spawn_enemy(pos: Vec2) { export function sys_spawn_enemy(pos: Vec2) {
@ -53,15 +47,14 @@ export function sys_spawn_enemy(pos: Vec2) {
} }
export function sys_update_enemy() { export function sys_update_enemy() {
const trigger_range = 5;
const forget_range = 10;
return (engine: Engine) => { return (engine: Engine) => {
for (const [enemy, enemy_pos] of engine.all(Query.with(CompEnemy).with(CompPos))) { 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, trigger_range); if (enemy.target === null) enemy.find_target_from(engine, enemy_pos.pos, 3);
if (enemy.target === null) continue;
const pos = enemy.get_target_pos(engine); const pos = enemy.get_target_pos(engine);
if (pos === null) continue; assert(pos !== null);
const direction = pos.sub(enemy_pos.pos); const direction = pos.sub(enemy_pos.pos);
if (direction.len() <= forget_range) { if (direction.len() <= 5) {
const displacement = direction.normalize(); const displacement = direction.normalize();
const moved = enemy_pos.move_collide(engine, displacement); const moved = enemy_pos.move_collide(engine, displacement);
if (!moved) { if (!moved) {

View file

@ -10,8 +10,6 @@ import { enemy_plugin } from "./entities/enemy.ts";
const log = log_from(import.meta); const log = log_from(import.meta);
async function main() { async function main() {
const args = parse_args(Deno.args);
log("Starting."); log("Starting.");
const engine = new Engine(); const engine = new Engine();
enemy_plugin(engine); enemy_plugin(engine);
@ -20,18 +18,12 @@ async function main() {
engine.run(await sys_spawn_structure("../data/structures/houses.txt", v2(2, 2))); engine.run(await sys_spawn_structure("../data/structures/houses.txt", v2(2, 2)));
engine.run(sys_spawn_enemy(v2(1, 1))); engine.run(sys_spawn_enemy(v2(1, 1)));
const gateway = new Gateway(args.port); const port = 9999;
const gateway = new Gateway(port);
log("Awaiting connection."); log("Awaiting connection.");
for await (const client of gateway.accept()) { for await (const client of gateway.accept()) {
Session.init(client, engine).start(); 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(); if (import.meta.main) await main();