xxxxxxxxxx
var Blobs = [];
var Droplets = [];
var SelectedBlobs = [];
var sizes = [20, 20, 20, 20, 30, 30, 40];
var startDragX;
var startDragY;
var endDragX;
var endDragY;
var w = 1400;
var h = 800;
var sti;
function preload() {
sti = loadImage('stars.jpg')
}
function setup() {
createCanvas(w, h+100);
generateMap(random(20, 30));
}
function draw() {
background(50);
image(sti, 0, 0, width, height);
if (frameCount % 2 == 0) runEnemy(Blobs);
for (var b of Blobs) {
b.display();
b.update();
}
for (var d in Droplets) {
Droplets[d].display();
Droplets[d].update(d);
}
if (SelectedBlobs.length !== 0) {
for (var SelectedBlob of SelectedBlobs) {
push();
stroke(255, 0, 0);
strokeWeight(3);
line(SelectedBlob.x, SelectedBlob.y, mouseX, mouseY);
}
pop();
}
//draw box thing
if(mouseIsPressed) {
push();
strokeWeight(4);
stroke(100);
fill(150, 60);
rect(min(startDragX, mouseX), min(startDragY, mouseY), max(startDragX, mouseX)- min(startDragX, mouseX), max(startDragY, mouseY) - min(startDragY, mouseY));
pop();
}
push();
noStroke();
fill(50, 100);
rect(0, h, w, 100);
drawBar();
pop();
}
function t(a, b) {
if (a > 0) {
return abs(b);
}
return abs(b) * -1;
}
function f(a) {
if (a > 0) {
return floor(a);
}
return ceil(a);
}
function mouseReleased() {
endDragX = mouseX;
endDragY = mouseY;
for (var b of Blobs) {
if (b.x > min(startDragX, endDragX) && b.x < max(startDragX, endDragX) && b.y > min(startDragY, endDragY) && b.y < max(startDragY, endDragY) && b.value > 0) {
SelectedBlobs.push(b);
//f = true;
}
}
}
function mousePressed() {
startDragX = mouseX;
startDragY = mouseY;
if (SelectedBlobs.length == 0) {
var f = false;
for (var b of Blobs) {
if (dist(mouseX, mouseY, b.x, b.y) < (30 + b.maxValue) / 2&& b.value > 0) {
SelectedBlobs.push(b);
f = true;
}
}
} else {
var f = false;
for (var b of Blobs) {
if (dist(mouseX, mouseY, b.x, b.y) < (30 + b.maxValue) / 2 && !SelectedBlobs.includes(b) ) {
for (var SelectedBlob of SelectedBlobs) {
if (abs(SelectedBlob.value) >= 2) {
SelectedBlob.value -= t(SelectedBlob.value, 1);
Droplets.push(new Droplet(SelectedBlob.x, SelectedBlob.y, t(SelectedBlob.value, 1), b));
}
f = true;
}
}
}
if (!f) {
SelectedBlobs = [];
}
}
}
function drawBar() {
var rtotal = 0;
var btotal = 0;
for(var b of Blobs) {
if(b.value > 0) {
btotal += b.value;
} else if(b.value< 0) {
rtotal += abs(b.value);
}
}
btotal = floor(btotal);
rtotal = floor(rtotal);
var place = btotal / rtotal * width/2 ;
textSize(40);
fill(255, 0, 0);
text(floor(rtotal), max(width*0.75, place), height-50);
rect(place, height-50, width-place, 50);
fill(0, 0, 255);
text(floor(btotal), min(width*0.25, place), height-50);
rect(0, height-50, place, 50);
}
function mouseDragged(event) {
}
function generateMap(num) {
Blobs = [];
for (var i = 0; i < num; i++) {
for (var j = 0; j < 20; j++) {
var nx = random(50, w - 50)
var ny = random(50, h - 50);
var posOK = true;
for (var nb of Blobs) {
if (dist(nx, ny, nb.x, nb.y) < 60) {
//add spacing between
posOK = false;
}
}
if (posOK) {
Blobs.push(new Blob(nx, ny, 0, sizes[floor(random(sizes.length))]));
j = 20;
}
}
}
Blobs.push(new Blob(w / 2, 100, -10, 20));
Blobs.push(new Blob(w / 2, h - 100, 10, 20));
}
function generatePoints(size, variation, pointsNum) {
var ret = [];
for(var i = 0; i < pointsNum; i++) {
ret.push(random(size * (1 - variation), size * (1 + variation)));
}
return ret;
}
function displayPoints(pointsList, offX, offY, value) {
push();
translate(offX, offY);
if(value == 0) {
fill(150);
} else if(value >0) {
fill(0, 0, 255);
}
else if(value < 0) {
fill(255, 0, 0);
}
stroke(0);
strokeWeight(4);
beginShape();
for(var i = 0; i < pointsList.length; i++) {
var theta = TWO_PI * i / pointsList.length;
var vec = p5.Vector.fromAngle(theta).mult(pointsList[i]);
vertex(vec.x, vec.y);
}
endShape(CLOSE);
pop();
}