topdownController
Permet à un objet d'être controllé par un joueur
require
alive()body()rotate()triggers on self
"moved"parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
upKey | array | ["up", "z"] | |
leftKey | array | ["left", "q"] | |
rightKey | array | ["right", "d"] | |
downKey | array | ["down", "s"] | |
moveSpeed | number | 480 | |
rotationDelay | number | 0.04 | |
accelerationtionDelay | number | 0.04 | |
rotation | number | -90 | |
startRotation | number | 90 | |
rotateSprite | boolean | true |
example
topdownController({ upKey: ["up", "z"], leftKey: ["left", "q"], rightKey: ["right", "d"], downKey: ["down", "s"], moveSpeed: 480, rotationDelay: 0.04, accelerationtionDelay: 0.04, rotation: -90, startRotation: 90, rotateSprite: true}),function topdownController(p) { const param = { upKey: ["up", "z"], leftKey: ["left", "q"], rightKey: ["right", "d"], downKey: ["down", "s"], moveSpeed: 480, rotationDelay: 0.04, accelerationtionDelay: 0.04, rotation: -90, startRotation: 90, rotateSprite: true, ...p } return { id: "player", direction: vec2(0, 0), require: ["alive", "body", "rotate"], 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) this.trigger("facing", inputDirection) this.trigger("moved") } const acceleration = keyDown ? 1 : 0 tween(this.acceleration, acceleration, param.accelerationtionDelay, (p) => (this.acceleration = p), easings.easeInQuad) this.move(this.direction.scale(param.moveSpeed * this.acceleration)) if (param.rotateSprite) this.angle = this.direction.angle() + param.rotation if (this.acceleration > 0.1) this.moveSavedTime = time() } } }}