/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.u21rTj5JHby/rev.2651
*
* authors:
*
* Austin Gleim
* Kalen Mullin
*
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
// Pressing Control-R will render this sketch.
int xspace = 500;
int yspace = 300;
int xLimitLo = 0;
int xlimitHi = 800;
int yLimitLo = 0;
int ylimitHi = 400;
static final int NUM = 100;
final int[] xBadGuy = new int[NUM], yBadGuy = new int[NUM];
final int[] xBadGuyDelta = new int[NUM], yBadGuyDelta = new int[NUM];
void setup() { // this is run once.
// set the background color
background(255);
// canvas size (Variable aren't evaluated. Integers only, please.)
size(800, 400);
// smooth edges
smooth();
// limit the number of frames per second
frameRate(30);
// set the width of the line.
strokeWeight(12);
noFill();
// create NUM badguys randomly located on screen
for (int i = 0; i < NUM ; i = i+1) {
xBadGuy[i] = random(width);
yBadGuy[i] = random(height);
// set the current delta between player and bad guy
xBadGuyDelta[i] = (width / 2) - xBadGuy[i];
yBadGuyDelta[i] = (height/ 2) - yBadGuy[i];
}
}
// for every bad guy maintain their position with respect to the player
void trackBadGuys() {
for (int i = 0; i < NUM ; i = i+1) {
// how far are they from the player right now ?
int currentDeltaX = xspace - xBadGuy[i];
int currentDeltaY = yspace - yBadGuy[i];
// if the player has moved right, do so as well
if(currentDeltaX > xBadGuyDelta[i]) {
xBadGuy[i] = xBadGuy[i] + 1;
}
// otherwise if the player has moved left, do so as well
else if(currentDeltaX < xBadGuyDelta[i]) {
xBadGuy[i] = xBadGuy[i] - 1;
}
// if the player has moved down, do so as well
if(currentDeltaY > yBadGuyDelta[i]) {
yBadGuy[i] = yBadGuy[i] + 1;
}
// otherwise if the player has moved up, do so as well
else if(currentDeltaY < yBadGuyDelta[i]) {
yBadGuy[i] = yBadGuy[i] - 1;
}
// wrap em, wrap em good
yBadGuy[i]= wrapY(yBadGuy[i]);
xBadGuy[i]= wrapX(xBadGuy[i]);
}
}
// maybe bad guys get closer to the player
void approachPlayer() {
// for each bad guy
for (int i = 0; i < NUM ; i = i+1) {
// if fate has decided they can get closer on the x axis
if(random(5) < 3) {
// random perturbation (they're all drunk)
int staggerx = random(6) - 2;
// make the tracking delta (hopefully) closer to the player
if(xBadGuyDelta[i] < 0) {
xBadGuyDelta[i] = xBadGuyDelta[i] + staggerx ;
}
else if(xBadGuyDelta[i] > 0) {
xBadGuyDelta[i] = xBadGuyDelta[i] - staggerx;
}
}
// if fate has decided they can get closer on the x axis
if(random(5) < 3) {
int staggery = random(6) - 2;
if(yBadGuyDelta[i] < 0) {
yBadGuyDelta[i] = yBadGuyDelta[i] + staggery ;
}
else if(yBadGuyDelta[i] > 0) {
yBadGuyDelta[i] = yBadGuyDelta[i] - staggery ;
}
}
}
if(yBadGuy[i] == yspace || xBadGuy[i] == xspace){
delete(BadGuys[i]);
}
}
void wrapX(x) {
if(x< 0){
x= width - 1;
}
if(x> 1000){
x= 1;
}
return x;
}
void wrapY(y) {
if(y< 0){
y = height - 1;
}
if(y> 400){
y= 1;
}
return y;
}
void draw() { // this is run repeatedly.
// set the color
background(255);
stroke(random(50), random(255), random(255), 100);
ellipse(xspace, yspace, 1, 1);
// draw bad guys
stroke(255,0,0, 100);
for (int i = 0; i < NUM ; i = i+1) {
ellipse(xBadGuy[i], yBadGuy[i], 1, 1);
}
if (keyPressed){
if(key == 'w' || key == 'W' ){
yspace -= 2;
}
if(key == 's' || key == 'S'){
yspace += 2;
}
if(key == 'a' || key == 'A'){
xspace -= 2;
}
if(key == 'd' || key == 'D'){
xspace += 2;
}
}
yspace = wrapY(yspace);
xspace = wrapX(xspace);
trackBadGuys();
approachPlayer();
// if(BadGuys <= 5){
// end
// }
}