|
|
|
@ -23,8 +23,8 @@ function Particle(parent, bounds, config, globalGrid) { |
|
|
|
|
}, config); |
|
|
|
|
|
|
|
|
|
this.grids = { |
|
|
|
|
global: globalGrid, |
|
|
|
|
vision: calculateVisionGrid(this.config) |
|
|
|
|
global: globalGrid || {}, |
|
|
|
|
vision: createVisionGrid(this.config) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
this.arc = { |
|
|
|
@ -62,13 +62,14 @@ Particle.prototype.remove = function() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Particle.prototype.nextFrame = function() { |
|
|
|
|
// this.visionGrid = updateVisionGrid(this.visionGrid, this.globalGrid, this.particle);
|
|
|
|
|
this.arc = updateArc(this.arc, this.config); |
|
|
|
|
this.grids.vision = updateVisionGrid(this.arc, this.grids); |
|
|
|
|
|
|
|
|
|
repaintContainer(this.nodes.container, this.arc); |
|
|
|
|
repaintBody(this.nodes.body, this.arc); |
|
|
|
|
repaintCircle(this.nodes.circle, this.arc); |
|
|
|
|
// repaintVision(this.arc, this.nodes.vision, this.visionGrid, this.visionGridDivs);
|
|
|
|
|
repaintVision(this.nodes.vision, this.arc); |
|
|
|
|
repaintVisionGrid(this.nodes.visionGrid, this.grids, this.config); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Particle.prototype.updateConfig = function(config) { |
|
|
|
@ -88,27 +89,18 @@ Particle.prototype.updateConfig = function(config) { |
|
|
|
|
|
|
|
|
|
if (showVisionGrid === true && this.nodes.vision === undefined) { |
|
|
|
|
this.nodes.vision = createVisionNode(config, this.grids); |
|
|
|
|
// visionNode.appendChild(div);
|
|
|
|
|
this.nodes.visionGrid = createVisionGridNodes(this.config, this.grids, this.nodes); |
|
|
|
|
this.nodes.container.appendChild(this.nodes.vision); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (showMovementCircle === false && this.nodes.circle !== undefined) { |
|
|
|
|
if (showVisionGrid === false && this.nodes.vision !== undefined) { |
|
|
|
|
this.nodes.container.removeChild(this.nodes.vision); |
|
|
|
|
delete this.nodes.vision; |
|
|
|
|
delete this.nodex.visionGrid; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// if (config.showVisionGrid === false && this.nodes.vision !== undefined) {
|
|
|
|
|
// // this.nodes.container.removeChild(this.vision);
|
|
|
|
|
// // delete this.vision;
|
|
|
|
|
//
|
|
|
|
|
// // this.visionGrid.forEach(point => {
|
|
|
|
|
// // this.vision.removeChild(point.div);
|
|
|
|
|
// // });
|
|
|
|
|
// // this.visionGrid = [];
|
|
|
|
|
// }
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ===== DOM CREATION =====
|
|
|
|
|
// ===== CREATION =====
|
|
|
|
|
|
|
|
|
|
function createBodyNode(config) { |
|
|
|
|
const node = document.createElement('div'); |
|
|
|
@ -145,18 +137,51 @@ function createVisionNode(config) { |
|
|
|
|
return node; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function createVisionGridNodes(config, grids) { |
|
|
|
|
function createVisionGrid(config) { |
|
|
|
|
if (config.showVisionGrid === false) { |
|
|
|
|
return []; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const { gridSize: side, visionRadius: radius } = config; |
|
|
|
|
const r0 = Math.pow(radius, 2); |
|
|
|
|
const r1 = Math.pow(radius - side, 2); |
|
|
|
|
|
|
|
|
|
const points = []; |
|
|
|
|
|
|
|
|
|
for (let x = -radius; x <= radius; x += side) { |
|
|
|
|
for (let y = -radius; y <= radius; y += side) { |
|
|
|
|
// Half of triangle
|
|
|
|
|
if (x > -y) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Vision band
|
|
|
|
|
const p = Math.pow(x, 2) + Math.pow(y, 2); |
|
|
|
|
if (p > r0 || p < r1) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
points.push({ x, y, touch: false }); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return points; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function createVisionGridNodes(config, grids, nodes) { |
|
|
|
|
if (config.showVisionGrid === false) { |
|
|
|
|
return undefined; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return grids.visionGrid.forEach((acc, { x, y }) => { |
|
|
|
|
return grids.vision.reduce((acc, { x, y }) => { |
|
|
|
|
const div = document.createElement('div'); |
|
|
|
|
div.className = 'particle-vision-dot'; |
|
|
|
|
// div.style.backgroundColor = config.color;
|
|
|
|
|
div.style.backgroundColor = config.color; |
|
|
|
|
div.style.left = `${x + config.visionRadius}px`; |
|
|
|
|
div.style.top = `${y + config.visionRadius}px`; |
|
|
|
|
|
|
|
|
|
nodes.vision.appendChild(div); |
|
|
|
|
|
|
|
|
|
if (acc[x] === undefined) { |
|
|
|
|
acc[x] = {}; |
|
|
|
|
} |
|
|
|
@ -194,7 +219,7 @@ function updateArc(arc, { bounds, randomize, speed }) { |
|
|
|
|
arc.endX = arc.centerX + arc.radius * Math.cos(arc.theta); |
|
|
|
|
arc.endY = arc.centerY - arc.radius * Math.sin(arc.theta); |
|
|
|
|
|
|
|
|
|
// // Overflow.
|
|
|
|
|
// Overflow.
|
|
|
|
|
if (arc.endX < 0) { |
|
|
|
|
arc.endX += bounds.width; |
|
|
|
|
arc.centerX += bounds.width |
|
|
|
@ -204,7 +229,7 @@ function updateArc(arc, { bounds, randomize, speed }) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (arc.endY < 0) { |
|
|
|
|
arc.endY += bounds.height;
|
|
|
|
|
arc.endY += bounds.height; |
|
|
|
|
arc.centerY += bounds.height |
|
|
|
|
} else if (arc.endY > bounds.height) { |
|
|
|
|
arc.endY -= bounds.height; |
|
|
|
@ -214,26 +239,23 @@ function updateArc(arc, { bounds, randomize, speed }) { |
|
|
|
|
return arc; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// function updateVisionGrid(visionGrid, globalGrid, particle) {
|
|
|
|
|
// return visionGrid.reduce((acc, point) => {
|
|
|
|
|
// // Location of point on grid
|
|
|
|
|
// const x = Math.round(particle.x + point.x);
|
|
|
|
|
// const y = Math.round(particle.y + point.y);
|
|
|
|
|
//
|
|
|
|
|
// const gridX = x - x % 5;
|
|
|
|
|
// const gridY = y - y % 5;
|
|
|
|
|
//
|
|
|
|
|
// // console.warn(gridX, particle.x, point.x);
|
|
|
|
|
// point.touch = (globalGrid[gridX] !== undefined && globalGrid[gridX][gridY] !== undefined);
|
|
|
|
|
//
|
|
|
|
|
// if (point.touch) {
|
|
|
|
|
// console.warn('yay');
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return acc.concat(point);
|
|
|
|
|
// }, []);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
function updateVisionGrid(arc, grids) { |
|
|
|
|
const { global, vision } = grids; |
|
|
|
|
|
|
|
|
|
return vision.reduce((acc, point) => { |
|
|
|
|
// Location of point on grid
|
|
|
|
|
const x = Math.round(arc.endX + point.x); |
|
|
|
|
const y = Math.round(arc.endY + point.y); |
|
|
|
|
|
|
|
|
|
const gridX = x - x % 5; |
|
|
|
|
const gridY = y - y % 5; |
|
|
|
|
|
|
|
|
|
point.touch = (global[gridX] !== undefined && global[gridX][gridY] !== undefined); |
|
|
|
|
|
|
|
|
|
return acc.concat(point); |
|
|
|
|
}, []); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function moveArc(arc, newRadius) { |
|
|
|
|
const r0 = arc.radius; |
|
|
|
|
const r1 = newRadius; |
|
|
|
@ -254,36 +276,6 @@ function changeDirection(arc) { |
|
|
|
|
return arc; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function calculateVisionGrid(config) { |
|
|
|
|
if (config.showVisionGrid === false) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const { gridSize: side, visionRadius: radius } = config; |
|
|
|
|
const r0 = Math.pow(radius, 2); |
|
|
|
|
const r1 = Math.pow(radius - side, 2); |
|
|
|
|
|
|
|
|
|
const points = []; |
|
|
|
|
for (let x = -radius; x <= radius; x += side) { |
|
|
|
|
for (let y = -radius; y <= radius; y += side) { |
|
|
|
|
// Half of triangle
|
|
|
|
|
if (x > -y) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Vision band
|
|
|
|
|
const p = Math.pow(x, 2) + Math.pow(y, 2); |
|
|
|
|
if (p > r0 || p < r1) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
points.push({ x, y, touch: false }); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return points; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ===== RENDERING =====
|
|
|
|
|
function repaintContainer(node, arc) { |
|
|
|
|
node.style.left = `${arc.endX}px`; |
|
|
|
@ -312,23 +304,26 @@ function repaintCircle(node, arc) { |
|
|
|
|
node.style.borderRadius = `${arc.radius}px`; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// function repaintVision(arc, visionNode, visionGrid, visionGridDivs) {
|
|
|
|
|
// if (visionNode === undefined) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// const rad = arc.clockwise
|
|
|
|
|
// ? RAD.t180 - arc.theta
|
|
|
|
|
// : RAD.t360 - arc.theta;
|
|
|
|
|
//
|
|
|
|
|
// // visionNode.style.transform = `rotate(${rad + RAD.t45}rad)`;
|
|
|
|
|
//
|
|
|
|
|
// visionGrid.forEach(({ x, y, touch }) => {
|
|
|
|
|
// if (touch === true) {
|
|
|
|
|
// console.warn(x, y)
|
|
|
|
|
// visionGridDivs[x][y].style.background = 'black';
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
function repaintVision(node, arc) { |
|
|
|
|
if (node === undefined) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const rad = arc.clockwise |
|
|
|
|
? RAD.t180 - arc.theta |
|
|
|
|
: RAD.t360 - arc.theta; |
|
|
|
|
|
|
|
|
|
node.style.transform = `rotate(${rad + RAD.t45}rad)`; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function repaintVisionGrid(nodes, grids) { |
|
|
|
|
if (nodes === undefined) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
grids.vision.forEach(({ x, y, touch }) => { |
|
|
|
|
nodes[x][y].style.border = (touch ? '2px solid red' : '0'); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export default Particle; |
|
|
|
|