factor player direction as component property
This commit is contained in:
parent
e502ca00de
commit
fb85af7c40
2 changed files with 15 additions and 10 deletions
|
@ -1,4 +1,3 @@
|
||||||
import { assert } from "https://deno.land/std@0.221.0/assert/assert.ts";
|
|
||||||
import { v2 } from "../../common/utils.ts";
|
import { v2 } from "../../common/utils.ts";
|
||||||
import { Vec2 } from "../../common/utils.ts";
|
import { Vec2 } from "../../common/utils.ts";
|
||||||
import { CompDisplay } from "../components/display.ts";
|
import { CompDisplay } from "../components/display.ts";
|
||||||
|
@ -19,7 +18,9 @@ export class CompEnemy {
|
||||||
|
|
||||||
find_target_from(engine: Engine, pos: Vec2, range: number) {
|
find_target_from(engine: Engine, pos: Vec2, range: number) {
|
||||||
const radius = v2(range, range);
|
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) {
|
if (found === null) {
|
||||||
this.target = null;
|
this.target = null;
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -55,9 +55,10 @@ export class Session {
|
||||||
move(direction: Vec2) {
|
move(direction: Vec2) {
|
||||||
const pos = this.entity.get_force(CompPos);
|
const pos = this.entity.get_force(CompPos);
|
||||||
pos.move_collide(this.engine, direction);
|
pos.move_collide(this.engine, direction);
|
||||||
|
const player = this.entity.get_force(CompPlayer);
|
||||||
const display = this.entity.get_force(CompDisplay);
|
const display = this.entity.get_force(CompDisplay);
|
||||||
if (direction.x() > 0) display.update("P/");
|
if (direction.x() > 0) player.direction = Dir.Right, display.update("P/");
|
||||||
if (direction.x() < 0) 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(
|
entity.insert(
|
||||||
new CompPlayer(),
|
new CompPlayer(),
|
||||||
new CompPos(entity, position),
|
new CompPos(entity, position),
|
||||||
new CompDisplay("P/"),
|
new CompDisplay("\\P"),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Dir {
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
}
|
||||||
|
|
||||||
export class CompPlayer {
|
export class CompPlayer {
|
||||||
life;
|
life = 10;
|
||||||
constructor() {
|
direction = Dir.Left;
|
||||||
this.life = 10;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue