Skip to content

Commit

Permalink
chore: rethink ai system
Browse files Browse the repository at this point in the history
  • Loading branch information
ufocoder committed May 22, 2024
1 parent f927b10 commit f22b057
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions src/lib/ecs/systems/AISystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,92 @@ import ECS from "src/lib/ecs";
import TextureComponent from "../components/TextureComponent";
import PositionMap from "../lib/PositionMap";
import { ComponentContainer } from "../Component";
import EnemyComponent from "../components/EnemyComponent";
import MoveComponent, { MainDirection } from "../components/MoveComponent";
import AngleComponent from "../components/AngleComponent";
// import { normalizeAngle, radiansToDegrees } from "src/lib/utils";

export default class AISystem extends System {
componentsRequired = new Set([AIComponent, AnimatedSpriteComponent, CircleComponent, PositionComponent]);
componentsRequired = new Set([
AIComponent,
EnemyComponent,
PositionComponent,
AngleComponent,
MoveComponent,
]);

protected readonly soundManager: SoundManager;
protected readonly textureMap: PositionMap<ComponentContainer>;

constructor(ecs: ECS, level: Level, soundManager: SoundManager) {
super(ecs);
this.soundManager = soundManager;

const cols = level.map[0].length;
const rows = level.map.length;

this.textureMap = new PositionMap(cols, rows);
}

start(): void {
this.ecs.query([PositionComponent, TextureComponent]).forEach(container => {
if (container.has(CameraComponent)) {
return;
}
this.ecs
.query([PositionComponent, TextureComponent])
.forEach((container) => {
if (container.has(CameraComponent)) {
return;
}

const position = container.get(PositionComponent);
const position = container.get(PositionComponent);

this.textureMap.set(
Math.floor(position.x),
Math.floor(position.y),
container
);
})
this.textureMap.set(
Math.floor(position.x),
Math.floor(position.y),
container
);
});
}

update(dt: number, entities: Set<Entity>) {
const [camera] = this.ecs.query([HealthComponent, CircleComponent, CameraComponent]);
const [camera] = this.ecs.query([
HealthComponent,
CircleComponent,
CameraComponent,
]);

const cameraPosition = camera.get(PositionComponent);
const cameraCircle = camera.get(CircleComponent);
const cameraAngle = camera.get(AngleComponent);
const cameraHealth = camera.get(HealthComponent);

entities.forEach((entity: Entity) => {
const components = this.ecs.getComponents(entity);
const entityAI = components.get(AIComponent);
const entityPosition = components.get(PositionComponent);
const entityAngle = components.get(AngleComponent);
const entityCircle = components.get(CircleComponent);
const entityMove = components.get(MoveComponent);
const entityAnimation = components.get(AnimatedSpriteComponent);

const dx = cameraPosition.x - entityPosition.x;
const dy = cameraPosition.y - entityPosition.y;
const distance = Math.sqrt(dx**2 + dy**2) - cameraCircle.radius - entityCircle.radius;

// @TODO: move to move system
if (entityAI.distance > distance && distance > 0) {
entityAnimation.switchState('walk');
const newX = entityPosition.x + dx * dt * entityAI.moveSpeed;
const newY = entityPosition.y + dy * dt * entityAI.moveSpeed;
if (!this.textureMap.has(Math.floor(newX), Math.floor(newY))) {
entityPosition.x = newX;
entityPosition.y = newY;
}
const d =
Math.sqrt(dx ** 2 + dy ** 2) -
cameraCircle.radius -
entityCircle?.radius;

if (entityAI.distance > d && d > 0) {
const newAngle = cameraAngle.angle + 180; // normalizeAngle(-radiansToDegrees(Math.acos(dx / d)));

entityAnimation.switchState("walk");
entityMove.mainDirection = MainDirection.Forward;
entityAngle.angle = newAngle;
} else {
entityAnimation.switchState('idle');
entityMove.mainDirection = MainDirection.None;
entityAnimation.switchState("idle");
}

if (distance <= 0) {
entityAnimation.switchState('attack');
if (d <= 0) {
entityAnimation.switchState("attack");
entityAI.lastAttackTime += dt;
} else {
entityAI.lastAttackTime = 0;
Expand All @@ -91,6 +111,5 @@ export default class AISystem extends System {
});
}

destroy(){}

}
destroy() {}
}

0 comments on commit f22b057

Please sign in to comment.