Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: fa99fec87f7ea9fbd1a01c0bca715ec7bf5e44b1 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import * as panzoom from './modules/panzoom.js';
import * as game from './modules/game.js';
import * as recordSheet from './modules/recordSheet.js';

globalThis.svgns = "http://www.w3.org/2000/svg";

const mapPlaceholder = document.querySelector('.map-placeholder'),
  distanceOutput = document.getElementById('status'),
  proneToggle = document.getElementById('toggle-prone-counter');

document.querySelector('object').addEventListener('load', function () {
  mapPlaceholder.remove();
  this.style.opacity = 1;
});

document
  .querySelector('#content input[type="checkbox"].visible')
  .addEventListener('input', function () {
    let divs = document.querySelectorAll('#content div');

    divs.forEach(d => {
      if (this.checked) {
        d.style.display = d.id == 'record-sheet' ? 'flex' : 'block';
      } else {
        d.style.display = 'none';
      }
    });
  });

window.addEventListener('load', () => {
  const svg = document.querySelector('object').contentDocument.querySelector('svg');

  game.start(svg);
  panzoom.start(svg);

  game.setDistanceCallback((count = '-') => {
    distanceOutput.querySelector('#hex-count').textContent = count;
    distanceOutput.style.display = count === '-' ? 'none' : 'block';
  });

  game.setProneFlagCallback(checked => proneToggle.checked = checked);
  game.setSelectCallback(data => recordSheet.select(data));

  document.querySelectorAll('.soldier-record').forEach(el =>
    el.addEventListener('click', () => {
      if (el.classList.contains('selected')) {
        el.classList.remove('selected');
        game.unSelect();
        recordSheet.unSelect();
      } else {
        game.select(el);
      }
    })
  );

  document.querySelectorAll('.end-move').forEach(el => el.addEventListener('click', () => {
    recordSheet.endMove();
    game.endMove();
  }));

  document.querySelectorAll('.end-turn').forEach(el =>
    el.addEventListener('click', ({ target: { dataset: { allegiance }}}) => {
      const dataSelector = `[data-allegiance="${allegiance}"]`,
        records = Array.from(document.querySelectorAll(`.soldier-record${dataSelector}`)),
        turnCounter = document.getElementById('turn-count'),
        { dataset: { update }} = turnCounter;

      el.setAttribute('disabled', '');

      document
        .querySelector(`button.end-turn:not([data-allegiance="${allegiance}"])`)
        .removeAttribute('disabled');

      if (update === '1') {
        turnCounter.children.namedItem('count').textContent++
        turnCounter.dataset.update = '0';
      } else {
        turnCounter.dataset.update = '1';
      }

      records
        .sort((el1, el2) => el1.dataset.number > el2.dataset.number)
        .forEach(el => el.classList.remove('movement-ended'));

      game.endTurn(allegiance);
      game.select(records.at(0));
    })
  );

  document.querySelectorAll('.set-firing-arc').forEach(el =>
    el.addEventListener('click', game.setFiringArc)
  );

  document.querySelector('.set-grenade').addEventListener('click', game.setGrenade);

  document.querySelectorAll('#toggle-firing-arc-vis input').forEach(el =>
    el.addEventListener('input', game.toggleFiringArcVisibility)
  );

  document.getElementById('toggle-prone-counter').addEventListener('input', function () {
    const selected = recordSheet.getSelected();
    selected && game.toggleProne();
  });
});