Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.8 kB
2
Indexable
Never
import kaboom from "kaboom"

kaboom({
  global: true,
  fullscreen: true,
  scale: 1,
  debug: true,
  background: [0, 0, 0]
})

const MOVE_SPEED = 120

loadRoot('https://i.imgur.com/')
loadSprite('link-going-left', '1Xq9biB.png')
loadSprite('link-going-right', 'yZIb8O2.png')
loadSprite('link-going-down', 'r377FIM.png')
loadSprite('link-going-up', 'UkV0we0.png')
loadSprite('left-wall', 'rfDoaa1.png')
loadSprite('top-wall', 'QA257Bj.png')
loadSprite('bottom-wall', 'vWJWmvb.png')
loadSprite('right-wall', 'SmHhgUn.png')
loadSprite('bottom-left-wall', 'awnTfNC.png')
loadSprite('bottom-right-wall', '84oyTFy.png')
loadSprite('top-left-wall', 'xlpUxIm.png')
loadSprite('top-right-wall', 'z0OmBd1.jpg')
loadSprite('top-door', 'U9nre4n.png')
loadSprite('fire-pot', 'I7xSp7w.png')
loadSprite('left-door', 'okdJNls.png')
loadSprite('lanterns', 'wiSiY09.png')
loadSprite('slicer', 'c6JFi5Z.png')
loadSprite('skeletor', 'Ei1VnX8.png')
loadSprite('kaboom', 'o9WizfI.png')
loadSprite('stairs', 'VghkL08.png')
loadSprite('bg', 'u4DVsx6.png')

scene("game", ({ level, score }) => {
  layers(['bg', 'obj', 'ui'], 'obj')

  const maps = [
    [
      'ycc)cc^ccw',
      'a        b',
      'a      * b',
      'a    (   b',
      '%        b',
      'a    (   b',
      'a   *    b',
      'a        b',
      'xdd)dd)ddz',
    ],
    [
      'yccccccccw',
      'a        b',
      ')        )',
      'a        b',
      'a        b',
      'a    $   b',
      ')   }    )',
      'a        b',
      'xddddddddz',
    ]

  ]

  const levelCfg = {
    width: 48,
    height: 48,
    'a': () => [sprite('left-wall'), solid(), 'wall', area()],
    'b': () => [sprite('right-wall'), solid(), 'wall', area()],
    'c': () => [sprite('top-wall'), solid(), 'wall', area()],
    'd': () => [sprite('bottom-wall'), solid(), 'wall', area()],
    'w': () => [sprite('top-right-wall'), solid(), 'wall', area()],
    'x': () => [sprite('bottom-left-wall'), solid(), 'wall', area()],
    'y': () => [sprite('top-left-wall'), solid(), 'wall', area()],
    'z': () => [sprite('bottom-right-wall'), solid(), 'wall', area()],
    '%': () => [sprite('left-door'), solid(), 'door', area()],
    '^': () => [sprite('top-door'), 'next-level'],
    '$': () => [sprite('stairs'), 'next-level'],
    '*': () => [sprite('slicer'), 'slicer', { dir: -1 }, 'dangerous', solid(), area()],
    '}': () => [sprite('skeletor'), 'dangerous', 'skeletor', { dir: -1, timer: 0 }],
    ')': () => [sprite('lanterns'), solid(), area()],
    '(': () => [sprite('fire-pot'), solid(), area()],
  }

  addLevel(maps[level], levelCfg)

  add([sprite('bg'), layer('bg')])

  const scoreLabel = add([
    text('0', { font: "sink" }),
    pos(400, 450),
    layer('ui'),
    {
      value: score,
    },
    scale(2)
  ])
  add([text('level ' + parseInt(level + 1), { font: "sink" }), pos(400, 485), scale(2)])

  const player = add([
    sprite('link-going-right'),
    pos(5, 190),
    solid(),
    area(),
    {
      dir: vec2(1, 0),
    }
  ])
  /*player.overlaps('next-level', () => {
    go("game", {
      level: (level + 1) % maps.length,
      score: scoreLabel.value
    })
  })*/
  keyDown('right', () => {
    player.use(sprite('link-going-right'))
    player.move(MOVE_SPEED, 0)
    player.dir = vec2(1, 0)
  })
  keyDown('left', () => {
    player.use(sprite('link-going-left'))
    player.move(-MOVE_SPEED, 0)
    player.dir = vec2(-1, 0)
  })
  keyDown('up', () => {
    player.use(sprite('link-going-up'))
    player.move(0, -MOVE_SPEED)
    player.dir = vec2(0, -1)
  })
  keyDown('down', () => {
    player.use(sprite('link-going-down'))
    player.move(0, MOVE_SPEED)
    player.dir = vec2(0, 1)
  })
  
  function spawnKaboom(p) {
    const obj = add([sprite('kaboom'), pos(p), 'kaboom'])
    wait(1, () => {
      destroy(obj)
    })
  }

  keyPress('space', () => {
    spawnKaboom(player.pos.add(player.dir.scale(48)))
  })

  player.collides('door', (d) => {
    destroy(d)
  })
  
  collides('kaboom', 'skeletor', (k, s) => {
    camShake(4)
    wait(1, () => {
      destroy(k)
    })
    destroy(s)
    scoreLabel.value++
    scoreLabel.text = scoreLabel.value
  })
  
  const SLICER_SPEED = 100

  action('slicer', (s) => {
    s.move(s.dir * SLICER_SPEED, 0)
  })
  onCollide('slicer', 'wall', (s) => {
    s.dir = -s.dir
  })

  const SKELETOR_SPEED = 60

  action('skeletor', (s) => {
    s.move(0, s.dir * SKELETOR_SPEED)
    s.timer -= dt()
    if (s.timer <= 0) {
      s.dir = - s.dir
      s.timer = rand(5)
    }
  })

  collides('skeletor', 'wall', (s) => {
    s.dir = -s.dir
  })
  
player.collides('dangerous', () => {
    go('lose', { score: scoreLabel.value })
  })
})

scene("lose", ({ score }) => {
  add([text(score , 32 ), origin('center'), pos(width() / 2, height() / 2)])
  
})

go("game", { level: 0, score: 0 })