init
This commit is contained in:
commit
28b026a614
17 changed files with 895 additions and 0 deletions
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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue