|
|
|
@ -61,15 +61,12 @@ Particle.prototype.remove = function() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Particle.prototype.nextFrame = function(globalGrid) { |
|
|
|
|
// Randomly change radius and rotation direction.
|
|
|
|
|
if (this.arc.length <= 0 && this.config.randomize) { |
|
|
|
|
this.arc = Arc.randomize(this.arc); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.arc = Arc.step(this.arc, this.config); |
|
|
|
|
this.arc = Arc.step(this.arc, this.config.bounds, this.config.speed); |
|
|
|
|
|
|
|
|
|
if (this.leader !== null) { |
|
|
|
|
this.arc = Arc.follow(this.arc, this.leader.arc); |
|
|
|
|
} else if (this.arc.length <= 0 && this.config.randomize) { |
|
|
|
|
this.arc = Arc.randomize(this.arc); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.grids.global = globalGrid; |
|
|
|
@ -78,6 +75,7 @@ Particle.prototype.nextFrame = function(globalGrid) { |
|
|
|
|
const { hazards, particles } = look(this.arc, this.grids); |
|
|
|
|
|
|
|
|
|
if (this.leader === null && particles.length > 0) { |
|
|
|
|
// Beware of circular leadership, where a leader sees its tail.
|
|
|
|
|
const candidates = particles |
|
|
|
|
.filter(v => v.leader ? (v.leader.id !== this.id) : true); |
|
|
|
|
|
|
|
|
@ -85,10 +83,8 @@ Particle.prototype.nextFrame = function(globalGrid) { |
|
|
|
|
|
|
|
|
|
if (leader !== undefined) { |
|
|
|
|
leader.isLeader = true; |
|
|
|
|
|
|
|
|
|
// console.log(`${this.id} is now following ${leader.id}`);
|
|
|
|
|
|
|
|
|
|
this.isLeader = false; |
|
|
|
|
this.leader = leader; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -134,15 +130,13 @@ Particle.prototype.updateLeader = function() { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
while (this.leader.leader !== null) { |
|
|
|
|
this.leader.isLeader = false; |
|
|
|
|
if (this.leader.leader !== null) { |
|
|
|
|
this.leader = this.leader.leader; |
|
|
|
|
// console.error(this.id, 'is now following', this.leader.id)
|
|
|
|
|
// console.warn(`${this.id} is now following ${this.leader.id}`);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Prevents circular leadership, where a leader sees its tail.
|
|
|
|
|
if (this.leader.id === this.id) { |
|
|
|
|
this.leader = null; |
|
|
|
|
if (this.leader !== null && this.isLeader) { |
|
|
|
|
this.isLeader = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -196,7 +190,7 @@ function createContainerNode(config, id) { |
|
|
|
|
function createVisionGrid(config) { |
|
|
|
|
const { gridSize: side, visionRadius: radius } = config; |
|
|
|
|
const r0 = radius; |
|
|
|
|
const r1 = 30; |
|
|
|
|
const r1 = 45; |
|
|
|
|
|
|
|
|
|
const points = []; |
|
|
|
|
|
|
|
|
|