Lancer une balle

pour Simon

Charger une image

load.js
const PNG = [ "ball", "grass", ]

Charger des effets sonores

load.js
const MP3 = [
"throw" , "bong" ,
"wooosh", "off", "hit", ]

Créer trois composants

component.js
// Ajoutes ici tes propres composants
function throwBallController(p) { // permet à un objet de lancer une balle lorsqu'un joueur appuie sur shootKey const param = { shootKey: "space", reloadDelay: 0, ...p } return { id: "throwBallController", require: ["alive", "throwBall"], throwSavedTime: 0, add() { onKeyPress(param.shootKey, () => this.throwBallTrigger()) }, throwBallTrigger() { if (this.isAlive && time() - this.throwSavedTime > param.reloadDelay) { this.throw(this.facing) this.throwSavedTime = time() } }, } } function throwBall(p) { // permet à un objet de lancer une balle lorsqu'un joueur appuie sur shootKey const param = { speed: 800, margin: vec2(40, 40), duration: 4, fadeDuration: 0.4, danger: false, bouncing: 0.8, friction: 0.4, detuneAmp: 400, ...p } return { id: "throwBall", throw(facing = 1) { if (this.isAlive) { const dir = vec2(facing, -0.5) const x = this.pos.x + param.margin.x * facing const y = this.pos.y - param.margin.y if (param.danger) ball = add([ sprite("ball"), pos(x, y), lifespan(param.duration, { fade: param.fadeDuration }), area(), body(), bouncingBall({ direction: dir, speed: param.speed }), anchor("center"), opacity(), danger(), "projectile", ]) else ball = add([ sprite("ball"), pos(x, y), lifespan(param.duration, { fade: param.fadeDuration }), area(), body(), bouncingBall({...param,...{ direction: dir }}), anchor("center"), opacity(), "projectile", ]) play("throw") } }, } } function bouncingBall(p) { // permet à un objet de se déplacer et de rebondir const param = { direction: vec2(0, 0), speed: 0, bouncing: 0.8, friction: 0.4, detuneAmp: 400, ...p } return { currentSpeed: 1, currentDirection: param.direction, add() { this.onCollide("body", (obj, col) => this.changeDirection(obj, col)) }, update() { this.move(this.currentDirection.scale(this.currentSpeed * param.speed)) if (this.isGrounded()) { this.currentSpeed -= this.currentSpeed * param.friction * dt() } }, changeDirection(obj, col) { let m = vec2(1, 1) if (col.isTop() || col.isBottom()) m = vec2(1, -1) else if (col.isLeft() || col.isRight()) m = vec2(-1, 1) if (col.isBottom()) this.jump(this.currentSpeed * param.speed) this.currentDirection = this.currentDirection.scale(m) this.currentSpeed = this.currentSpeed * param.bouncing this.currentVolume = this.currentVolume * param.bouncing play("bong", { volume: this.currentSpeed, detune: this.currentSpeed * param.detuneAmp, }) }, } }

Modifier un symbole

levelConf.js
tiles :
"#": () => [ // player
throwBallController(), throwBall(),
sprite("bean"), platformerController(), alive(), opacity(), scale(), health(1, 4), area(), anchor("bot"), body(), respawn(), falling(), coloring(), animator(), ],

throwBallController

levelConf.js
tiles :
throwBallController({ shootKey: "space", reloadDelay: 0, }),