Charger une image
load.js
const PNG = [
"btfly",
"grass",
]
Créer un composant
component.js
// Ajoutes ici tes propres composants
function fly(p) {
// permet à un objet de descendre lorsqu'un autre objet est dessus
const param = {
fallSpeed: 40,
magnetic: true,
speed: 60,
wanderDistance: 0,
stopdistance: 6,
rotateDelay: 0.8,
...p
}
return {
magneticPos: null,
falling: false,
id: "fly",
wanderTarget: null,
wanderOrientation: vec2(0, 0),
add() {
this.onCollide(() => {
this.falling = true
this.setWanderTarget()
})
this.onCollideUpdate((obj, col) => this.checkFlyColission(obj, col))
this.onCollideEnd(() => this.falling = false)
this.collisionIgnore.push("fly")
},
checkFlyColission(obj, col) {
if (obj.is("body") && !obj.is("fly") && !obj.isStatic && col.isTop()) {
this.move(0, param.fallSpeed)
obj.move(0, param.fallSpeed)
this.falling = true
}
},
update() {
if (this.magneticPos == null) this.magneticPos = vec2(this.pos)
if (this.wanderTarget == null) this.setWanderTarget()
tween(this.wanderOrientation, Vec2.fromAngle(this.wanderTarget.sub(this.pos).angle()), param.rotateDelay, (v) => this.wanderOrientation = v)
if (param.magnetic) {
if (!this.falling) {
if (this.pos.dist(this.wanderTarget) > param.stopdistance) {
this.move(this.wanderOrientation.scale(param.speed))
}
else this.setWanderTarget()
}
}
},
setWanderTarget() {
this.wanderTarget = this.magneticPos.add(Vec2.fromAngle(rand(360)).scale(rand(param.wanderDistance)))
},
}
}
Déclarer un symbole
levelConf.js
const LEVEL_CONFIG = {
// paramètres du niveau
tileWidth: 64,
tileHeight: 64,
backgroundColor: "afe1ff",
gravity: 3200,
tiles: {
// listes des objets à placer dans les niveaux
"i": () => [ // papillon passif
sprite("btfly"),
area(),
anchor("bot"),
offscreen({ hide: true }),
body({ gravityScale: 0 }),
respawn(),
fly(),
],
"I": () => [ // papillon hostile
sprite("btfly"),
area(),
anchor("bot"),
offscreen({ hide: true }),
body({ gravityScale: 0 }),
coloring({ color: Color.fromHex("ff9696") }),
respawn(),
fly({ wanderDistance: 400, speed: 120, stopdistance: 30 }),
z(1),
alive(),
health(1),
danger({ tag: "player" }),
],
"#": () => [ // player
sprite("bean"),
platformerController(),
alive(),
opacity(),
scale(),
health(1, 4),
area(),
anchor("bot"),
body(),
respawn(),
falling(),
coloring(),
animator(),
],
"=": () => [ // bloc
sprite("grass"),
area(),
body({ isStatic: true }),
anchor("bot"),
offscreen({ hide: true }),
],
},
}
Placer les objets
level.js
const LEVELS = [
// liste des niveaux du jeu
{
map: `
=== i
i
i
i
i I
i
i
i
i
#
=====
`,
},
];