Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: de66c79dd652fde4d0850fa32ee8f75d0ce61bed (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var isEven = n => n % 2 === 0;
var toFixed = n => Number.parseFloat(n).toFixed(2);
var radToDeg = radians => radians * 180 / Math.PI;

let getPointCoords = (x, y) => {
  let point = document.querySelector(`[data-x="${x}"][data-y="${y}"]`);

  return [point.x.baseVal.value, point.y.baseVal.value]
};

var svgns = "http://www.w3.org/2000/svg",
    svg = document.querySelector('svg'),
    rect = document.querySelector('rect#map');

var columnCount = 33,
    rowCount = 25,
    pointDistanceInInches = 1.005;

var columns = [...Array(columnCount).keys()],
    rows = [...Array(rowCount).keys()],
    points = rows.map(y => columns.map(x => [x, y]));

var xOffset = 0.25,
    yOffset = 0.45;
    calcY = Math.sqrt(3) * pointDistanceInInches / 2,
    alternatingOffset = pointDistanceInInches / 2;

rect.addEventListener('mousemove', e => {
  var rect = e.target.getBoundingClientRect();
  var x = e.clientX - rect.left;
  var y = e.clientY - rect.top;
  let [maxX, maxY] = [e.target.width, e.target.height].map(v => v.baseVal.valueInSpecifiedUnits);

  // console.log('x', `${toFixed(x / rect.width * maxX)}"`, 'y', `${toFixed(y / rect.height * maxY)}"`);

  let aim = document.querySelector('.firing-arc.active');

  if (aim) {
    let xIntercept = y => (y - y1) * (x2 - x1) / (y2 - y1) + x1;
    let yIntercept = x => (x - x1) * (y2 - y1) / (x2 - x1) + y1;
    let newX, newY;

    let [x1, y1] = [aim.x1, aim.y1].map(v => v.baseVal.valueInSpecifiedUnits);
    let [x2, y2] = [x / rect.width * maxX, y / rect.height * maxY];

    // TODO: handle horizontal and vertical lines

    if (x2 - x1 > 0 && y2 - y1 > 0) {
      let yWhenXisMax = yIntercept(maxX);
      let xWhenYisMax = xIntercept(maxY);

      if (xWhenYisMax <= maxX) {
        newX = xWhenYisMax;
        newY = maxY;
      } else {
        newX = maxX;
        newY = yWhenXisMax;
      }
    } else if (x2 - x1 > 0 && y2 - y1 < 0) {
      let yWhenXisMax = yIntercept(maxX);
      let xWhenYisZero = xIntercept(0);

      if (xWhenYisZero <= maxX) {
        newX = xWhenYisZero;
        newY = 0;
      } else {
        newX = maxX;
        newY = yWhenXisMax;
      }
    } else if (x2 - x1 < 0 && y2 - y1 < 0) {
      let yWhenXisZero = yIntercept(0);
      let xWhenYisZero = xIntercept(0);

      if (yWhenXisZero >= 0) {
        newX = 0;
        newY = yWhenXisZero;
      } else {
        newX = xWhenYisZero;
        newY = 0;
      }
    } else {
      let yWhenXisZero = yIntercept(0);
      let xWhenYisMax = xIntercept(maxY);

      if (yWhenXisZero <= maxY) {
        newX = 0;
        newY = yWhenXisZero;
      } else {
        newX = xWhenYisMax;
        newY = maxY;
      }
    }

    aim.setAttributeNS(null, 'x2', `${newX}in`);
    aim.setAttributeNS(null, 'y2', `${newY}in`);

    let width = aim.x2.baseVal.value - aim.x1.baseVal.value;
    let height = -(aim.y2.baseVal.value - aim.y1.baseVal.value);
    let angle = Math.abs(radToDeg(Math.atan(height / width)));

    if (width < 0 && height > 0) {
      angle = 180 - angle;
    } else if (width < 0 && height < 0) {
      angle = 180 + angle;
    } else if (width > 0 && height < 0) {
      angle = 360 - angle;
    }

    console.log(`${toFixed(angle)}\u00B0`);
  }
});

document.querySelectorAll('.soldier-record').forEach(el =>
  el.addEventListener('click', e => {
    if (e.target.classList.contains('selected')) {
      e.target.classList.remove('selected');
    } else {
      document.querySelectorAll('.soldier-record.selected').forEach(el =>
        el.classList.remove('selected')
      );
      e.target.classList.add('selected');
    }
  })
);

document.querySelector('#set-firing-arc').addEventListener('click', e => {
  let selectedSoldier = document.querySelector('.soldier-record.selected');

  if (selectedSoldier) {
    let {troopNumber, troopAllegiance} = selectedSoldier.dataset;

    let firingArc = document.querySelector(
      `.firing-arc[data-troop-number="${troopNumber}"][data-troop-allegiance="${troopAllegiance}"]`
    );

    if (firingArc) { firingArc.remove() }

    let counter = document.querySelector(
      `circle.counter[data-troop-number="${troopNumber}"][data-troop-allegiance="${troopAllegiance}"]`
    );

    let aim = document.createElementNS(svgns, 'line');

    aim.classList.add('firing-arc', 'active');
    aim.dataset.troopNumber = troopNumber;
    aim.dataset.troopAllegiance = troopAllegiance;
    aim.setAttributeNS(null, 'x1', counter.cx.baseVal.valueAsString);
    aim.setAttributeNS(null, 'y1', counter.cy.baseVal.valueAsString);
    aim.setAttributeNS(null, 'x2', counter.cx.baseVal.valueAsString);
    aim.setAttributeNS(null, 'y2', counter.cy.baseVal.valueAsString);
    // aim.style.transformOrigin = `${counter.cx.baseVal.valueAsString} ${counter.cy.baseVal.valueAsString}`;

    svg.appendChild(aim);

    aim.addEventListener('click', e => {
      e.target.classList.remove('active');
      document.querySelector('circle#point').style.display = '';
    });

    document.querySelector('circle#point').style.display = 'none';
  }
});

points.forEach((row, index) => row.forEach(([x, y]) => {
  var cx = x * pointDistanceInInches + xOffset + (isEven(index) ? alternatingOffset : 0),
      cy = y * pointDistanceInInches * calcY + yOffset,
      point = document.createElementNS(svgns, 'use');

  point.setAttributeNS(null, 'href', `#point`);
  point.setAttributeNS(null, 'x', `${cx}in`);
  point.setAttributeNS(null, 'y', `${cy}in`);
  point.dataset.x = x;
  point.dataset.y = y;

  point.addEventListener('click', e => {
    let selectedSoldier = document.querySelector('.soldier-record.selected');

    if (selectedSoldier) {
      let counter = document.createElementNS(svgns, 'circle');
      let text = document.createElementNS(svgns, 'text');

      counter.setAttributeNS(null, 'cx', `${cx}in`);
      counter.setAttributeNS(null, 'cy', `${cy}in`);
      counter.setAttributeNS(null, 'r', '0.25in');
      counter.dataset.troopNumber = selectedSoldier.dataset.troopNumber;
      counter.dataset.troopAllegiance = selectedSoldier.dataset.troopAllegiance;
      counter.classList.add('counter');

      text.setAttributeNS(null, 'text-anchor', 'middle');
      text.setAttributeNS(null, 'x', `${cx}in`);
      text.setAttributeNS(null, 'y', `${cy + 0.25}in`);
      text.dataset.troopNumber = selectedSoldier.dataset.troopNumber;
      text.dataset.troopAllegiance = selectedSoldier.dataset.troopAllegiance;
      text.textContent = `${selectedSoldier.dataset.troopNumber}`;
      text.classList.add('counter');

      document.querySelectorAll(
        `.counter[data-troop-number="${selectedSoldier.dataset.troopNumber}"][data-troop-allegiance="${selectedSoldier.dataset.troopAllegiance}"]`
      ).forEach(el => el.remove());

      svg.appendChild(counter);
      svg.appendChild(text);
    }
  });

  svg.appendChild(point);
}));

let smlArc = document.createElementNS(svgns, 'polygon');
smlArc.setAttributeNS(null, 'id', 'small-arc');
let smlArcPoints = `${getPointCoords(1, 24)} ${getPointCoords(1, 21)} ${getPointCoords(2, 21)}`;
smlArc.setAttributeNS(null, 'points', smlArcPoints);

svg.appendChild(smlArc);

let medArc = document.createElementNS(svgns, 'polygon');
medArc.setAttributeNS(null, 'id', 'med-arc');
let medArcPoints = `${getPointCoords(4, 24)} ${getPointCoords(3, 21)} ${getPointCoords(6, 21)}`;
medArc.setAttributeNS(null, 'points', medArcPoints);

svg.appendChild(medArc);

let lrgArc = document.createElementNS(svgns, 'polygon');
lrgArc.setAttributeNS(null, 'id', 'lrg-arc');
let lrgArcPoints = `${getPointCoords(19, 24)} ${getPointCoords(9, 21)} ${getPointCoords(30, 21)}`;
lrgArc.setAttributeNS(null, 'points', lrgArcPoints);

svg.appendChild(lrgArc);

// arc = document.querySelector('#med-arc');
// let [ox, oy] = getPointCoords(4, 24);
// arc.style.transformOrigin = `${ox}px ${oy}px`;
// arc.style.transform = 'rotate(90deg)';

var smlArcAngle = radToDeg((Math.atan(pointDistanceInInches / (6 * calcY)) * 2));
var medArcAngle = radToDeg((Math.atan((3 * pointDistanceInInches) / (6 * calcY)) * 2));
var lrgArcAngle = radToDeg((Math.atan((21 * pointDistanceInInches) / (6 * calcY)) * 2));