2023-07-19 23:38:24 -04:00
|
|
|
import {
|
|
|
|
ComponentNames,
|
|
|
|
Velocity,
|
|
|
|
FacingDirection as FacingDirectionComponent,
|
2023-08-12 15:49:16 -04:00
|
|
|
Control,
|
2023-07-19 23:38:24 -04:00
|
|
|
} from "../components";
|
2023-07-20 23:47:32 -04:00
|
|
|
import { Game } from "../Game";
|
2023-07-19 23:38:24 -04:00
|
|
|
import { System, SystemNames } from "./";
|
|
|
|
|
|
|
|
export class FacingDirection extends System {
|
|
|
|
constructor() {
|
|
|
|
super(SystemNames.FacingDirection);
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:47:32 -04:00
|
|
|
public update(_dt: number, game: Game) {
|
2023-08-12 15:49:16 -04:00
|
|
|
game.forEachEntityWithComponent(
|
|
|
|
ComponentNames.FacingDirection,
|
|
|
|
(entity) => {
|
2023-07-19 23:38:24 -04:00
|
|
|
if (!entity.hasComponent(ComponentNames.Velocity)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-12 15:49:16 -04:00
|
|
|
const totalVelocity: Velocity = new Velocity();
|
|
|
|
const control = entity.getComponent<Control>(ComponentNames.Control);
|
2023-07-19 23:38:24 -04:00
|
|
|
const velocity = entity.getComponent<Velocity>(ComponentNames.Velocity);
|
2023-08-12 15:49:16 -04:00
|
|
|
totalVelocity.add(velocity);
|
|
|
|
if (control) {
|
|
|
|
totalVelocity.add(control.controlVelocity);
|
|
|
|
}
|
|
|
|
|
2023-07-19 23:38:24 -04:00
|
|
|
const facingDirection = entity.getComponent<FacingDirectionComponent>(
|
2023-08-12 15:49:16 -04:00
|
|
|
ComponentNames.FacingDirection,
|
2023-07-19 23:38:24 -04:00
|
|
|
);
|
|
|
|
|
2023-08-12 15:49:16 -04:00
|
|
|
if (totalVelocity.dCartesian.dx > 0) {
|
2023-07-19 23:38:24 -04:00
|
|
|
entity.addComponent(facingDirection.facingRightSprite);
|
2023-08-12 15:49:16 -04:00
|
|
|
} else if (totalVelocity.dCartesian.dx < 0) {
|
2023-07-19 23:38:24 -04:00
|
|
|
entity.addComponent(facingDirection.facingLeftSprite);
|
|
|
|
}
|
2023-08-12 15:49:16 -04:00
|
|
|
},
|
|
|
|
);
|
2023-07-19 23:38:24 -04:00
|
|
|
}
|
|
|
|
}
|