Tirer
Enam
Charger des images
Section intitulée « Charger des images »load.js const PNG = ["bullet","grass",]Charger un son
Section intitulée « Charger un son »load.js const MP3 = ["shoot","bim","wooosh","off",]Créer des composants
Section intitulée « Créer des composants »component.js function shootBullet(p) {const param = {speed: 1200,margin: vec2(36, 32),duration: 4,fadeDuration: 0.4,bulletSprite: "bullet",bulletWidth: 20,tag: "alive",damage: 1,ongoing: false,bulletReversed: false,collisionIgnore: ["player"],...p,}return {id: "shoot bullet",add() {this.on("shoot", (facing = 1) => {const direction = vec2(facing, 0)const x = this.pos.x + param.margin.x * facingconst y = this.pos.y - param.margin.yconst bullet = add([sprite(param.bulletSprite, { width: param.bulletWidth }),pos(x, y),lifespan(param.duration, { fade: param.fadeDuration }),move(direction.angle(vec2(0, 0)), param.speed),area({ collisionIgnore: param.collisionIgnore }),anchor("center"),opacity(),danger({ tag: param.tag, damage: param.damage, ongoing: param.ongoing, }),"projectile",])bullet.onCollide("body", () => {destroy(bullet)bullet.trigger("hit")})if ((!param.bulletReversed && facing == -1) || (param.bulletReversed && facing == 1)) bullet.flipX = true})},}}component.js function shootBulletController(p) {const param = {shootKey: "space",reloadDelay: 0,...p,}return {id: "shootBulletController",require: ["alive"],shootSavedTime: 0,add() {onKeyPress(param.shootKey, () => {if (this.isAlive && time() - this.shootSavedTime > param.reloadDelay) {this.shootSavedTime = time()this.trigger("shoot", this.facing)}})},}}component.js function danger(p) {const param = {damage: 1,collisions: ["top", "left", "bot", "right"],ongoing: false,tag: "alive",...p}return {add() {const c = param.ongoing ? "collideUpdate" : "collide"this.on(c, (obj, col) => this.checkDangerColission(obj, col))},checkDangerColission(obj, col) {if (obj.is(param.tag) && !this.hidden) {for (const c of param.collisions) {if (c == "top" && col.isTop()) obj.trigger("hit", param.damage)else if (c == "left" && col.isLeft()) obj.trigger("hit", param.damage)else if (c == "bot" && col.isBottom()) obj.trigger("hit", param.damage)else if (c == "right" && col.isRight()) obj.trigger("hit", param.damage)}}},}}modifier le joueur
Section intitulée « modifier le joueur »config.js "#": () => [ // player 1shootBulletController(),shootBullet(),sprite("bean"),platformerController(),alive(),opacity(),scale(),health(1, 4),area(),anchor("bot"),body(),respawn(),falling(),],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('shoot', 'player', () => play('shoot'))on('hit', 'projectile', (obj) => splashFX(obj.pos))on('hit', 'projectile', () => play('bim'))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'))}
Paramètres
Section intitulée « Paramètres »exampleconfig.js
shootBullet({ speed: 1200, margin: vec2(36, 32), duration: 4, fadeDuration: 0.4, bulletSprite: "bullet", bulletWidth: 20, tag: "alive", damage: 1, ongoing: false, bulletReversed: false, collisionIgnore: ["player"]}),exampleconfig.js
shootBulletController({ shootKey: "space", reloadDelay: 0}),exampleconfig.js
danger({ damage: 1, collisions: ["top", "left", "bot", "right"], ongoing: false, tag: "alive"}),