platformerController
Permet à un objet d'être controllé par un joueur
require
alive()body()triggers on self
"moved""slowing start""slowing stop""gliding start""gliding stop"parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
leftKey | string | "left" | |
rightKey | string | "right" | |
moveSpeed | number | 480 | |
glideDelay | number | 0.1 | |
moveReductionDelay | number | 0.1 |
example
platformerController({ leftKey: "left", rightKey: "right", moveSpeed: 480, glideDelay: 0.1, moveReductionDelay: 0.1}),function platformerController(p) { const param = { leftKey: "left", rightKey: "right", moveSpeed: 480, glideDelay: 0.1, moveReductionDelay: 0.1, ...p, } return { id: "platformerController", direction: vec2(0, 0), require: ["alive", "body"], moveReduction: 0, isSlowedDown: false, isGliding: false, 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 surfaceMoveReduction = (() => { if (!this.isGrounded()) return 0 else return this.curPlatform().moveReduction || 0 })() tween(this.direction, inputDirection, param.glideDelay + surfaceGlide, (p) => (this.direction = p), easings.easeOutQuad) tween(this.moveReduction, surfaceMoveReduction, param.moveReductionDelay, (p) => (this.moveReduction = p), easings.easeInQuad) this.move(this.direction.scale(param.moveSpeed / (this.moveReduction + 1))) if (this.direction.len() > 0.1) this.trigger("facing", this.direction) if (this.direction.len() > 0.1) this.trigger("moved") if (!this.isSlowedDown && surfaceMoveReduction > 0) this.trigger("slowing start") else if (this.isSlowedDown && surfaceMoveReduction == 0) this.trigger("slowing stop") if (!this.isGliding && surfaceGlide > 0) this.trigger("gliding start") else if (this.isGliding && surfaceGlide == 0) this.trigger("gliding stop") this.isSlowedDown = surfaceMoveReduction > 0 this.isGliding = surfaceGlide > 0 }, }}