jumpController
Permet à un objet d'être controllé par un joueur
require
alive()body()triggers on self
"moved""jump"parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
jumpKey | array | ['up', 'space'] | |
keyPressToJump | boolean | true | |
mousePressToJump | boolean | false | |
wheelRollToJump | boolean | false | |
jumpForce | number | 1000 | |
airjumpForce | number | 800 | |
doubleJump | number | 0 | |
rollJumpDelay | number | 0.2 |
example
jumpController({ jumpKey: ['up', 'space'], keyPressToJump: true, mousePressToJump: false, wheelRollToJump: false, jumpForce: 1000, airjumpForce: 800, doubleJump: 0, rollJumpDelay: 0.2}),function jumpController(p) { const param = { jumpKey: ['up', 'space'], keyPressToJump: true, mousePressToJump: false, wheelRollToJump: false, jumpForce: 1000, airjumpForce: 800, doubleJump: 0, rollJumpDelay: 0.2, ...p, }; return { id: 'jumpController', jumpCount: 0, require: ['alive', 'body'], isPassingThrough: 0, jumpReduction: 0, add() { const tryJump = () => { if (this.isAlive && !this.isClimbing) { if (this.isGrounded()) { const surfaceJumpReduction = (() => { if (!this.isGrounded()) return 0; else return this.curPlatform().jumpReduction || 0; })(); this.jump(param.jumpForce / (surfaceJumpReduction + 1)); this.trigger('moved'); this.trigger('jump'); } else if (this.jumpCount < param.doubleJump) { this.jump(param.airjumpForce); this.jumpCount++; this.trigger('moved'); this.trigger('double jump', { facing: facing }); } } }; let facing = 1; if (param.keyPressToJump) onKeyPress(param.jumpKey, () => tryJump()); if (param.mousePressToJump) onMousePress(() => tryJump()); if (param.wheelRollToJump) { let savedTime = time(); let lastDelta = 0; onScroll((delta) => { const d = delta.y / Math.abs(delta.y) || 0; const t = time() - savedTime < param.rollJumpDelay; const r = d != lastDelta; if (r && t) tryJump(); lastDelta = d; savedTime = time(); }); } this.onGround(() => (this.jumpCount = 0)); this.on('facing', (p = vec2(0, 0)) => (facing = p.unit().x)); }, };}