jumpstorm/engine/systems/FacingDirection.ts

47 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2023-07-19 23:38:24 -04:00
import {
ComponentNames,
Velocity,
FacingDirection as FacingDirectionComponent,
2023-08-25 18:48:17 -04:00
Control
} from '../components';
import { Game } from '../Game';
import { System, SystemNames } from './';
2023-07-19 23:38:24 -04:00
export class FacingDirection extends System {
constructor() {
super(SystemNames.FacingDirection);
}
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;
}
const totalVelocityComponent = new Velocity();
2023-08-12 15:49:16 -04:00
const control = entity.getComponent<Control>(ComponentNames.Control);
const velocity = entity.getComponent<Velocity>(
2023-08-25 18:48:17 -04:00
ComponentNames.Velocity
).velocity;
totalVelocityComponent.add(velocity);
2023-08-12 15:49:16 -04:00
if (control) {
totalVelocityComponent.add(control.controlVelocityComponent.velocity);
2023-08-12 15:49:16 -04:00
}
2023-07-19 23:38:24 -04:00
const facingDirection = entity.getComponent<FacingDirectionComponent>(
2023-08-25 18:48:17 -04:00
ComponentNames.FacingDirection
2023-07-19 23:38:24 -04:00
);
if (totalVelocityComponent.velocity.dCartesian.dx > 0) {
2023-07-19 23:38:24 -04:00
entity.addComponent(facingDirection.facingRightSprite);
} else if (totalVelocityComponent.velocity.dCartesian.dx < 0) {
2023-07-19 23:38:24 -04:00
entity.addComponent(facingDirection.facingLeftSprite);
}
2023-08-25 18:48:17 -04:00
}
2023-08-12 15:49:16 -04:00
);
2023-07-19 23:38:24 -04:00
}
}