aboutsummaryrefslogtreecommitdiffhomepage
path: root/desktop/sources/scripts/programs/w.js
blob: 767a3c0499bc7e3323a09e6542a7476f97f8451d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict'

function program_W (x, y) {
  Program_Default.call(this, x, y)

  this.name = 'warp'
  this.glyph = 'w'
  this.ports = [{ x: 0, y: -1 }, { x: 0, y: 1, output: true }, { x: 0, y: 0, bang: true }]

  this.operation = function () {
    const input = this.up()
    const active = this.bang() || this.neighbors('1').length > 0

    if (input && active) {
      const warp = this.find_warp(this)
      if (!warp) { return }
      pico.program.add(warp.x, warp.y + 1, input.glyph)
      pico.program.remove(this.x, this.y - 1)
      pico.program.lock(warp.x, warp.y + 1)
    }
  }

  this.find_warps = function (origin) {
    const a = []
    let y = 0
    while (y < pico.program.h) {
      let x = 0
      while (x < pico.program.w) {
        if (pico.program.glyph_at(x, y) == 'w') {
          a.push({ x: x, y: y })
        }
        x += 1
      }
      y += 1
    }
    return a
  }

  this.find_warp = function (origin) {
    const warps = this.find_warps(origin)

    if (warps.length < 2) { return }

    let warp_id = -1
    for (const id in warps) {
      const warp = warps[id]
      if (warp.x == this.x && warp.y == this.y) {
        warp_id = id
      }
    }
    return warps[(warp_id + 1) % warps.length]
  }
}