From fb85af7c40d90c8db51eb28e2e8ccd0f34e8ec1e Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Sun, 20 Oct 2024 22:43:16 +0200 Subject: [PATCH] factor player direction as component property --- server/entities/enemy.ts | 5 +++-- server/entities/player.ts | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/server/entities/enemy.ts b/server/entities/enemy.ts index a1decc7..6b34edd 100644 --- a/server/entities/enemy.ts +++ b/server/entities/enemy.ts @@ -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; diff --git a/server/entities/player.ts b/server/entities/player.ts index 431e393..6ade586 100644 --- a/server/entities/player.ts +++ b/server/entities/player.ts @@ -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; }