init
This commit is contained in:
commit
28b026a614
17 changed files with 895 additions and 0 deletions
153
server/engine.ts
Normal file
153
server/engine.ts
Normal file
|
@ -0,0 +1,153 @@
|
|||
import { assertEquals } from "https://deno.land/std@0.221.0/assert/mod.ts";
|
||||
import { chunks, enumerate, log_from, Prototyped, range, Vec2 } from "../common/utils.ts";
|
||||
import { ClientInterface } from "./network.ts";
|
||||
import { Session } from "./entities/player.ts";
|
||||
import { ClassMap } from "../common/utils.ts";
|
||||
import { maybe } from "../common/utils.ts";
|
||||
const log = log_from(import.meta);
|
||||
|
||||
export class WorldEntity {
|
||||
display;
|
||||
world;
|
||||
identifier;
|
||||
position;
|
||||
compontents;
|
||||
|
||||
constructor(world: World, identifier: number, display: string, position: Vec2, ...components: Prototyped[]) {
|
||||
this.display = display;
|
||||
this.world = world;
|
||||
this.identifier = identifier;
|
||||
this.position = position;
|
||||
this.compontents = new ClassMap();
|
||||
for (const c of components) this.compontents.insert(c);
|
||||
}
|
||||
}
|
||||
|
||||
export class World {
|
||||
next_id;
|
||||
entities;
|
||||
|
||||
constructor() {
|
||||
this.next_id = 0;
|
||||
this.entities = new Map<number, WorldEntity>();
|
||||
}
|
||||
|
||||
spawn_entity(display: string, pos: Vec2) {
|
||||
const entity = new WorldEntity(this, this.next_id++, display, pos);
|
||||
this.entities.set(entity.identifier, entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
async spawn_structure(file_path: string, origin: Vec2) {
|
||||
const content = await Deno.readTextFile(file_path);
|
||||
let count = 0;
|
||||
for (const [y, line] of enumerate(content.split("\n"))) {
|
||||
for (const [x, chars] of enumerate(chunks(line, 2))) {
|
||||
const display = chars[0] + chars[1];
|
||||
if (display === " ") continue;
|
||||
this.spawn_entity(display, origin.add(new Vec2(x, y)));
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
log("Spawned", count, "tiles from", file_path);
|
||||
}
|
||||
|
||||
render(center: Vec2, size: Vec2) {
|
||||
const radius = size.scale(0.5);
|
||||
const result = Array.from(range(0, size.y())).map(() => Array.from(range(0, size.x())).map(() => " "));
|
||||
const top_left = center.sub(radius);
|
||||
const bottom_right = center.add(radius);
|
||||
for (const entity of this.entities.values()) {
|
||||
if (!entity.position.inside(top_left, bottom_right)) continue;
|
||||
const local_pos = entity.position.sub(top_left);
|
||||
result[local_pos.y()][local_pos.x()] = entity.display;
|
||||
}
|
||||
return result.map((line) => line.join("")).toReversed().join("\n");
|
||||
}
|
||||
|
||||
move_collide(entity_id: number, displacement: Vec2) {
|
||||
let result = false;
|
||||
const entity = this.get_entity(entity_id);
|
||||
if (entity === undefined) return result;
|
||||
for (const step of displacement_steps(entity.position, displacement)) {
|
||||
const collision = this.get_entity_at(step);
|
||||
if (collision !== undefined) return result;
|
||||
entity.position = step;
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
get_entity(entity_id: number) {
|
||||
return this.entities.get(entity_id);
|
||||
}
|
||||
|
||||
*get_entities_at(pos: Vec2) {
|
||||
for (const entity of this.entities.values()) if (entity.position.overlaps(pos)) yield entity;
|
||||
}
|
||||
|
||||
get_entity_at(pos: Vec2) {
|
||||
return maybe([...this.get_entities_at(pos)][0]);
|
||||
}
|
||||
|
||||
*get_entities_in_range(min: Vec2, max: Vec2) {
|
||||
for (const entity of this.entities.values()) if (entity.position.inside(min, max)) yield entity;
|
||||
}
|
||||
|
||||
get_entity_in_range(min: Vec2, max: Vec2) {
|
||||
return maybe([...this.get_entities_in_range(min, max)][0]);
|
||||
}
|
||||
|
||||
kill(entity_id: number) {
|
||||
this.entities.delete(entity_id);
|
||||
}
|
||||
}
|
||||
|
||||
export class Engine {
|
||||
world;
|
||||
|
||||
constructor(world: World) {
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
async start() {
|
||||
log("Started engine.");
|
||||
}
|
||||
|
||||
add_session(client: ClientInterface) {
|
||||
const player_entity = this.world.spawn_entity("`/", new Vec2(0, 0));
|
||||
const session = new Session(client, this.world, player_entity);
|
||||
session.spin();
|
||||
}
|
||||
}
|
||||
|
||||
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)],
|
||||
);
|
||||
});
|
33
server/entities/enemy.ts
Normal file
33
server/entities/enemy.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import { World, WorldEntity } from "../engine.ts";
|
||||
import { v2, Vec2 } from "../../common/utils.ts";
|
||||
import { wait } from "../../common/utils.ts";
|
||||
|
||||
export class Enemy {
|
||||
entity;
|
||||
alive;
|
||||
|
||||
constructor(entity: WorldEntity) {
|
||||
this.entity = entity;
|
||||
this.alive = true;
|
||||
}
|
||||
|
||||
static spawn(world: World, pos: Vec2) {
|
||||
const entity = world.spawn_entity("èé", pos);
|
||||
}
|
||||
|
||||
async spin() {
|
||||
while (this.alive) {
|
||||
await wait(500);
|
||||
const target = this.find_target();
|
||||
}
|
||||
}
|
||||
|
||||
find_target() {
|
||||
const world = this.entity.world;
|
||||
const local_origin = this.entity.position;
|
||||
const found = world.get_entities_in_range(local_origin.sub(v2(3, 3)), local_origin.add(v2(3, 3)));
|
||||
for (const entity of found) {
|
||||
// if (entity.compontents.get())
|
||||
}
|
||||
}
|
||||
}
|
61
server/entities/player.ts
Normal file
61
server/entities/player.ts
Normal file
|
@ -0,0 +1,61 @@
|
|||
import { mts } from "../../common/mod.ts";
|
||||
import { log_from, Vec2 } from "../../common/utils.ts";
|
||||
import { World, WorldEntity } from "../engine.ts";
|
||||
import { ClientInterface } from "../network.ts";
|
||||
const log = log_from(import.meta);
|
||||
|
||||
export class Session {
|
||||
client;
|
||||
world;
|
||||
player_entity;
|
||||
|
||||
constructor(client: ClientInterface, world: World, player_entity: WorldEntity) {
|
||||
this.client = client;
|
||||
this.world = world;
|
||||
this.player_entity = player_entity;
|
||||
}
|
||||
|
||||
async spin() {
|
||||
try {
|
||||
for await (const input of this.client.inputs.iter()) {
|
||||
if (input.kind !== "request_display") log("Received", input);
|
||||
|
||||
if (input.kind === "ping") this.client.outputs.send({ kind: "ping_response", content: input.content });
|
||||
if (input.kind === "request_display") this.send_display(input.content.width, input.content.height);
|
||||
if (input.kind === "input") this.handle_input(input);
|
||||
if (input.kind === "exit") this.world.kill(this.player_entity.identifier);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Session loop failed, ", error);
|
||||
this.world.kill(this.player_entity.identifier);
|
||||
}
|
||||
}
|
||||
|
||||
send_display(width: number, height: number) {
|
||||
const raw = this.world.render(this.player_entity.position, new Vec2(width, height));
|
||||
this.client.outputs.send({ kind: "display", content: { raw } });
|
||||
}
|
||||
|
||||
handle_input(input: mts.MsgInput) {
|
||||
if (input.content.control === "up") this.world.move_collide(this.player_entity.identifier, new Vec2(0, 1));
|
||||
if (input.content.control === "down") this.world.move_collide(this.player_entity.identifier, new Vec2(0, -1));
|
||||
if (input.content.control === "left") this.world.move_collide(this.player_entity.identifier, new Vec2(-1, 0));
|
||||
if (input.content.control === "right") this.world.move_collide(this.player_entity.identifier, new Vec2(1, 0));
|
||||
}
|
||||
}
|
||||
|
||||
export class PlayerComponent {
|
||||
entity;
|
||||
|
||||
constructor(entity: WorldEntity) {
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
move(direction: Vec2) {
|
||||
return this.entity.world.move_collide(this.entity.identifier, direction);
|
||||
}
|
||||
|
||||
render(res: Vec2) {
|
||||
return this.entity.world.render(this.entity.position, res);
|
||||
}
|
||||
}
|
24
server/main.ts
Executable file
24
server/main.ts
Executable file
|
@ -0,0 +1,24 @@
|
|||
#!/bin/env -S deno run --allow-net --allow-read
|
||||
|
||||
import { log_from, Vec2 } from "../common/utils.ts";
|
||||
import { Engine, World } from "./engine.ts";
|
||||
import { Enemy } from "./entities/enemy.ts";
|
||||
import { Gateway } from "./network.ts";
|
||||
const log = log_from(import.meta);
|
||||
|
||||
async function main() {
|
||||
log("Starting.");
|
||||
const world = new World();
|
||||
await world.spawn_structure("../data/structures/houses.txt", new Vec2(2, 2));
|
||||
const engine = new Engine(world);
|
||||
engine.start();
|
||||
|
||||
Enemy.spawn(world, new Vec2(-3, -3));
|
||||
|
||||
const port = 9999;
|
||||
const gateway = new Gateway(port);
|
||||
log("Awaiting connection.");
|
||||
for await (const session of gateway.accept()) engine.add_session(session);
|
||||
}
|
||||
|
||||
if (import.meta.main) await main();
|
50
server/network.ts
Normal file
50
server/network.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
#!/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";
|
||||
const log = log_from(import.meta);
|
||||
|
||||
export class Gateway {
|
||||
server;
|
||||
constructor(port: number) {
|
||||
this.server = Deno.listen({ port });
|
||||
log("Listening on", port);
|
||||
}
|
||||
async *accept() {
|
||||
for await (const connection of this.server) {
|
||||
const session = await ClientInterface.init(connection);
|
||||
log("New session.");
|
||||
yield session;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class ClientInterface {
|
||||
inputs;
|
||||
outputs;
|
||||
|
||||
constructor(inputs: Receiver<MsgToServer>, outputs: Sender<MsgToClient>) {
|
||||
this.inputs = inputs;
|
||||
this.outputs = outputs;
|
||||
}
|
||||
|
||||
// deno-lint-ignore require-await
|
||||
static async init(connection: Deno.Conn) {
|
||||
// TODO : handshake ?
|
||||
const [input_sender, input_receiver] = channel<MsgToServer>();
|
||||
const [output_sender, output_receiver] = channel<MsgToClient>();
|
||||
input_sender.send_all(parsed_stream(mts.message_to_server_parser())(connection.readable))
|
||||
.finally(() => input_sender.send({ kind: "exit" }));
|
||||
serialized_stream(output_receiver.iter())(connection.writable)
|
||||
.finally(() => input_sender.send({ kind: "exit" }));
|
||||
return new ClientInterface(input_receiver, output_sender);
|
||||
}
|
||||
}
|
5
server/watch.sh
Executable file
5
server/watch.sh
Executable file
|
@ -0,0 +1,5 @@
|
|||
#!/bin/sh
|
||||
set -e
|
||||
cd "$(dirname "$(realpath "$0")")"
|
||||
|
||||
nodemon -w ../common -w . -e ts -x 'clear && ./main.ts'
|
Loading…
Add table
Add a link
Reference in a new issue