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);
|
|
|
|
}
|
|
|
|
|
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-13 19:09:12 -04:00
|
|
|
const totalVelocityComponent = new Velocity();
|
2023-08-12 15:49:16 -04:00
|
|
|
const control = entity.getComponent<Control>(ComponentNames.Control);
|
2023-08-13 19:09:12 -04:00
|
|
|
const velocity = entity.getComponent<Velocity>(
|
2023-08-25 18:48:17 -04:00
|
|
|
ComponentNames.Velocity
|
2023-08-13 19:09:12 -04:00
|
|
|
).velocity;
|
|
|
|
|
|
|
|
totalVelocityComponent.add(velocity);
|
2023-08-12 15:49:16 -04:00
|
|
|
if (control) {
|
2023-08-13 19:09:12 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2023-08-13 19:09:12 -04:00
|
|
|
if (totalVelocityComponent.velocity.dCartesian.dx > 0) {
|
2023-07-19 23:38:24 -04:00
|
|
|
entity.addComponent(facingDirection.facingRightSprite);
|
2023-08-13 19:09:12 -04:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|