29 lines
1,007 B
TypeScript
29 lines
1,007 B
TypeScript
import { Agent } from '../pipelines/agents/agent';
|
|
import { settings } from '../settings';
|
|
import { Random } from '../utils/random';
|
|
|
|
import { vec2 } from 'gl-matrix';
|
|
|
|
export const spawnAgents = (canvasSize: vec2, agentCount: number): Array<Agent> => {
|
|
const minSize = Math.min(...canvasSize);
|
|
const ratio = Math.max(...canvasSize) / minSize;
|
|
const size = vec2.scale(vec2.create(), canvasSize, 1 / minSize);
|
|
vec2.normalize(size, size);
|
|
return new Array(agentCount).fill(0).map(() => {
|
|
const radius = Random.randomBetween(0, settings.startingRadius / ratio);
|
|
const angle = Random.randomBetween(0, Math.PI * 2);
|
|
const center = vec2.fromValues(0.5, 0.5);
|
|
|
|
const delta = vec2.fromValues(Math.cos(angle) * radius, Math.sin(angle) * radius);
|
|
vec2.divide(delta, delta, size);
|
|
|
|
const position = vec2.add(vec2.create(), center, delta);
|
|
|
|
return {
|
|
position,
|
|
angle: angle + Math.PI,
|
|
species: 0,
|
|
timeToLive: Random.randomBetween(10, 15000),
|
|
};
|
|
});
|
|
};
|