diff --git a/06/animate.js b/06/animate.js index b01264b..942fb41 100644 --- a/06/animate.js +++ b/06/animate.js @@ -4,6 +4,9 @@ const HEIGHT = 800; const NCRITTERS = 10; const CLOSE = 20; const BOUNCE = 0.02; +const STUN = 10; +const MIN_RADIUS = 2; +const HIDE_SPEED = 10.5; const pointer = { x: 0, y: 0 }; @@ -41,6 +44,16 @@ class Critter { } } 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" ) { this.tick--; if( this.tick === 0 ) { @@ -91,9 +104,16 @@ document.addEventListener('DOMContentLoaded', () => { if( lights === "on" ) { lights = "off"; dark.setAttribute("fill-opacity", "0.8"); + for( const c of critters ) { + c.status = "go"; + } } else { lights = "on"; dark.setAttribute("fill-opacity", "0"); + for( const c of critters ) { + c.status = "hide"; + c.tick = Math.random() * 10 + 15; + } } }); } else {