Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/game/soldier.js')
-rw-r--r--src/modules/game/soldier.js95
1 files changed, 53 insertions, 42 deletions
diff --git a/src/modules/game/soldier.js b/src/modules/game/soldier.js
index db5fd3d..8e2725d 100644
--- a/src/modules/game/soldier.js
+++ b/src/modules/game/soldier.js
@@ -25,62 +25,73 @@ function getClones(svg, counter) {
return svg.querySelectorAll(`.counter.clone${dataSelector(counter)}`);
}
-export function getAllCounters(container) {
- return container.querySelectorAll('g.counter[data-allegiance][data-number]');
-}
+function addMoveToHistory(selected) {
+ const clone = selected.cloneNode(true);
+ clone.classList.remove(selectedClass);
+ clone.classList.add('clone');
+ selected.parentElement.appendChild(clone);
-export function getCounter(svg, selected) {
- return svg.querySelector(`.counter${dataSelector(selected)}:not(.clone)`);
+ return clone;
}
-export function getTrace(svg, counter) {
- return svg.querySelector(traceSelector(counter));
-}
+function updatePlacement(cell, selected, clone) {
+ const prevCoords = [
+ clone.parentElement.dataset.x,
+ clone.parentElement.parentElement.dataset.y
+ ]
-export function place(svg, selected, cell) {
- if (svg.querySelector('.grid').contains(selected)) {
- let trace = svg.querySelector(traceSelector(selected));
+ selected.dataset.previous = prevCoords;
+ cell.appendChild(selected);
- let prevCoords = [
- selected.parentElement.dataset.x,
- selected.parentElement.parentElement.dataset.y
- ]
+ selected.childNodes.forEach(n => {
+ if (n.classList.contains('removed')) {
+ n.remove();
+ } else if ('preexisting' in n.dataset) {
+ delete n.dataset.preexisting;
+ }
+ });
+}
- let clone = selected.cloneNode(true);
- clone.classList.remove(selectedClass);
- clone.classList.add('clone');
+function createTrace(previous, current, selected) {
+ const trace = document.createElementNS(svgns, 'polyline');
- selected.dataset.previous = prevCoords;
- selected.parentElement.appendChild(clone);
- cell.appendChild(selected);
+ trace.dataset.number = selected.dataset.number;
+ trace.dataset.allegiance = selected.dataset.allegiance;
+ trace.classList.add('move-trace');
+ trace.setAttributeNS(null, 'points', `${previous.x},${previous.y} ${current.x},${current.y}`);
- selected.childNodes.forEach(n => {
- if (n.classList.contains('removed')) {
- n.remove();
- } else if ('preexisting' in n.dataset) {
- delete n.dataset.preexisting;
- }
- });
+ return trace;
+}
- let previous = getCellPosition(clone.parentElement),
- current = getCellPosition(selected.parentElement),
- points;
+export function handleTrace(svg, selected, clone, current) {
+ let trace = getTrace(svg, selected);
- if (!trace) {
- trace = document.createElementNS(svgns, 'polyline');
+ if (!trace) {
+ trace = createTrace(getCellPosition(clone.parentElement), current, selected);
+ svg.querySelector('.gameboard').prepend(trace);
+ } else {
+ const points = `${trace.getAttribute('points')} ${current.x},${current.y}`;
+ trace.setAttributeNS(null, 'points', points);
+ }
+}
- points = `${previous.x},${previous.y} ${current.x},${current.y}`;
+export function getAllCounters(container) {
+ return container.querySelectorAll('g.counter[data-allegiance][data-number]');
+}
- trace.dataset.number = selected.dataset.number;
- trace.dataset.allegiance = selected.dataset.allegiance;
- trace.classList.add('move-trace');
+export function getCounter(svg, selected) {
+ return svg.querySelector(`.counter${dataSelector(selected)}:not(.clone)`);
+}
- svg.querySelector('.gameboard').prepend(trace);
- } else {
- points = `${trace.getAttribute('points')} ${current.x},${current.y}`;
- }
+export function getTrace(svg, counter) {
+ return svg.querySelector(traceSelector(counter));
+}
- trace.setAttributeNS(null, 'points', points);
+export function place(svg, selected, cell) {
+ if (svg.querySelector('.grid').contains(selected)) {
+ const clone = addMoveToHistory(selected);
+ updatePlacement(cell, selected, clone)
+ handleTrace(svg, selected, clone, getCellPosition(cell));
} else {
selected.removeAttribute('data-x');
cell.appendChild(selected);