OK it's sort of done

This commit is contained in:
Mike Lynch 2026-01-06 21:36:32 +11:00
parent e93fd30849
commit 1500d4ee55

View File

@ -4,6 +4,9 @@ const HEIGHT = 800;
const NCRITTERS = 10; const NCRITTERS = 10;
const CLOSE = 20; const CLOSE = 20;
const BOUNCE = 0.02; const BOUNCE = 0.02;
const STUN = 10;
const MIN_RADIUS = 2;
const HIDE_SPEED = 10.5;
const pointer = { x: 0, y: 0 }; const pointer = { x: 0, y: 0 };
@ -41,6 +44,16 @@ class Critter {
} }
} }
update() { update() {
if( this.status === "hide" ) {
if( this.tick > STUN ) {
this.tick--;
this.elt.setAttribute("r", this.tick + MIN_RADIUS);
} else {
this.hide_vec = Math.atan2(this.y - pointer.y, this.x - pointer.x);
this.vx = Math.cos(this.hide_vec) * HIDE_SPEED;
this.vy = Math.sin(this.hide_vec) * HIDE_SPEED;
}
}
if( this.status === "collide" ) { if( this.status === "collide" ) {
this.tick--; this.tick--;
if( this.tick === 0 ) { if( this.tick === 0 ) {
@ -91,9 +104,16 @@ document.addEventListener('DOMContentLoaded', () => {
if( lights === "on" ) { if( lights === "on" ) {
lights = "off"; lights = "off";
dark.setAttribute("fill-opacity", "0.8"); dark.setAttribute("fill-opacity", "0.8");
for( const c of critters ) {
c.status = "go";
}
} else { } else {
lights = "on"; lights = "on";
dark.setAttribute("fill-opacity", "0"); dark.setAttribute("fill-opacity", "0");
for( const c of critters ) {
c.status = "hide";
c.tick = Math.random() * 10 + 15;
}
} }
}); });
} else { } else {