|
|
|
@ -5,13 +5,13 @@ import Store from './store'; |
|
|
|
|
const random = { |
|
|
|
|
bool: (weight) => Math.random() < (weight || 0.5), |
|
|
|
|
color: () => `rgb(
|
|
|
|
|
${Math.floor(Math.random() * 170)}, |
|
|
|
|
${Math.floor(Math.random() * 170)}, |
|
|
|
|
${Math.floor(Math.random() * 170)} |
|
|
|
|
${Math.floor(Math.random() * 230)}, |
|
|
|
|
${Math.floor(Math.random() * 230)}, |
|
|
|
|
${Math.floor(Math.random() * 230)} |
|
|
|
|
)`,
|
|
|
|
|
id: () => String.fromCharCode( |
|
|
|
|
random.num(65, 90), random.num(97, 122), random.num(97, 122), |
|
|
|
|
random.num(97, 122), random.num(97, 122), random.num(97, 122) |
|
|
|
|
random.num(65, 90), random.num(97, 122), random.num(97, 122) |
|
|
|
|
// random.num(97, 122), random.num(97, 122), random.num(97, 122)
|
|
|
|
|
), |
|
|
|
|
num: (min, max) => min + Math.round(Math.random() * (max - min)), |
|
|
|
|
} |
|
|
|
@ -24,12 +24,12 @@ function Particle(parent, bounds, config, globalGrid) { |
|
|
|
|
behavior: BEHAVIOR.COHESION, |
|
|
|
|
bounds, |
|
|
|
|
color: random.color(), |
|
|
|
|
gridSize: 10, |
|
|
|
|
gridSize: 5, |
|
|
|
|
randomize: true, |
|
|
|
|
showMovementCircle: false, |
|
|
|
|
showVisionGrid: false, |
|
|
|
|
speed: 4, |
|
|
|
|
visionRadius: 200 |
|
|
|
|
visionRadius: 50 |
|
|
|
|
}, config) |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
@ -99,14 +99,15 @@ Particle.prototype.nextFrame = function(globalGrid) { |
|
|
|
|
if (leader !== undefined) { |
|
|
|
|
leader.isLeader = true; |
|
|
|
|
|
|
|
|
|
console.warn(`${particles[0].id} is now a leader`); |
|
|
|
|
console.log(`${this.id} is now following ${leader.id}`); |
|
|
|
|
// console.log(`${this.id} is now following ${leader.id}`);
|
|
|
|
|
|
|
|
|
|
this.isLeader = false; |
|
|
|
|
this.leader = leader; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.updateLeader(); |
|
|
|
|
|
|
|
|
|
// if (hazards.length) {
|
|
|
|
|
// this.arc = evade(this.arc, this.grids.vision);
|
|
|
|
|
// }
|
|
|
|
@ -141,6 +142,23 @@ Particle.prototype.updateConfig = function(config) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Particle.prototype.updateLeader = function() { |
|
|
|
|
if (this.leader === null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
while (this.leader.leader !== null) { |
|
|
|
|
this.leader.isLeader = false; |
|
|
|
|
this.leader = this.leader.leader; |
|
|
|
|
// console.error(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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ===== CREATION =====
|
|
|
|
|
|
|
|
|
|
function createArc(bounds, grids) { |
|
|
|
@ -199,7 +217,7 @@ function createContainerNode(config, id) { |
|
|
|
|
function createVisionGrid(config) { |
|
|
|
|
const { gridSize: side, visionRadius: radius } = config; |
|
|
|
|
const r0 = radius; |
|
|
|
|
const r1 = 20; |
|
|
|
|
const r1 = 30; |
|
|
|
|
|
|
|
|
|
const points = []; |
|
|
|
|
|
|
|
|
@ -324,11 +342,11 @@ function followArc(arc, arcToFollow) { |
|
|
|
|
arc = reverseArc(arc); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// if (Math.abs(arc.theta - arcToFollow.theta) > 0.1) {
|
|
|
|
|
// arc = moveArc(arc, 20);
|
|
|
|
|
// } else {
|
|
|
|
|
// arc = moveArc(arc, arcToFollow.radius);
|
|
|
|
|
// }
|
|
|
|
|
if (Math.abs(arc.theta - arcToFollow.theta) > 0.1) { |
|
|
|
|
arc = moveArc(arc, 20); |
|
|
|
|
} else { |
|
|
|
|
arc = moveArc(arc, arcToFollow.radius); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return arc; |
|
|
|
|
} |
|
|
|
|