xxxxxxxxxx
class Orb {
constructor(){
this.location = createVector(random(300),random(500)); // object location (vector)
this.velocity = createVector(0,0); // object speed (as vector)
this.acceleration = createVector(0,0); // object acceleration
this.bsize = random(8,40);
this.topSpeed = 8-this.bsize/5;
} // ------------------------------------- end constructor
update(){
this.magnet = createVector(mouseX,mouseY);
this.acceleration = p5.Vector.sub(this.magnet,this.location);
this.velocity.add(this.acceleration);
this.acceleration.setMag(0.2);
this.velocity.limit(this.topSpeed); // limits top speed
this.location.add(this.velocity); // location delta
} //end update
show(){
//print(this.location);
stroke(255);
strokeWeight(4);
fill(0,255,255);
ellipse(this.location.x,this.location.y,this.bsize,this.bsize);
} // end show
} // end of class ======================================== END CLASS
var level = "START";
var score = 0;
var health = 100;
var chasers = []; // array to hold all the orb objects...
function setup() {
createCanvas(windowWidth, windowHeight);
textAlign(CENTER);
textSize(32);
// for(var i=0; i<12; i++){
// chasers.push(new Orb()); // creates objects in array
// }
} // end of setup ======================================= END SETUP
function draw() {
background(100);
ellipse(mouseX, mouseY, 20, 20);
for(var i=0; i<chasers.length; i++){
chasers[i].update();
chasers[i].show();
var distToChaser = dist(mouseX,mouseY,chasers[i].location.x,chasers[i].location.y);
if(distToChaser < (chasers[i].bsize/2+10)){
background(random(255));
chasers.splice(i,1);
health = health - 5;
}
} // end of for
if(level == "START"){
startGame();
} else if(level == "LEVEL1"){
levelOne();
} else if(level == "GAMEOVER"){
gameOver();
}
fill(255);
noStroke();
text("♥ Health: " + health, 100,20);
text(" Score: " + score, 100,40);
if(health <= 0){
level = "GAMEOVER";
}
}// end of draw ======================================= END DRAW
function startGame(){
stroke(0,255,0);
textSize(32);
text("SURVIVAL GAME",width/2,height/4);
ellipse(width/2,height/2,60,60);
textSize(14);
fill(random(255));
text("START",width/2,height/2+6);
var distanceToStart = dist(width/2,height/2,mouseX,mouseY);
if(distanceToStart < 30){
level = "LEVEL1";
} // end IF
} // end of startGame ================================= END "startGame
function levelOne(){
if(frameCount%100 == 0){
chasers.push(new Orb()); // creates objects in array
score = score + 100;
}
fill(255);
text("LEVEL 1", width/2,height-40);
if(score > 1000){
// do whatever you dream up
}
} // end of level one ================================= END "level one"
function gameOver(){
fill(255,0,0);
text("GAME OVER",width/2,height/4);
} // end of game over ================================= END "game over"