refactor enemy update
This commit is contained in:
parent
5da4bcc64e
commit
db1db1e519
1 changed files with 21 additions and 14 deletions
|
@ -17,20 +17,26 @@ export class CompEnemy {
|
||||||
this.life = 10;
|
this.life = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
get_target_pos(engine: Engine) {
|
|
||||||
if (this.target === null) return null;
|
|
||||||
const result = engine.one(Query.with(CompId).filter(([c]) => c.id === this.target).with(CompPos));
|
|
||||||
if (result === null) return null;
|
|
||||||
const [_, pos] = result;
|
|
||||||
return pos.pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
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) return this.target = null;
|
if (found === null) {
|
||||||
|
this.target = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
const [_, id, __] = found;
|
const [_, id, __] = found;
|
||||||
return this.target = id.id;
|
this.target = id.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
get_target_pos(engine: Engine) {
|
||||||
|
if (this.target === null) return null;
|
||||||
|
const result = engine.one(Query.with(CompId).filter(([c]) => c.id === this.target).with(CompPos));
|
||||||
|
if (result === null) {
|
||||||
|
this.target = null;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const [_, pos] = result;
|
||||||
|
return pos.pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,14 +53,15 @@ export function sys_spawn_enemy(pos: Vec2) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sys_update_enemy() {
|
export function sys_update_enemy() {
|
||||||
|
const trigger_range = 5;
|
||||||
|
const forget_range = 10;
|
||||||
return (engine: Engine) => {
|
return (engine: Engine) => {
|
||||||
for (const [enemy, enemy_pos] of engine.all(Query.with(CompEnemy).with(CompPos))) {
|
for (const [enemy, enemy_pos] of engine.all(Query.with(CompEnemy).with(CompPos))) {
|
||||||
if (enemy.target === null) enemy.find_target_from(engine, enemy_pos.pos, 3);
|
if (enemy.target === null) enemy.find_target_from(engine, enemy_pos.pos, trigger_range);
|
||||||
if (enemy.target === null) continue;
|
|
||||||
const pos = enemy.get_target_pos(engine);
|
const pos = enemy.get_target_pos(engine);
|
||||||
assert(pos !== null);
|
if (pos === null) continue;
|
||||||
const direction = pos.sub(enemy_pos.pos);
|
const direction = pos.sub(enemy_pos.pos);
|
||||||
if (direction.len() <= 5) {
|
if (direction.len() <= forget_range) {
|
||||||
const displacement = direction.normalize();
|
const displacement = direction.normalize();
|
||||||
const moved = enemy_pos.move_collide(engine, displacement);
|
const moved = enemy_pos.move_collide(engine, displacement);
|
||||||
if (!moved) {
|
if (!moved) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue