class Pulser { int x, y, followDist; float radius, currRadius, pulseAmt; boolean follow, pulsing; color c; Pulser (int ix, int iy, float r, int fd, float pa, color ic) { x = ix; y = iy; radius = currRadius = r; followDist = fd + int(r); pulseAmt = pa; c = ic; follow = false; pulsing = false; } void update() { if ( follow ) { float buffer = min(followDist/2, radius+10); if ( distFrom(pl) < buffer ) { // println(" Avoiding the player."); avoid(pl); } else if ( distFrom(pl) > followDist ) { // println(" Cohering to the player."); cohere(pl); } for (int i = 0; i < pulsers.length; i++) { if ( pulsers[i] == this ) break; while ( distFrom(pulsers[i]) < (pulsers[i].currRadius + currRadius + 5.0) ) { // println(" Avoiding pulser " + i); avoid(pulsers[i]); } } } if ( distFrom(pl) < 50 ) { c = color(0, 200, 0); follow = true; } } void render() { noStroke(); fill(c); ellipse(x, y, currRadius*2, currRadius*2); if ( pulsing ) { currRadius -= 2; if ( currRadius < radius ) { currRadius = radius; pulsing = false; } } } void pulse() { if ( !pulsing ) { pulsing = true; currRadius = radius * pulseAmt; } } float distFrom(Pulser p) { return dist(p.x, p.y, x, y); } float distFrom(Player p) { return dist(p.x, p.y, x, y); } void avoid(Pulser p) { x += dist(p.x, p.y, x+1, y) > distFrom(p) ? 1 : -1; y += dist(p.x, p.y, x, y+1) > distFrom(p) ? 1 : -1; } void avoid(Player p) { int inc = p.speed; x += dist(p.x, p.y, x+inc, y) > distFrom(p) ? inc : -inc; y += dist(p.x, p.y, x, y+inc) > distFrom(p) ? inc : -inc; } void cohere(Player p) { int inc = p.speed; x += dist(p.x, p.y, x+inc, y) < distFrom(p) ? inc : -inc; y += dist(p.x, p.y, x, y+inc) < distFrom(p) ? inc : -inc; } void wander() { // when not following, randomly meander around the playfield. } }