carController
parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
leftKey | string | 'left' | |
rightKey | string | 'right' | |
upKey | string | 'up' | |
downKey | string | 'down' | |
acceleration | number | 0.8 | |
deceleration | number | 0.8 | |
brakesSpeed | number | 0.2 | |
speed | number | 1000 | |
rotationSpeed | number | 1.5 | |
camRotation | boolean | true |
example
carController({ leftKey: 'left', rightKey: 'right', upKey: 'up', downKey: 'down', acceleration: 0.8, deceleration: 0.8, brakesSpeed: 0.2, speed: 1000, rotationSpeed: 1.5, camRotation: true}),function carController(p) { const param = { leftKey: 'left', rightKey: 'right', upKey: 'up', downKey: 'down', acceleration: 0.8, deceleration: 0.8, brakesSpeed: 0.2, speed: 1000, rotationSpeed: 1.5, camRotation: true, ...p, }; return { add() { let rotation = 0; let current_speed = 0; onKeyDown(param.leftKey, () => { rotation += param.rotationSpeed; }); onKeyDown(param.rightKey, () => { rotation -= param.rotationSpeed; }); if (param.camRotation) onKeyDown([param.rightKey, param.leftKey], () => setCamRot(rotation)); onUpdate(() => { const targetSpeed = isKeyDown(param.upKey) ? param.speed : 0; this.angle = -rotation; if (isKeyDown(param.upKey)) tween( current_speed, param.speed, param.acceleration, (v) => (current_speed = v) ); else if (isKeyDown(param.downKey)) tween( current_speed, 0, param.brakesSpeed, (v) => (current_speed = v) ); else tween( current_speed, 0, param.deceleration, (v) => (current_speed = v) ); this.move( -Math.sin((rotation / 360) * 6.28) * current_speed, -Math.cos((rotation / 360) * 6.28) * current_speed ); }); }, };}