topdownController
Permet à un objet d'être controllé par un joueur
require
alive()body()triggers on self
"sleep""awake"parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
upKey | string | "up" | |
leftKey | string | "left" | |
rightKey | string | "right" | |
downKey | string | "down" | |
moveSpeed | number | 480 | |
rotationDelay | number | 0.04 | |
accelerationtionDelay | number | 0.04 | |
sleepDelay | number | 2 | |
rotation | number | -90 | |
startRotation | number | 90 |
example
topdownController({ upKey: "up", leftKey: "left", rightKey: "right", downKey: "down", moveSpeed: 480, rotationDelay: 0.04, accelerationtionDelay: 0.04, sleepDelay: 2, rotation: -90, startRotation: 90}),function topdownController(p) { const param = { upKey: "up", leftKey: "left", rightKey: "right", downKey: "down", moveSpeed: 480, rotationDelay: 0.04, accelerationtionDelay: 0.04, sleepDelay: 2, rotation: -90, startRotation: 90, ...p } return { id: "player", direction: vec2(0, 0), require: ["alive", "body"], moveSavedTime: 0, asleep: false, acceleration: 0, add() { this.gravityScale = 0 this.moveSavedTime = time() this.direction = Vec2.fromAngle(param.startRotation) }, update() { if (this.isAlive) { const keyDown = isKeyDown([param.leftKey, param.rightKey, param.upKey, param.downKey]) if (keyDown) { const inputDirection = (() => { let t = vec2(0.0, 0.0) if (isKeyDown(param.leftKey) && isKeyDown(param.rightKey)) t.x = 0.0 else if (isKeyDown(param.leftKey)) t.x = -1 else if (isKeyDown(param.rightKey)) t.x = 1 if (isKeyDown(param.upKey) && isKeyDown(param.downKey)) t.y = 0.0 else if (isKeyDown(param.upKey)) t.y = -1 else if (isKeyDown(param.downKey)) t.y = 1 return Vec2.fromAngle(t.angle()) })() tween(this.direction, inputDirection, param.rotationDelay, (p) => (this.direction = p), easings.easeInQuad) } const acceleration = keyDown ? 1 : 0 tween(this.acceleration, acceleration, param.accelerationtionDelay, (p) => (this.acceleration = p), easings.easeInQuad) const s = time() - this.moveSavedTime > param.sleepDelay; this.angle = this.direction.angle() + param.rotation this.move(this.direction.scale(param.moveSpeed * this.acceleration)) if (this.acceleration > 0.1) this.moveSavedTime = time() if (!this.asleep && s) this.trigger("sleep") else if (this.asleep && !s) this.trigger("awake") this.asleep = s } } }}