Messages
Yanis & Sullivan
Affiche des messages à l’adresse du joueur
Charger une image
Section intitulée « Charger une image »load.js const PNG = ["paper","grass",]Charger un son
Section intitulée « Charger un son »load.js const MP3 = ["bloup","wooosh","off",]Créer des composants
Section intitulée « Créer des composants »component.js function message(p) {const param = {defaultText: "!",size: 60,margin: vec2(0, -100),showDistance: 60,tag: "player",showAnimDuration: 0.2,hideAnimDuration: 0.2,rotationAnimAmp: 20,rotationAnimCount: 3,...p}return {message: null,shown: false,animated: false,id: "message",add() {this.message = add([text(param.defaultText, { size: param.size }),pos(this.pos.add(param.margin)),anchor("bot"),scale(0),rotate(),])},update() {const viewers = get(param.tag, { recursive: true })let shown = falsefor (const e of viewers) if (this.pos.dist(e.pos) <= param.showDistance) shown = trueif (shown && !this.shown && !this.animated) this.showMessage()if (!shown && this.shown && !this.animated) this.hideMessage()this.message.hidden = !this.shown},showMessage() {tween(0, 1, param.showAnimDuration, (v) => this.message.scaleTo(v), easings.easeOutSine)tween(0, 1, param.showAnimDuration, (v) => this.message.angle = Math.sin(v * Math.PI * param.rotationAnimCount) * param.rotationAnimAmp, easings.easeOutSine)this.animated = truethis.shown = truewait(param.showAnimDuration, () => this.animated = false)this.trigger("show message")},hideMessage() {tween(1, 0, param.hideAnimDuration, (v) => this.message.scaleTo(v), easings.easeOutSine)this.animated = truewait(param.hideAnimDuration, () => {this.animated = falsethis.shown = false})this.trigger("hide message")},}}Déclarer un objet
Section intitulée « Déclarer un objet »config.js const LEVEL_CONFIG = {// paramètres du niveautileWidth: 64,tileHeight: 64,backgroundColor: "afe1ff",gravity: 3200,tiles: {"!": () => [ // textsprite("paper"),message(),anchor("bot"),offscreen({ hide: true }),z(-1),],"#": () => [ // player 1sprite("bean"),platformerController(),alive(),opacity(),scale(),health(1, 4),area(),anchor("bot"),body(),respawn(),falling(),],"=": () => [ // blocksprite("grass"),area(),body({ isStatic: true }),anchor("bot"),offscreen({ hide: true }),],},}Modifier la scène
Section intitulée « Modifier la scène »game.js scene("game", () => {const config = { ...LEVEL_CONFIG, ...LEVELS[CURRENT_LEVEL].config }const map = LEVELS[CURRENT_LEVEL].map.split("\n")const level = addLevel(map, config)add([setMessage(config),multiplayerCamera(),])setGravity(config.gravity)setBackground(config.backgroundColor)on('jump', 'player', () => play('wooosh'))on('drop', 'player', () => play('off'))on('respawn', 'player', (obj) => obj.play('idle'))on('sleep', 'player', (obj) => obj.play('sleep'))on('awake', 'player', (obj) => obj.play('idle'))on('jump', 'player', (obj) => obj.play('jump', { speed: 4, onEnd: () => obj.play('idle') }))on('drop', 'player', (obj) => obj.play('worry'))}Lancer une animation
Section intitulée « Lancer une animation »game.js scene("game", () => {const config = { ...LEVEL_CONFIG, ...LEVELS[CURRENT_LEVEL].config }const map = LEVELS[CURRENT_LEVEL].map.split("\n")const level = addLevel(map, config)add([multiplayerCamera(),])setGravity(config.gravity)setBackground(config.backgroundColor)on('show message', 'message', () => play('bloup'))on('jump', 'player', () => play('wooosh'))on('drop', 'player', () => play('off'))on('respawn', 'player', (obj) => obj.play('idle'))on('sleep', 'player', (obj) => obj.play('sleep'))on('awake', 'player', (obj) => obj.play('idle'))on('jump', 'player', (obj) => obj.play('jump', { speed: 4, onEnd: () => obj.play('idle') }))on('drop', 'player', (obj) => obj.play('worry'))}Placer les objets
Section intitulée « Placer les objets »level.js const LEVELS = [{map: `# ! !==============`,},]Rédiger les messages
Section intitulée « Rédiger les messages »level.js const LEVELS = [{map: `# ! !==============`,config:{messages:["oh hi,","oh bye!",],},},];
Paramètres
Section intitulée « Paramètres »exampleconfig.js
message({ defaultText: "!", size: 60, margin: vec2(0, -100), showDistance: 60, tag: "player", showAnimDuration: 0.2, hideAnimDuration: 0.2, rotationAnimAmp: 20, rotationAnimCount: 3}),