flappyController
Permet à un objet d'être controllé par un joueur
require
alive()body()triggers on self
"moved""jump""fly""landing""land"parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
jumpKey | string | 'up' | |
leftKey | string | 'left' | |
rightKey | string | 'right' | |
downKey | string | 'down' | |
jumpForce | number | 800 | |
moveSpeed | number | 480 | |
flySpeed | number | 680 | |
glideDelay | number | 0.1 | |
doubleJump | number | 0 | |
sleepDelay | number | 2 | |
jump | boolean | true | |
rotationDelay | number | 0.04 | |
barOffset | vec2 | vec2(0, -64) | |
barSize | vec2 | vec2(64, 10) | |
color | string | '8b57cf' | |
fuelCombustionSpeed | number | 3 | |
fuelRefillSpeed | number | 4 | |
displayBar | boolean | true | |
displayBarFadeOutDelay | number | 0.1 | |
radius | number | 4 | |
outline | number | 4 |
example
flappyController({ jumpKey: 'up', leftKey: 'left', rightKey: 'right', downKey: 'down', jumpForce: 800, moveSpeed: 480, flySpeed: 680, glideDelay: 0.1, doubleJump: 0, sleepDelay: 2, jump: true, rotationDelay: 0.04, barOffset: vec2(0, -64), barSize: vec2(64, 10), color: '8b57cf', fuelCombustionSpeed: 3, fuelRefillSpeed: 4, displayBar: true, displayBarFadeOutDelay: 0.1, radius: 4, outline: 4}),function flappyController(p) { const param = { jumpKey: 'up', leftKey: 'left', rightKey: 'right', downKey: 'down', jumpForce: 800, moveSpeed: 480, flySpeed: 680, glideDelay: 0.1, doubleJump: 0, sleepDelay: 2, jump: true, rotationDelay: 0.04, barOffset: vec2(0, -64), barSize: vec2(64, 10), color: '8b57cf', fuelCombustionSpeed: 3, fuelRefillSpeed: 4, displayBar: true, displayBarFadeOutDelay: 0.1, radius: 4, outline: 4, ...p, }; return { jumpCount: 0, require: ['alive', 'body'], isFlying: false, isLanding: false, flyFuel: 1, flappyBar: null, flyDirection: vec2(0, 0), add() { const initGravityScale = this.gravityScale; let displayBarOn = false; if (param.displayBar) { this.flappyBar = this.add([ pos(param.barOffset), rect(param.barSize.x, param.barSize.y, { radius: param.radius, }), color(Color.fromHex(param.color)), anchor('bot'), outline(param.outline), opacity(0), ]); } onKeyPress(param.jumpKey, () => { if (this.isAlive) { this.trigger('moved'); if (this.isGrounded()) { if (param.jump) { this.trigger('jump'); this.jump(param.jumpForce); } else if (this.flyFuel > 0) { this.trigger('fly'); this.isFlying = true; } } else if (this.flyFuel > 0) { this.trigger('fly'); this.isFlying = true; this.vel = vec2(0, 0); } } }); onKeyRelease(param.jumpKey, () => { this.isFlying = false; this.isLanding = true; this.trigger('landing'); }); this.on('respawn', () => (this.flyFuel = 1)); onUpdate(() => { if (this.isFlying) { this.gravityScale = 0; const keyDown = isKeyDown([ param.leftKey, param.rightKey, param.jumpKey, param.downKey, ]); if (keyDown) { const inputDirection = (() => { let t = vec2(0.0, 0.0); if (isKeyDown(param.jumpKey) && isKeyDown(param.downKey)) t.y = 0.0; else if (isKeyDown(param.jumpKey)) t.y = -1; else if (isKeyDown(param.downKey)) t.y = 1; return Vec2.fromAngle(t.angle()); })(); tween( this.flyDirection, inputDirection, param.rotationDelay, (p) => (this.flyDirection = p), easings.easeInQuad ); } this.move(this.flyDirection.scale(param.flySpeed)); this.isLanding = false; this.flyFuel = clamp( this.flyFuel - param.fuelCombustionSpeed * dt(), 0, 1 ); if (this.flyFuel == 0) this.isFlying = false; } else { this.gravityScale = initGravityScale; if (this.isGrounded()) { this.flyFuel = clamp( this.flyFuel + param.fuelRefillSpeed * dt(), 0, 1 ); if (this.isLanding) { this.isLanding = false; this.trigger('land'); } } } if (param.displayBar) { this.flappyBar.width = param.barSize.x * this.flyFuel; if (this.flappyBar.width < param.outline * 2) this.flappyBar.hidden = true; else { this.flappyBar.hidden = false; const visible = this.flyFuel < 1; if (visible && !displayBarOn) { this.flappyBar.opacity = 1; this.flappyBar.fadeIn(0.1); } else if (!visible && displayBarOn) { this.flappyBar.fadeOut(0.1); } displayBarOn = visible; } } }); }, };}