@needle-tools/car-physics
Version:
Car physics for Needle Engine: Create physical cars with ease
118 lines • 4.48 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, Camera, findObjectOfType, GameObject, serializable, SmoothFollow } from "@needle-tools/engine";
import { CarController } from "./CarController";
import { CarTouchControls } from "./CarTouchControls";
import { CarPhysics } from "./CarPhysics";
export class CarSelection extends Behaviour {
cameraRig = null;
cars;
awake() {
this.cars ??= [];
}
start() {
if (!this.cars?.length) {
this.cars = [...GameObject.findObjectsOfType(CarController)];
}
if (this.cars.length > 0) {
this.selectCarByIndex(0);
}
}
onEnable() {
this.context.input.addEventListener("keyup", this.onKey);
this.context.domElement.addEventListener("click", this.onClick);
}
onDisable() {
this.context.input.removeEventListener("keyup", this.onKey);
this.context.domElement.removeEventListener("click", this.onClick);
}
selectCar(car) {
if (!this.cars)
this.cars = [];
let index = this.cars.indexOf(car);
if (index === -1) {
this.cars.push(car);
index = this.cars.length - 1;
}
this.selectCarByIndex(index);
}
gamepadButtonDown = false;
update() {
const gamepad = navigator.getGamepads()?.[0];
if (gamepad) {
const yButton = gamepad.buttons?.[3];
if (yButton?.pressed) {
if (!this.gamepadButtonDown) {
this.gamepadButtonDown = true;
const active = this.cars.find(car => car.activeAndEnabled);
if (active) {
const activeIndex = active ? this.cars.indexOf(active) : -1;
const nextIndex = (activeIndex + 1) % this.cars.length;
this.selectCarByIndex(nextIndex);
}
}
}
else if (this.gamepadButtonDown) {
this.gamepadButtonDown = false;
}
}
}
onKey = (evt) => {
const index = parseInt(evt.key) - 1;
if (index >= 0 && index < this.cars.length) {
this.selectCarByIndex(index);
}
};
onClick = (_evt) => {
if (!this.cars?.length)
return;
if (_evt instanceof MouseEvent) {
if (_evt.button != 0)
return;
}
const hits = this.context.physics.raycast();
if (hits.length) {
const car = hits[0]?.object.getComponentInParent(CarController);
const index = car ? this.cars.indexOf(car) : -1;
if (index >= 0) {
this.selectCarByIndex(index);
}
}
};
selectCarByIndex(index) {
for (const car of this.cars) {
if (car)
car.enabled = false;
}
const car = this.cars[index];
if (car) {
car.enabled = true;
const touchControls = findObjectOfType(CarTouchControls);
if (touchControls) {
touchControls.carPhysics = car.gameObject.getComponentInChildren(CarPhysics) || undefined;
}
const camera = car.gameObject.getComponentInChildren(Camera);
if (camera) {
this.context.setCurrentCamera(camera);
}
else if (this.cameraRig) {
this.context.setCurrentCamera(this.cameraRig);
const smoothfollow = this.cameraRig.gameObject.getComponentInParent(SmoothFollow);
if (smoothfollow) {
smoothfollow.target = car.gameObject;
}
}
}
}
}
__decorate([
serializable(Camera)
], CarSelection.prototype, "cameraRig", void 0);
__decorate([
serializable(CarController)
], CarSelection.prototype, "cars", void 0);
//# sourceMappingURL=CarSelection.js.map