|
|
@ -3,24 +3,20 @@ import { BEHAVIOR, ENTITIES, RAD } from './enums'; |
|
|
|
import Arc from './arc'; |
|
|
|
import Arc from './arc'; |
|
|
|
import Random from './random'; |
|
|
|
import Random from './random'; |
|
|
|
|
|
|
|
|
|
|
|
const baseConfig = { |
|
|
|
// ===== Constructor =====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Particle(parent, bounds, globalGrid, observables) { |
|
|
|
|
|
|
|
this.config = { |
|
|
|
behavior: BEHAVIOR.COHESION, |
|
|
|
behavior: BEHAVIOR.COHESION, |
|
|
|
bounds: {}, |
|
|
|
bounds, |
|
|
|
color: 'red', |
|
|
|
color: Random.color(), |
|
|
|
gridSize: 5, |
|
|
|
gridSize: 5, |
|
|
|
randomize: true, |
|
|
|
randomize: true, |
|
|
|
showArc: false, |
|
|
|
showArc: false, |
|
|
|
showVision: false, |
|
|
|
showVision: false, |
|
|
|
speed: 4, |
|
|
|
speed: 4, |
|
|
|
visionRadius: 50 |
|
|
|
visionRadius: 50 |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// ===== Constructor =====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Particle(parent, bounds, config, globalGrid) { |
|
|
|
|
|
|
|
this.config = Object.assign({}, baseConfig, config); |
|
|
|
|
|
|
|
this.config.color = Random.color(); |
|
|
|
|
|
|
|
// this.config.bounds = bounds;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.grids = { |
|
|
|
this.grids = { |
|
|
|
global: globalGrid || {}, |
|
|
|
global: globalGrid || {}, |
|
|
@ -43,10 +39,24 @@ function Particle(parent, bounds, config, globalGrid) { |
|
|
|
this.isLeader = false; |
|
|
|
this.isLeader = false; |
|
|
|
|
|
|
|
|
|
|
|
this.arc = Arc.create(bounds, this.grids.global); |
|
|
|
this.arc = Arc.create(bounds, this.grids.global); |
|
|
|
// this.updateConfig(this.config);
|
|
|
|
// this.grids.global.setPoint({ x: p.arc.endX, y: p.arc.endY, type: ENTITIES.PARTICLE }, p); // USE ID?
|
|
|
|
// this.grids.global.setPoint({ x: p.arc.endX, y: p.arc.endY, type: ENTITIES.PARTICLE }, p);
|
|
|
|
|
|
|
|
// this.nextFrame(globalGrid);
|
|
|
|
this.remove$ = new Rx.Subject(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
observables.fps$ |
|
|
|
|
|
|
|
.takeUntil(this.remove$) |
|
|
|
|
|
|
|
.subscribe(this.subscribeNextFrame.bind(this)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
observables.speed$ |
|
|
|
|
|
|
|
.takeUntil(this.remove$) |
|
|
|
|
|
|
|
.subscribe(this.subscribeSpeed.bind(this)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
observables.circle$ && observables.circle$ |
|
|
|
|
|
|
|
.takeUntil(this.remove$) |
|
|
|
|
|
|
|
.subscribe(this.subscribeCircle.bind(this)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// observables.randomize$.subscribe(this.subscribeRandomize.bind(this));
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// ===== PROTOTYPE =====
|
|
|
|
// ===== PROTOTYPE =====
|
|
|
@ -56,56 +66,50 @@ Particle.prototype.remove = function() { |
|
|
|
|
|
|
|
|
|
|
|
const parent = this.nodes.container.parentNode; |
|
|
|
const parent = this.nodes.container.parentNode; |
|
|
|
parent.removeChild(this.nodes.container); |
|
|
|
parent.removeChild(this.nodes.container); |
|
|
|
|
|
|
|
this.remove$.next(); |
|
|
|
delete this.nodes; |
|
|
|
delete this.nodes; |
|
|
|
return this; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Particle.prototype.nextFrame = function() { |
|
|
|
Particle.prototype.subscribeNextFrame = function() { |
|
|
|
// this.arc = Arc.goto(this.arc, 200, 200, this.config.speed)
|
|
|
|
// this.arc = Arc.goto(this.arc, 200, 200, this.config.speed)
|
|
|
|
|
|
|
|
if (this.nodes === undefined) { |
|
|
|
|
|
|
|
console.warn('no nodes in', this.id); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.arc = Arc.step(this.arc, this.config.bounds, this.config.speed); |
|
|
|
this.arc = Arc.step(this.arc, this.config.bounds, this.config.speed); |
|
|
|
|
|
|
|
|
|
|
|
if (this.leader !== null) { |
|
|
|
if (this.leader !== null) { |
|
|
|
this.arc = Arc.follow(this.arc, this.leader.arc); |
|
|
|
// this.arc = Arc.follow(this.arc, this.leader.arc);
|
|
|
|
} else if (this.arc.length <= 0 && this.config.randomize) { |
|
|
|
} else if (this.arc.length <= 0 && this.config.randomize) { |
|
|
|
this.arc = Arc.randomize(this.arc); |
|
|
|
this.arc = Arc.randomize(this.arc); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// this.grids.vision = updateVisionGrid(this.arc, this.config, this.grids);
|
|
|
|
// this.grids.vision = updateVisionGrid(this.arc, this.config, this.grids);
|
|
|
|
// const { hazards, particles } = look(this.arc, this.grids);
|
|
|
|
// const { hazards, particles } = look(this.arc, this.grids);
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (hazards.length > 0) {
|
|
|
|
// if (hazards.length > 0) {
|
|
|
|
// this.arc = Arc.evade(this.arc);
|
|
|
|
// this.arc = Arc.evade(this.arc);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
|
|
|
|
// this.updateLeader(particles);
|
|
|
|
// this.updateLeader(particles);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const prevX = p.arc.endX;
|
|
|
|
|
|
|
|
// const prevY = p.arc.endY;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// this.grid.deletePoint({ x: prevX, y: prevY, type: ENTITIES.PARTICLE });
|
|
|
|
|
|
|
|
// this.grid.setPoint({ x: p.arc.endX, y: p.arc.endY, type: ENTITIES.PARTICLE }, p);
|
|
|
|
|
|
|
|
|
|
|
|
repaintContainer(this.nodes.container, this.arc); |
|
|
|
repaintContainer(this.nodes.container, this.arc); |
|
|
|
repaintBody(this.nodes.body, this.arc, this.isLeader); |
|
|
|
repaintBody(this.nodes.body, this.arc, this.isLeader); |
|
|
|
repaintCircle(this.nodes.circle, this.arc); |
|
|
|
repaintCircle(this.nodes.circle, this.arc); |
|
|
|
repaintVisionGrid(this.nodes.visionGrid, this.arc, this.grids); |
|
|
|
repaintVisionGrid(this.nodes.visionGrid, this.arc, this.grids); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Particle.prototype.subscriber = function({ key, value }) { |
|
|
|
Particle.prototype.subscribeSpeed = function(value) { |
|
|
|
|
|
|
|
this.config.speed = value; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Particle.prototype.updateConfig = function(config) { |
|
|
|
|
|
|
|
// Object.assign(this.config, config);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// const { showArc, showVision } = this.config;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (showArc === true && this.nodes.circle === undefined) {
|
|
|
|
|
|
|
|
// this.nodes.circle = createCircleNode(this.config);
|
|
|
|
|
|
|
|
// this.nodes.container.appendChild(this.nodes.circle);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (showArc === false && this.nodes.circle !== undefined) {
|
|
|
|
|
|
|
|
// this.nodes.container.removeChild(this.nodes.circle);
|
|
|
|
|
|
|
|
// delete this.nodes.circle;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (showVision === true && this.nodes.visionGrid === undefined) {
|
|
|
|
// if (showVision === true && this.nodes.visionGrid === undefined) {
|
|
|
|
// this.nodes.visionGrid = createVisionGridNodes(this.config, this.grids, this.nodes);
|
|
|
|
// this.nodes.visionGrid = createVisionGridNodes(this.config, this.grids, this.nodes);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
@ -113,6 +117,19 @@ Particle.prototype.updateConfig = function(config) { |
|
|
|
// if (showVision === false && this.nodes.visionGrid !== undefined) {
|
|
|
|
// if (showVision === false && this.nodes.visionGrid !== undefined) {
|
|
|
|
// delete this.nodex.visionGrid;
|
|
|
|
// delete this.nodex.visionGrid;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Particle.prototype.subscribeCircle = function(show) { |
|
|
|
|
|
|
|
if (show === false) { |
|
|
|
|
|
|
|
this.nodes.container.removeChild(this.nodes.circle); |
|
|
|
|
|
|
|
delete this.nodes.circle; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
this.nodes.circle = createCircleNode(this.config); |
|
|
|
|
|
|
|
this.nodes.container.appendChild(this.nodes.circle); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Particle.prototype.subscribeRandomize = function(value) { |
|
|
|
|
|
|
|
this.config.randomize = value; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Particle.prototype.updateLeader = function(particles) { |
|
|
|
Particle.prototype.updateLeader = function(particles) { |
|
|
@ -186,10 +203,6 @@ function createBodyNode(config) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function createCircleNode(config) { |
|
|
|
function createCircleNode(config) { |
|
|
|
if (config.showArc === false) { |
|
|
|
|
|
|
|
return undefined; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const node = document.createElement('div'); |
|
|
|
const node = document.createElement('div'); |
|
|
|
node.className = 'particle-movement-circle'; |
|
|
|
node.className = 'particle-movement-circle'; |
|
|
|
node.style.borderColor = config.color; |
|
|
|
node.style.borderColor = config.color; |
|
|
|