Skip to content

Instantly share code, notes, and snippets.

@alexanderhenne
Last active April 1, 2017 12:21
Show Gist options
  • Save alexanderhenne/c86b1117dd6d159b7ef48a2330bed36a to your computer and use it in GitHub Desktop.
Save alexanderhenne/c86b1117dd6d159b7ef48a2330bed36a to your computer and use it in GitHub Desktop.
import java.util.*;
int INSTRUCTION_TEXT_DURATION = 3500; //ms
int MAX_SPHERE_COUNT = 20;
int MIN_INITIAL_VELOCITY = 2;
int MAX_INITIAL_VELOCITY = 6;
int VELOCITY_REDUCTION_INTERVAL = 500; //ms
float VELOCITY_REDUCTION_FACTOR = 0.8;
int EXPLOSION_MAX_SIZE = 500;
List<Sphere> spheres = new ArrayList<Sphere>();
long explosionSpawnTime = -1;
long startTime = System.currentTimeMillis();
long lastVelocityReductionTime;
void setup() {
size(800, 600);
colorMode(HSB, 360, 100, 100);
for (int i = 0; i < MAX_SPHERE_COUNT; i++) {
Sphere sphere = new Sphere((int) random(width),
(int) random(height),
random(MIN_INITIAL_VELOCITY, MAX_INITIAL_VELOCITY),
random(MIN_INITIAL_VELOCITY, MAX_INITIAL_VELOCITY),
color(random(360), 50, 50));
if ((int) random(2) < 1) {
sphere.xVelocity *= -1;
sphere.yVelocity *= -1;
}
spheres.add(sphere);
}
}
void draw() {
background(#141414);
long currentTime = System.currentTimeMillis();
long elapsedTimeSinceStart = currentTime - startTime;
if (elapsedTimeSinceStart < INSTRUCTION_TEXT_DURATION) {
float progressFraction = (float) elapsedTimeSinceStart / INSTRUCTION_TEXT_DURATION;
fill(255, (1 - progressFraction) * 255);
textSize(24);
textAlign(CENTER);
text("Håll in musen för att skapa en explosion", width / 2, 75);
}
boolean reduceVelocity = false;
if (currentTime - lastVelocityReductionTime >= VELOCITY_REDUCTION_INTERVAL) {
reduceVelocity = true;
}
noStroke();
for (Sphere sphere : spheres) {
sphere.x += sphere.xVelocity;
sphere.y += sphere.yVelocity;
if (sphere.x <= 0) {
sphere.xVelocity *= -1;
sphere.x = 0;
} else if (sphere.x > width) {
sphere.xVelocity *= -1;
sphere.x = width;
}
if (sphere.y <= 0) {
sphere.yVelocity *= -1;
sphere.y = 0;
} else if (sphere.y > height) {
sphere.yVelocity *= -1;
sphere.y = height;
}
if (reduceVelocity) {
sphere.xVelocity *= VELOCITY_REDUCTION_FACTOR;
sphere.yVelocity *= VELOCITY_REDUCTION_FACTOR;
}
// Sfärerna ska ha minst 1 i hastighet,
// så att de alltid rör på sig
if (sphere.xVelocity > 0 && sphere.xVelocity < 1) {
sphere.xVelocity = 1;
}
if (sphere.yVelocity > 0 && sphere.yVelocity < 1) {
sphere.yVelocity = 1;
}
}
if (reduceVelocity) {
lastVelocityReductionTime = currentTime;
}
for (Sphere sphere : spheres) {
stroke(sphere.fillColor);
fill(sphere.fillColor, 50);
ellipse(sphere.x, sphere.y, 40, 40);
List<Sphere> nearbySpheres = getNearbySpheres(sphere);
for (Sphere sphere2 : nearbySpheres) {
explodeSphere(sphere2, sphere.x, sphere.y, 0.5);
}
}
if (explosionSpawnTime != -1) {
long elapsedTimeSinceSpawn = currentTime - explosionSpawnTime;
int size = min((int) pow(elapsedTimeSinceSpawn / 1000F * 15, 2), EXPLOSION_MAX_SIZE);
stroke(#d66e13);
fill(#d66e13, 50);
ellipse(mouseX, mouseY, size, size);
}
}
List<Sphere> getNearbySpheres(Sphere sphere) {
List<Sphere> nearbySpheres = new ArrayList<Sphere>();
int x = sphere.x;
int y = sphere.y;
for (Sphere sphere2 : spheres) {
float distance = dist(x, y, sphere2.x, sphere2.y);
if (distance > 10 && distance < 40) {
nearbySpheres.add(sphere2);
}
}
return nearbySpheres;
}
void mousePressed() {
explosionSpawnTime = System.currentTimeMillis();
}
void mouseReleased() {
long elapsedTimeSinceSpawn = System.currentTimeMillis() - explosionSpawnTime;
int size = min((int) pow(elapsedTimeSinceSpawn / 1000F * 15, 2), EXPLOSION_MAX_SIZE);
for (Sphere sphere : spheres) {
explodeSphere(sphere, mouseX, mouseY, size * 0.2);
}
explosionSpawnTime = -1;
}
void explodeSphere(Sphere sphere, int x, int y, float power) {
int sX = sphere.x;
int sY = sphere.y;
if (sphere.xVelocity < 0){
sphere.xVelocity -= power;
if (sX >= x) {
sphere.xVelocity *= -1;
}
} else {
sphere.xVelocity += power;
if (sX <= x) {
sphere.xVelocity *= -1;
}
}
if (sphere.yVelocity < 0){
sphere.yVelocity -= power;
if (sY >= y) {
sphere.yVelocity *= -1;
}
} else {
sphere.yVelocity += power;
if (sY <= y) {
sphere.yVelocity *= -1;
}
}
}
class Sphere {
int x;
int y;
float xVelocity;
float yVelocity;
color fillColor;
public Sphere(int x, int y,
float xVelocity, float yVelocity,
color fillColor) {
this.x = x;
this.y = y;
this.xVelocity = xVelocity;
this.yVelocity = yVelocity;
this.fillColor = fillColor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment