Refactor & improve
This commit is contained in:
parent
b51cba28ad
commit
b3d9229af5
22 changed files with 714 additions and 335 deletions
29
src/game-loop/spawn-agents.ts
Normal file
29
src/game-loop/spawn-agents.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
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),
|
||||
};
|
||||
});
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue