jumpstorm/engine/systems/FacingDirection.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-07-19 23:38:24 -04:00
import {
ComponentNames,
Velocity,
FacingDirection as FacingDirectionComponent,
} from "../components";
import { Game } from "../Game";
2023-07-19 23:38:24 -04:00
import type { Entity } from "../entities";
import { System, SystemNames } from "./";
export class FacingDirection extends System {
constructor() {
super(SystemNames.FacingDirection);
}
public update(_dt: number, game: Game) {
game.componentEntities
2023-07-19 23:38:24 -04:00
.get(ComponentNames.FacingDirection)
?.forEach((entityId) => {
const entity = game.entities.get(entityId);
2023-07-19 23:38:24 -04:00
if (!entity.hasComponent(ComponentNames.Velocity)) {
return;
}
const velocity = entity.getComponent<Velocity>(ComponentNames.Velocity);
const facingDirection = entity.getComponent<FacingDirectionComponent>(
ComponentNames.FacingDirection
);
if (velocity.dCartesian.dx > 0) {
entity.addComponent(facingDirection.facingRightSprite);
} else if (velocity.dCartesian.dx < 0) {
entity.addComponent(facingDirection.facingLeftSprite);
}
});
}
}