Charger une image
load.jsconst PNG = [ "ball" , "grass" , "bean" , ]
Charger des effets sonores
load.jsconst MP3 = [
"throw" , "bong" ,"wooosh" , "off" , ]
Créer des composants
component.js// Ajoutes ici tes propres composants
function shootBallController( shootKey = "space" ) { const marginX = 40 const marginY = 40 const ballSpeed = 800 const direction = vec2(1,-0.5) const duration = 4 const fadeDuration = 0.4 return { add() { onKeyPress( shootKey , () => this.shoot() ) }, shoot() { const x = this.facing < 0 ? this.pos.x - marginX : this.pos.x + marginX const y = this.pos.y - marginY const d = this.facing < 0 ? vec2(-direction.x,direction.y) : direction const ball = add([ sprite("ball"), pos(x,y), lifespan(duration,{fade:fadeDuration}), area(), body(), bouncingBall(d,ballSpeed), anchor("center"), opacity(), "projectile", ]) play("throw") }, } } function bouncingBall(direction,speed){ const bouncingFactor = 0.8 ; const frictionFactor = 0.4 ; const detune_amp = 400 ; return{ currentSpeed : 1 , currentDirection : direction , add(){ this.onCollide( "body" , (obj, col) => this.changeDirection(obj, col) ) }, update(){ this.move(this.currentDirection.scale(this.currentSpeed*speed)) if (this.isGrounded()) this.currentSpeed-= this.currentSpeed*frictionFactor*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*speed) this.currentDirection = this.currentDirection.scale(m) this.currentSpeed = this.currentSpeed * bouncingFactor this.currentVolume = this.currentVolume * bouncingFactor play("bong",{volume:this.currentSpeed,detune:this.currentSpeed*detune_amp}) }, } }
Modifier un symbole
level.jslevelConf"#": () => [ // player shootBallController(), sprite("bean"), platformerController(), health(1), character(), area(), anchor("bot"), body(), ],
les contrôles du joueur
shootBallController( "space" ),