> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.BSKjx3U7TaF/rev.13
 * 
 * authors: 
 *   GoToLoop

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/**
 Spit Fire Sample (v2.71)
 by  asimes (2013/Apr)
 mod GoToLoop
 for mnredr
 
 http://forum.processing.org/topic/how-can-i-make-a-ball-always-move-towards-the-player-character
 
 http://studio.processingtogether.com/sp/pad/export/ro.91nVQMnL$v06L/latest
 */

final static String GFX = P2D; // Use JAVA2D for Processing 2+

final static short E_SPD = 25, P_SPD = 5, B_SPD = 5, B_FREQ = 40;
final static short E_SIZE = 50, P_SIZE = 50, B_SIZE = 10;
final static float FPS = 50, BOLD = 2;

final static color E_COL = #00FF00, P_COL = #0000FF, B_COL = #FF0000;
final static color BG = 0100, FG = 0300;

final static byte MAX_BULLETS = 3;
final static PVector[] bullets = new PVector[MAX_BULLETS];
final static PVector[] targets = new PVector[MAX_BULLETS];

final static PVector enemy  = new PVector();
final static PVector player = new PVector();

int centerX, centerY, diam, rad, bulletsIdx;
float theta;

void setup() {
  size(700, 700, GFX);
  frameRate(FPS);
  smooth();
  strokeWeight(BOLD);
  ellipseMode(CENTER);
  imageMode(CENTER);

  centerX = width  >> 1;
  centerY = height >> 1;
  diam = width - 100;
  rad  = diam >> 1;

  enemy.set(rad, 0, 0);

  for (int i = 0; i != MAX_BULLETS; i++) {
    bullets[i] = new PVector(1e5, 1e5);
    targets[i] = new PVector();
  }
}

void draw() {
  background(BG);

  // Move the enemy in a circle:
  //enemy.rotate(-HALF_PI/E_SPD);
  enemy.set(cos(theta -= HALF_PI/E_SPD)*rad, sin(theta)*rad, 0);

  // Create bullets:
  if (frameCount % B_FREQ == 0)    spitFire();

  // Display all objects:
  show();
}

void spitFire() {
  // Make the starting position of the bullets be where the enemy is:
  bullets[bulletsIdx = (bulletsIdx + 1) % MAX_BULLETS].set(enemy);

  // Aim at wherever the player currently is:
  final PVector t = targets[bulletsIdx];
  //PVector.sub(player, enemy, t);
  t.set( PVector.sub(player, enemy) );

  // Normalize the direction vector:
  t.normalize();

  // Multiply by whatever speed you want bullets to move:
  t.mult(B_SPD);
}

void show() {
  translate(centerX, centerY);              // centralizes

  fill(FG);
  ellipse(0, 0, diam, diam);

  fill(E_COL);
  ellipse(enemy.x, enemy.y, E_SIZE, E_SIZE);

  fill(P_COL);
  player.add(random(-P_SPD, P_SPD), random(-P_SPD, P_SPD), 0);
  ellipse(player.x, player.y, P_SIZE, P_SIZE);

  // Update & display bullets:
  showBullets();
}

void showBullets() {
  fill(B_COL);

  for (int i = 0; i != MAX_BULLETS;) {
    PVector b = bullets[i];
    b.add(targets[i++]);                    // updates
    ellipse(b.x, b.y, B_SIZE, B_SIZE);      // displays
  }
}