@needle-tools/car-physics
Version:
Car physics for Needle Engine: Create physical cars with ease
55 lines • 1.85 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Behaviour, Mathf, serializable } from '@needle-tools/engine';
import { CarPhysics } from './CarPhysics';
export class CarTouchControls extends Behaviour {
carPhysics;
steerLeftState = 0;
steerRightState = 0;
throttleState = 0;
breakState = 0;
update() {
this.throttleInput();
this.steerInput();
}
throttleInput() {
this.carPhysics?.accelerationImpulse(Mathf.clamp(this.throttleState + this.breakState, -1, 1));
}
steerInput() {
this.carPhysics?.steerImpulse(Mathf.clamp(this.steerLeftState + this.steerRightState, -1, 1));
}
// ---
steerLeftPress() {
this.steerLeftState = -1;
}
steerLeftRelease() {
this.steerLeftState = 0;
}
steerRightPress() {
this.steerRightState = 1;
}
steerRightRelease() {
this.steerRightState = 0;
}
throttlePress() {
this.throttleState = 1;
}
throttleRelease() {
// do nothing
}
brakePress() {
this.throttleState = 0;
this.breakState = -1;
}
brakeRelease() {
this.breakState = 0;
}
}
__decorate([
serializable(CarPhysics)
], CarTouchControls.prototype, "carPhysics", void 0);
//# sourceMappingURL=CarTouchControls.js.map