add enemy forget target once range is too large
This commit is contained in:
parent
c04c7b725f
commit
5da4bcc64e
1 changed files with 12 additions and 5 deletions
|
@ -25,9 +25,9 @@ export class CompEnemy {
|
||||||
return pos.pos;
|
return pos.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
find_target_from(engine: Engine, pos: Vec2) {
|
find_target_from(engine: Engine, pos: Vec2, range: number) {
|
||||||
const RANGE = v2(2, 2);
|
const radius = v2(range, range);
|
||||||
const found = engine.one(Query.with(CompPlayer).with(CompId).and(query_in_rect(pos.sub(RANGE), pos.add(RANGE))));
|
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) return this.target = null;
|
||||||
const [_, id, __] = found;
|
const [_, id, __] = found;
|
||||||
return this.target = id.id;
|
return this.target = id.id;
|
||||||
|
@ -49,12 +49,19 @@ export function sys_spawn_enemy(pos: Vec2) {
|
||||||
export function sys_update_enemy() {
|
export function sys_update_enemy() {
|
||||||
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);
|
if (enemy.target === null) enemy.find_target_from(engine, enemy_pos.pos, 3);
|
||||||
if (enemy.target === null) continue;
|
if (enemy.target === null) continue;
|
||||||
const pos = enemy.get_target_pos(engine);
|
const pos = enemy.get_target_pos(engine);
|
||||||
assert(pos !== null);
|
assert(pos !== null);
|
||||||
const direction = pos.sub(enemy_pos.pos);
|
const direction = pos.sub(enemy_pos.pos);
|
||||||
enemy_pos.move_collide(engine, direction.normalize());
|
if (direction.len() <= 5) {
|
||||||
|
const displacement = direction.normalize();
|
||||||
|
const moved = enemy_pos.move_collide(engine, displacement);
|
||||||
|
if (!moved) {
|
||||||
|
enemy_pos.move_collide(engine, v2(displacement.x(), 0));
|
||||||
|
enemy_pos.move_collide(engine, v2(0, displacement.y()));
|
||||||
|
}
|
||||||
|
} else enemy.target = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue