Add cancer

This commit is contained in:
Andras Schmelczer 2023-05-21 13:51:09 +01:00
parent b547dc3339
commit 3414f38c3a
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
14 changed files with 258 additions and 161 deletions

View file

@ -1,9 +1,9 @@
export interface GameLoopSettings {
agentCount: number;
initialDeadRatio: number;
renderSpeed: number;
simulatedDelayMs: number;
aggressionFactor: number;
nextGenerationSpawnRadius: number;
nextGenerationSpawnInterval: number;
}

View file

@ -48,6 +48,8 @@ export default class GameLoop {
this.trailMapB = new ResizableTexture(this.device, this.canvasSize);
this.resize();
this.copyPipeline = new CopyPipeline(this.device);
this.commonState = new CommonState(this.device);
this.commonState.setParameters({
canvasSize: this.canvasSize,
@ -55,13 +57,12 @@ export default class GameLoop {
deltaTime: 0,
});
this.copyPipeline = new CopyPipeline(this.device);
this.agentGenerationPipeline = new AgentGenerationPipeline(
this.device,
this.commonState,
settings.agentCount
);
this.agentGenerationPipeline.spawnFirstGeneration();
this.agentPipeline = new AgentPipeline(
this.device,
@ -87,10 +88,23 @@ export default class GameLoop {
public async start(): Promise<void> {
requestAnimationFrame(this.render.bind(this));
requestAnimationFrame(this.updateCounts.bind(this));
return this.hasFinishedPromise;
}
public get aliveAgentCounts(): GenerationCounts {
private async updateCounts(): Promise<void> {
if (this.hasFinished) {
return;
}
const generationCounts = await this.agentGenerationPipeline.countAgents();
this.gameRules.updateGenerationCounts(generationCounts);
requestAnimationFrame(this.updateCounts.bind(this));
}
public get aliveAgentCounts(): {
currentGenerationCount: number;
nextGenerationCount: number;
} {
return this.gameRules.generationCounts;
}
@ -131,7 +145,15 @@ export default class GameLoop {
].forEach((pipeline) =>
pipeline.setParameters({
time,
nextGenerationAggression: this.gameRules.nextGenerationAgression,
evenGenerationAggression:
this.gameRules.nextGenerationId % 2
? -1
: this.gameRules.nextGenerationAgression,
oddGenerationAggression:
this.gameRules.nextGenerationId % 2
? this.gameRules.nextGenerationAgression
: -1,
nextGenerationId: this.gameRules.nextGenerationId,
deltaTime,
canvasSize: this.canvasSize,
...settings,
@ -141,26 +163,11 @@ export default class GameLoop {
for (let i = 0; i < settings.renderSpeed; i++) {
const commandEncoder = this.device.createCommandEncoder();
if (
this.gameRules.generationCounts.currentGenerationCount == 0 &&
this.gameRules.generationCounts.nextGenerationCount == 0
) {
this.gameRules.updateGenerationCounts(
await this.agentGenerationPipeline.spawnNextGenerationCover(
0,
settings.agentCount * (1 - settings.initialDeadRatio)
)
);
}
const spawnAction = this.gameRules.getSpawnAction(timeInSeconds, this.canvasSize);
this.gameRules.updateGenerationCounts(
await this.agentGenerationPipeline.spawnNextGenerationCircle(
spawnAction.generation,
spawnAction.count,
spawnAction.position,
settings.nextGenerationSpawnRadius
)
this.agentGenerationPipeline.spawnNextGeneration(
spawnAction.position,
spawnAction.radius,
spawnAction.generation
);
this.copyPipeline.execute(

View file

@ -0,0 +1,11 @@
import { hsl } from '../utils/colors/hsl';
import { hash } from '../utils/hash';
import { vec3 } from 'gl-matrix';
export class GamePresentation {
public getGenerationColor(generation: number): vec3 {
const hue = Math.round(hash(generation) * 360);
return hsl(hue, 100, 50);
}
}

View file

@ -8,15 +8,16 @@ import { vec2 } from 'gl-matrix';
export interface SpawnAction {
generation: number;
position: vec2;
count: number;
radius: number;
}
export class GameRules {
private static SPAWN_INTERVAL = 3;
private lastSpawnTimeInSeconds = 0;
private nextGenerationId = 0;
public generationCounts: GenerationCounts = {
public nextGenerationId = 1;
public generationCounts: {
currentGenerationCount: number;
nextGenerationCount: number;
} = {
currentGenerationCount: 0,
nextGenerationCount: 0,
};
@ -26,11 +27,14 @@ export class GameRules {
}
public getSpawnAction(timeInSeconds: number, canvasSize: vec2): SpawnAction {
if (timeInSeconds - this.lastSpawnTimeInSeconds < GameRules.SPAWN_INTERVAL) {
if (
timeInSeconds - this.lastSpawnTimeInSeconds <
settings.nextGenerationSpawnInterval
) {
return {
generation: this.nextGenerationId,
position: vec2.create(),
count: 0,
radius: 0,
};
}
@ -42,17 +46,19 @@ export class GameRules {
Random.randomBetween(0, canvasSize.x),
Random.randomBetween(0, canvasSize.y)
),
count:
settings.agentCount -
this.generationCounts.nextGenerationCount -
this.generationCounts.currentGenerationCount,
radius: settings.nextGenerationSpawnRadius,
};
}
public updateGenerationCounts({
currentGenerationCount,
nextGenerationCount,
evenGenerationCount,
oddGenerationCount,
}: GenerationCounts): void {
const nextGenerationCount =
this.nextGenerationId % 2 === 1 ? oddGenerationCount : evenGenerationCount;
const currentGenerationCount =
this.nextGenerationId % 2 === 1 ? evenGenerationCount : oddGenerationCount;
if (currentGenerationCount === 0) {
this.nextGenerationId++;
}