factor player direction as component property

This commit is contained in:
JOLIMAITRE Matthieu 2024-10-20 22:43:16 +02:00
parent e502ca00de
commit fb85af7c40
2 changed files with 15 additions and 10 deletions

View file

@ -1,4 +1,3 @@
import { assert } from "https://deno.land/std@0.221.0/assert/assert.ts";
import { v2 } from "../../common/utils.ts";
import { Vec2 } from "../../common/utils.ts";
import { CompDisplay } from "../components/display.ts";
@ -19,7 +18,9 @@ export class CompEnemy {
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))));
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;

View file

@ -55,9 +55,10 @@ export class Session {
move(direction: Vec2) {
const pos = this.entity.get_force(CompPos);
pos.move_collide(this.engine, direction);
const player = this.entity.get_force(CompPlayer);
const display = this.entity.get_force(CompDisplay);
if (direction.x() > 0) display.update("P/");
if (direction.x() < 0) display.update("\\P");
if (direction.x() > 0) player.direction = Dir.Right, display.update("P/");
if (direction.x() < 0) player.direction = Dir.Left, display.update("\\P");
}
}
@ -67,15 +68,18 @@ export function sys_spawn_player(position: Vec2) {
entity.insert(
new CompPlayer(),
new CompPos(entity, position),
new CompDisplay("P/"),
new CompDisplay("\\P"),
)
);
};
}
export class CompPlayer {
life;
constructor() {
this.life = 10;
}
enum Dir {
Left,
Right,
}
export class CompPlayer {
life = 10;
direction = Dir.Left;
}