Aller au contenu

Tirer

Enam
  1. load.js
    const PNG = [
    "bullet",
    "grass",
    ]
  2. load.js
    const MP3 = [
    "shoot",
    "bim",
    "wooosh",
    "off",
    ]
  3. 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 * facing
    const y = this.pos.y - param.margin.y
    const 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)
    }
    }
    },
    }
    }
  4. config.js
    "#": () => [ // player 1
    shootBulletController(),
    shootBullet(),
    sprite("bean"),
    platformerController(),
    alive(),
    opacity(),
    scale(),
    health(1, 4),
    area(),
    anchor("bot"),
    body(),
    respawn(),
    falling(),
    ],
  5. 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'))
    }
example
config.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"]
}),
example
config.js
shootBulletController({
shootKey: "space",
reloadDelay: 0
}),
example
config.js
danger({
damage: 1,
collisions: ["top", "left", "bot", "right"],
ongoing: false,
tag: "alive"
}),