2d-physics-engine
Version:
A lightweight, flexible 2D physics engine with ECS architecture, built with TypeScript
60 lines • 2.21 kB
JavaScript
import { Vector2 } from '../math/Vector2';
import { Component } from './Component.abstract';
import { Rigidbody } from './Rigidbody.component';
import { Transform } from './Transform.component';
export class Controller extends Component {
constructor(inputManager, moveForce) {
super();
Object.defineProperty(this, "inputManager", {
enumerable: true,
configurable: true,
writable: true,
value: inputManager
});
Object.defineProperty(this, "moveForce", {
enumerable: true,
configurable: true,
writable: true,
value: moveForce
});
Object.defineProperty(this, "componentId", {
enumerable: true,
configurable: true,
writable: true,
value: Symbol('Controller')
});
}
update(_deltaTime, parentEntity) {
let x = 0;
let y = 0;
// Get input vector from WASD or arrow keys
if (this.inputManager.isKeyPressed('w') || this.inputManager.isKeyPressed('ArrowUp')) {
y -= 1;
}
if (this.inputManager.isKeyPressed('s') || this.inputManager.isKeyPressed('ArrowDown')) {
y += 1;
}
if (this.inputManager.isKeyPressed('a') || this.inputManager.isKeyPressed('ArrowLeft')) {
x -= 1;
}
if (this.inputManager.isKeyPressed('d') || this.inputManager.isKeyPressed('ArrowRight')) {
x += 1;
}
const input = new Vector2(x, y);
if (input.getMagnitude() > 0) {
const force = input.getNormal().scale(this.moveForce);
const transform = parentEntity.getComponent(Transform);
const rigidbody = parentEntity.getComponent(Rigidbody);
if (!transform) {
throw new Error(`Entity ${parentEntity.name} must have a Transform component to be controlled.`);
}
if (rigidbody) {
rigidbody.addForce(force);
}
else {
transform.setPosition(transform.getPosition().add(force));
}
}
}
}
//# sourceMappingURL=Controller.component.js.map