platformerController
Permet à un objet d'être controllé par un joueur
require
alive()body()triggers on self
"jump""double jump""sleep""awake"parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
jumpKey | string | "up" | |
leftKey | string | "left" | |
rightKey | string | "right" | |
downKey | string | "down" | |
jumpForce | number | 1000 | |
moveSpeed | number | 480 | |
glideDelay | number | 0.1 | |
airjumpForce | number | 800 | |
doubleJump | number | 0 | |
sleepDelay | number | 2 |
example
platformerController({ jumpKey: "up", leftKey: "left", rightKey: "right", downKey: "down", jumpForce: 1000, moveSpeed: 480, glideDelay: 0.1, airjumpForce: 800, doubleJump: 0, sleepDelay: 2}),function platformerController(p) { const param = { jumpKey: "up", leftKey: "left", rightKey: "right", downKey: "down", jumpForce: 1000, moveSpeed: 480, glideDelay: 0.1, airjumpForce: 800, doubleJump: 0, sleepDelay: 2, ...p } return { id: "player", direction: vec2(0, 0), jumpCount: 0, facing: 1, require: ["alive", "body"], isPassingThrough: 0, moveSavedTime: 0, asleep: false, add() { onKeyPress(param.jumpKey, () => { if (this.isAlive && !this.isClimbing) { if (this.isGrounded()) this.trigger("jump") else if (this.jumpCount < param.doubleJump) this.trigger("double jump") } }) this.onGround(() => this.jumpCount = 0) this.on("jump", () => { this.jump(param.jumpForce) this.moveSavedTime = time() }) this.on("double jump", () => { this.jump(param.airjumpForce) this.jumpCount++ this.moveSavedTime = time() }) this.moveSavedTime = time() }, update() { const inputDirection = (() => { let t = vec2(0, 0) if (!this.isAlive) t.x = 0 else if (isKeyDown(param.leftKey) && isKeyDown(param.rightKey)) t.x = 0 else if (isKeyDown(param.leftKey)) t.x = -1 else if (isKeyDown(param.rightKey)) t.x = 1 return t })() const surfaceGlide = (() => { if (!this.isGrounded()) return 0 else return this.curPlatform().glide || 0 })() const s = time() - this.moveSavedTime > param.sleepDelay; tween( this.direction, inputDirection, param.glideDelay + surfaceGlide, (p) => (this.direction = p), easings.easeOutQuad ) this.move(this.direction.scale(param.moveSpeed)) if (this.direction.len() > 0.1) this.facing = this.direction.unit().x if (this.direction.len() > 0.1) this.moveSavedTime = time() if (!this.asleep && s) this.trigger("sleep") else if (this.asleep && !s) this.trigger("awake") this.asleep = s; }, }}