Skip to content

Commit 9d8d295

Browse files
committed
spawn from dead parents
1 parent c5d98b2 commit 9d8d295

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

bug.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Bug {
2020
this.lifetime = Math.floor(Math.random() * 200);
2121
this.radius -= Math.floor(Math.random() * 4);
2222
this.trailLength = this.radius;
23-
this.ageEvery = Math.floor(Math.random() * 3000) + 5000;
23+
this.ageEvery = Math.floor(Math.random() * 3000) + 0;
2424
this.ageTimer = this.ageEvery;
2525
this.maxAge = 16 + Math.floor(Math.random() * 3);
2626
this.dead = false;

canvas.mjs

+16-12
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,36 @@ const bugCountMin = 8;
1111
export const bugCountMax = 16;
1212

1313
function tick(dt) {
14-
var trySpawnKids = 0;
14+
var trySpawnKids = [];
1515
for(var i = 0; i < bugs.length; i++)
1616
{
1717
var b = bugs[i];
1818
b.tick(dt);
1919
if(b.dead) {
2020
bugs.splice(i, 1);
21-
trySpawnKids++;
21+
trySpawnKids.push(b);
2222
}
2323
}
24-
for(var i = 0; i < trySpawnKids; i++)
24+
for(var i = 0; i < trySpawnKids.length; i++)
2525
{
26+
var par = trySpawnKids[i];
2627
if(Math.random() > 0.5 && bugs.length < bugCountMax)
2728
{
28-
var b = new Bug(Math.floor(Math.random() * WIDTH), Math.floor(Math.random() * HEIGHT))
29+
var b = new Bug(par.pos.x, par.pos.y)
2930
bugs.push(b);
3031
console.log("Dead bug had first kid");
31-
if(Math.random() > 0.5 && bugs.length < bugCountMax)
32-
{
33-
var b = new Bug(Math.floor(Math.random() * WIDTH), Math.floor(Math.random() * HEIGHT))
34-
bugs.push(b);
35-
console.log("Dead bug had second kid");
36-
}
3732
}
38-
else {
39-
console.log("Dead bug had no kids");
33+
if(Math.random() > 0.5 && bugs.length < bugCountMax)
34+
{
35+
var b2 = new Bug(par.pos.x, par.pos.y)
36+
bugs.push(b2);
37+
console.log("Dead bug had second kid");
38+
}
39+
if(Math.random() > 0.5 && bugs.length < bugCountMax)
40+
{
41+
var b3 = new Bug(par.pos.x, par.pos.y)
42+
bugs.push(b3);
43+
console.log("Dead bug had third kid");
4044
}
4145
}
4246
if(bugs.length < bugCountMin) {

0 commit comments

Comments
 (0)