class Eye { float d = 35; int cooldown = 0; Particle target; Particle location; Spring s; Eye(Particle l, Particle t, Spring s) { location = l; target = t; this.s = s; s.turnOff(); } void update() { if (canJump() && (cooldown == 0)) { s.turnOn(); } else { s.turnOff(); } limitVelocity(); if(cooldown > 0) { cooldown--; } if(pdist(location, target) < d/2) { cooldown = 255; } } void draw() { strokeWeight(20); pushMatrix(); translate(location.position().x(), location.position().y()); scaleByVelocity(); fill(255); ellipse(0,0,25,25); float angleTowardsMouse = atan2(target.position().y() - location.position().y(), target.position().x() - location.position().x()); rotate(angleTowardsMouse); if(cooldown < 128) { drawEyeLid(); } popMatrix(); } void drawEyeLid() { fill(255); strokeWeight(0); float eyeLidAngle = map(cooldown, 128, 255, 0, PI/2); arc(0,0,d,d,eyeLidAngle,-eyeLidAngle); // Pupil translate(d/2,0); fill(0); ellipse(0,0,d/2,d/2); } void scaleByVelocity() { PVector p = new PVector(location.velocity().x(), location.velocity().y()); if(p.x != 0 && p.y != 0) { p.normalize(); p.x = constrain(p.x, 0.75, 1); p.y = constrain(p.y, 0.75, 1); } scale(p.x, p.y); } float pdist(Particle a, Particle b) { float dx = a.position().x() - b.position().x(); float dy = a.position().y() - b.position().y(); return sqrt(dx*dx + dy*dy); } void limitVelocity() { float vx = location.velocity().x(); float vy = location.velocity().y(); PVector pv = new PVector(vx, vy); pv.limit(5); location.setVelocity(pv.x, pv.y, 0); } boolean canJump() { return frameCount % 240 < 5; } }