Charger une image
load.js
const PNG = [
"bullet",
"grass",
]
Charger des effets sonores
load.js
const MP3 = [
"shoot",
"bim",
"wooosh",
"off",
"hit",
]
Créer deux composants
component.js
// Ajoutes ici tes propres composants
function shootBulletController(p) {
// permet à un objet de lancer une balle lorsqu'un joueur appuie sur shootKey
const param = {
shootKey: "space",
reloadDelay: 0,
...p
}
return {
id: "shootBulletController",
require: ["alive"],
shootSavedTime: 0,
add() {
onKeyPress(param.shootKey, () => this.shootBulletTrigger())
},
shootBulletTrigger() {
if (this.isAlive && time() - this.shootSavedTime > param.reloadDelay) {
this.shoot(this.facing)
this.shootSavedTime = time()
}
},
}
}
function shootBullet(p) {
// permet à un objet de lancer une balle lorsqu'un joueur appuie sur shootKey
const param = {
speed: 1200,
margin: vec2(36, 32),
duration: 4,
fadeDuration: 0.4,
danger: true,
...p
}
return {
id: "shootBullet",
require: ["alive"],
shoot(facing = 1) {
if (this.isAlive) {
const direction = vec2(facing, 0)
const x = this.pos.x + param.margin.x * facing
const y = this.pos.y - param.margin.y
let ball
if (param.danger) ball = add([
sprite("bullet"),
pos(x, y),
lifespan(param.duration, { fade: param.fadeDuration }),
move(direction.angle(vec2(0, 0)), param.speed),
area(),
anchor("center"),
opacity(),
danger(),
"projectile",
])
else ball = add([
sprite("bullet"),
pos(x, y),
lifespan(param.duration, { fade: param.fadeDuration }),
move(direction.angle(vec2(0, 0)), param.speed),
area(),
anchor("center"),
opacity(),
"projectile",
])
ball.onCollide("body", () => this.hitTarget(ball))
play("shoot")
}
},
hitTarget(b) {
splashFX(b.pos)
destroy(b)
play("bim")
},
}
}
Modifier un symbole
levelConf.js
tiles :
"#": () => [ // player
shootBulletController(),
shootBullet(),
sprite("bean"),
platformerController(),
alive(),
opacity(),
scale(),
health(1, 4),
area(),
anchor("bot"),
body(),
respawn(),
falling(),
coloring(),
animator(),
],