xxxxxxxxxx
105
class Chaser{constructor(x,y){
this.pos=createVector(x,y);
//this.targ=createVector(random(width), random(height));
//this.targ=createVector(mouseX, mouseY);
this.targ=runt[0].pos;
//console.log(this.targ);
this.acceleration=createVector(0,0);
this.velocity=createVector(random(0,-2));
this.speed=random(5);
this.xoff = random(1000); // For perlin noise
this.yoff = random(1000);
this.life=300;
this.dead=false;
//this.velocity.limit(this.speed);
this.size=10;
this.visibility=width/2;
}
update()
{
this.targ=runt[0].pos;
var d =dist(this.targ.x, this.targ.y, this.pos.x, this.pos.y);
if(d<this.visibility/2)
{
this.acceleration=p5.Vector.sub(this.targ, this.pos);
//this.accelerations.setMag(this.speed*10);
this.velocity.add(this.acceleration);
this.velocity.limit(this.speed);
//this.velocity.mult(-1);
this.pos.add(this.velocity);
this.acceleration.mult(0);
}
else
{
let vx = map(noise(this.xoff), 0, 1, -this.speed, this.speed);
let vy = map(noise(this.yoff), 0, 1, -this.speed, this.speed);
var vel=createVector(vx, vy)
this.xoff += 0.01;
this.yoff += 0.01;
this.pos.add(vel);
}
if(d<this.size/2+runt[0].size/2)
{
//this.targ=createVector(random(width), random(height));
this.life+=300;
//console.log("MEH");
}
this.edges();
//Death is always looming
this.life-=0.5;
if(this.life<=0)
{
this.dead=true;
}
}
show()
{
fill(0,0,255, this.life);
circle(this.pos.x, this.pos.y, this.size);
fill('blue');
// circle(this.targ.x, this.targ.y, this.size);
stroke(0,255,0,100);
noFill();
circle(this.pos.x, this.pos.y, this.visibility);
//line(this.pos.x, this.pos.y, this.targ.x, this.targ.y);
}
edges()
{
if(this.pos.x>width) this.pos.x=0;
if(this.pos.x<0) this.pos.x=width;
if(this.pos.y>height) this.pos.y=0;
if(this.pos.y<0) this.pos.y=height;
}
}