Last active
April 18, 2017 16:28
-
-
Save alexanderhenne/8089d402e005684378596e3ca428af4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
long startTime; | |
void setup() { | |
size(800, 600); | |
colorMode(HSB, 100); | |
startTime = System.currentTimeMillis(); | |
} | |
final int INSTRUCTION_DURATION = 3000; //ms | |
final float GRAVITY_ACCELERATION = 9.82; | |
final int SPEED_MULTIPLIER = 6; | |
final int TRAIL_PART_DURATION = 500; //ms | |
final int EXPLOSION_DURATION = 2000; //ms | |
final int EXPLOSION_MAX_RADIUS = 350; //pixlar | |
final int EXPLOSION_FLARE_COUNT = 16; | |
List<Firework> fireworks = new ArrayList<Firework>(); | |
void draw() { | |
background(#212121); | |
noStroke(); | |
long currentTime = System.currentTimeMillis(); | |
Iterator<Firework> fireworkIterator = fireworks.iterator(); | |
while (fireworkIterator.hasNext()) { | |
Firework firework = fireworkIterator.next(); | |
if (firework.explosionSpawnTime > 0) { | |
float t = (currentTime - firework.explosionSpawnTime) / (float) EXPLOSION_DURATION; | |
if (t >= 1) { | |
fireworkIterator.remove(); | |
} else { | |
int newX = round(t * firework.maxExplosionRadius); | |
long xDifference = newX - firework.lastExplosionRadius; | |
if (xDifference > 0) { | |
for (int i = 0; i < xDifference; i++) { | |
firework.flareParts.add(new FireworkComponent(firework.lastExplosionRadius + i, 0)); | |
} | |
firework.lastExplosionRadius = newX; | |
} | |
pushMatrix(); | |
translate(firework.endX, firework.endY); | |
for (int i = 0; i < EXPLOSION_FLARE_COUNT; i++) { | |
rotate(2 * PI / EXPLOSION_FLARE_COUNT); | |
for (FireworkComponent flarePart : firework.flareParts) { | |
float elapsedFraction = (currentTime - flarePart.spawnTime) / (float) (EXPLOSION_DURATION / 3); | |
int opacity = (int) ((255 - (elapsedFraction * 255)) * (1 - t)); | |
fill(firework.explosionColor, opacity); | |
ellipse(flarePart.x, flarePart.y, 1, 1); | |
// Rita eldpartiklar | |
randomSeed(flarePart.hashCode()); | |
fill(4.694, 85 + random(15), 50 + random(50), opacity); | |
int flameParticles = round(random(1, 2)); | |
for (int j = 0; j < flameParticles; j++) { | |
ellipse(flarePart.x + random(-1, 1), flarePart.y + random(-1, 1), 1, 1); | |
} | |
} | |
} | |
popMatrix(); | |
} | |
} else { | |
float t = (currentTime - firework.spawnTime) / 1000F; | |
float v = sqrt((-(2 * GRAVITY_ACCELERATION) * (height - firework.endY))/-1); | |
int newY = round(-GRAVITY_ACCELERATION/2*sq(SPEED_MULTIPLIER)*sq(t) | |
+ v*SPEED_MULTIPLIER*t); | |
int yDifference = newY - firework.coreY; | |
if (yDifference >= 0) { | |
for (int i = 0; i < abs(yDifference); i++) { | |
int y = firework.coreY; | |
if (yDifference > 0) { | |
y += i; | |
} else { | |
y -= i; | |
} | |
firework.trailParts.add(new FireworkComponent(firework.coreX, y)); | |
} | |
firework.coreY = newY; | |
} else { | |
firework.explosionSpawnTime = System.currentTimeMillis(); | |
} | |
} | |
Iterator<FireworkComponent> trailPartIterator = firework.trailParts.iterator(); | |
while (trailPartIterator.hasNext()) { | |
FireworkComponent trailPart = trailPartIterator.next(); | |
float elapsedFraction = (currentTime - trailPart.spawnTime) / (float) TRAIL_PART_DURATION; | |
if (elapsedFraction >= 1) { | |
trailPartIterator.remove(); | |
} else { | |
int opacity = (int) (255 - (elapsedFraction * 255)); | |
fill(#ffffff, opacity); | |
ellipse(trailPart.x, height - trailPart.y, 1, 1); | |
// Rita eldpartiklar | |
randomSeed(trailPart.hashCode()); | |
fill(4.694, 85 + random(15), 50 + random(50), opacity); | |
int flameParticles = round(random(2, 6)); | |
for (int i = 0; i < flameParticles; i++) { | |
ellipse(trailPart.x + random(-2, 2), height - trailPart.y + random(10), 1, 1); | |
} | |
} | |
} | |
} | |
long elapsedTimeSinceStart = currentTime - startTime; | |
if (elapsedTimeSinceStart < INSTRUCTION_DURATION) { | |
float elapsedFraction = elapsedTimeSinceStart / (float) INSTRUCTION_DURATION; | |
int opacity = (int) (255 - (elapsedFraction * 255)); | |
fill(#ffffff, opacity); | |
textAlign(CENTER); | |
textSize(24); | |
text("Tryck på skärmen för att avfyra fyrverkerier", width / 2, 75); | |
} | |
} | |
void mousePressed() { | |
color baseColor = color(random(100), 100, 85); | |
fireworks.add(new Firework(mouseX, mouseY, | |
baseColor, | |
(int) random(EXPLOSION_MAX_RADIUS * 0.75, EXPLOSION_MAX_RADIUS))); | |
} | |
class Firework { | |
long spawnTime; | |
int endX; | |
int endY; | |
int coreX; | |
int coreY; | |
List<FireworkComponent> trailParts = new ArrayList<FireworkComponent>(); | |
List<FireworkComponent> flareParts = new ArrayList<FireworkComponent>(); | |
color explosionColor; | |
long explosionSpawnTime; | |
int lastExplosionRadius; | |
int maxExplosionRadius; | |
public Firework(int endX, int endY, color explosionColor, int maxExplosionRadius) { | |
this.spawnTime = System.currentTimeMillis(); | |
this.endX = endX; | |
this.endY = endY; | |
this.coreX = endX; | |
this.explosionColor = explosionColor; | |
this.maxExplosionRadius = maxExplosionRadius; | |
} | |
} | |
class FireworkComponent { | |
long spawnTime; | |
int x; | |
int y; | |
public FireworkComponent(int x, int y) { | |
this.spawnTime = System.currentTimeMillis(); | |
this.x = x; | |
this.y = y; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment