Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.55 KB | None | 0 0
  1. import java.util.*; //We need it for the Iterator
  2.  
  3. PShader neon; //You can find the shader I'm using by searching "neon shader Processing" on Google!
  4. PImage moon;
  5.  
  6. Grid grid;
  7. Mountain mountain;
  8. FManager fm; //The launcher
  9.  
  10. void setup() {
  11. size(1920, 1080, P2D);
  12.  
  13. neon = loadShader("neon.glsl");
  14. moon = loadImage("moon.png");
  15.  
  16. grid = new Grid();
  17. mountain = new Mountain();
  18. fm = new FManager(new PVector(width * 0.5, height), FMM_BUGBURST_LARGE);
  19. }
  20.  
  21.  
  22. void draw() {
  23. background(0);
  24. grid.display();
  25. mountain.run();
  26. image(moon, width * 0.85, height * 0.03);
  27. fm.run();
  28. filter(neon);
  29.  
  30. if (frameCount < 1000) {
  31. //saveFrame("movie/pic####.tif");
  32. }
  33. }
  34.  
  35.  
  36.  
  37.  
  38. //_____________________________________________________
  39. //Const
  40. //Several modes, sets of ruls, for the launcher to follow
  41. //FMM stands for FManager Mode
  42.  
  43. final int FMM_SINGLE_STRAIGHT = 101;
  44. final int FMM_SINGLE_LARGE = 102;
  45. final int FMM_SMLBURST_STRAIGHT = 103;
  46. final int FMM_SMLBURST_LARGE = 104;
  47. final int FMM_BIGBURST_STRAIGHT = 105;
  48. final int FMM_BUGBURST_LARGE = 106;
  49.  
  50. //_____________________________________________________
  51. class FManager {
  52.  
  53. ArrayList<Firework> fireworkL;
  54.  
  55. PVector pos;
  56.  
  57. //How will the launcher behave?
  58. int freq; //The frequence at which we will launch new fireworks
  59. boolean burst; //Will we launch a single Firework, or several?
  60. int burstSeverity; //A little? A lot?
  61. PVector spread; //Where will the fireworks be launched? Always straig up, or with an angle? With how much vertical velocity?
  62. int spreadXmin;
  63. int spreadXmax;
  64. int spreadYmin;
  65. int spreadYmax;
  66.  
  67.  
  68.  
  69. FManager(PVector _pos, int _mode) {
  70. fireworkL = new ArrayList<Firework>();
  71.  
  72. pos = _pos.copy();
  73. this.setMode(_mode);
  74. }
  75.  
  76. void update() {
  77.  
  78.  
  79. if (burst) {
  80. if (frameCount%freq < burstSeverity) {
  81. if (frameCount%freq == 0) {
  82. pos.x = random(width);
  83. }
  84. this.launchFirework();
  85. }
  86. } else if (frameCount%freq == 0) {
  87. pos.x = random(width);
  88. this.launchFirework();
  89. }
  90.  
  91.  
  92. Iterator<Firework> it = fireworkL.iterator();
  93. while (it.hasNext()) {
  94. Firework f = it.next();
  95.  
  96. f.update();
  97.  
  98. if (f.isDone() && !f.children()) {
  99. f.createChildren();
  100. }
  101.  
  102. if (f.isFinished()) {
  103. it.remove();
  104. }
  105. }
  106. }
  107.  
  108. void display() {
  109. for (Firework f : fireworkL) {
  110. f.display();
  111. }
  112. }
  113.  
  114. void run() {
  115. this.update();
  116. this.display();
  117. }
  118.  
  119. void setRules(PVector _spread, int _freq, int _burstSeverity) {
  120. freq = _freq;
  121. burst = true;
  122. burstSeverity = _burstSeverity;
  123. spread = _spread.copy();
  124. }
  125. void setRules(PVector _spread, int _freq) {
  126. freq = _freq;
  127. burst = false;
  128. burstSeverity = 0;
  129. spread = _spread.copy();
  130. }
  131.  
  132. void setMode(int _mode) {
  133. int fMin = -60;
  134. int fMax = -60;
  135. int flMin = -10;
  136. int flMax = +10;
  137. int freqFix = 50;
  138. switch(_mode) {
  139. case FMM_SINGLE_STRAIGHT:
  140. freq = freqFix;
  141. burst = false;
  142. burstSeverity = 0;
  143. spreadXmin = -1;
  144. spreadXmax = 1;
  145. spreadYmin = fMin;
  146. spreadYmax = fMax;
  147. break;
  148.  
  149. case FMM_SINGLE_LARGE:
  150. freq = freqFix;
  151. burst = false;
  152. burstSeverity = 0;
  153. spreadXmin = flMin;
  154. spreadXmax = flMax;
  155. spreadYmin = fMin;
  156. spreadYmax = fMax;
  157. break;
  158.  
  159. case FMM_SMLBURST_STRAIGHT:
  160. freq = freqFix;
  161. burst = true;
  162. burstSeverity = 3;
  163. spreadXmin = -1;
  164. spreadXmax = 1;
  165. spreadYmin = fMin;
  166. spreadYmax = fMax;
  167. break;
  168.  
  169. case FMM_SMLBURST_LARGE:
  170. freq = freqFix;
  171. burst = true;
  172. burstSeverity = 3;
  173. spreadXmin = flMin;
  174. spreadXmax = flMax;
  175. spreadYmin = fMin;
  176. spreadYmax = fMax;
  177. break;
  178.  
  179. case FMM_BIGBURST_STRAIGHT:
  180. freq = freqFix;
  181. burst = true;
  182. burstSeverity = 10;
  183. spreadXmin = -1;
  184. spreadXmax = 1;
  185. spreadYmin = fMin;
  186. spreadYmax = fMax;
  187. break;
  188.  
  189. case FMM_BUGBURST_LARGE:
  190. freq = freqFix;
  191. burst = true;
  192. burstSeverity = 10;
  193. spreadXmin = flMin;
  194. spreadXmax = flMax;
  195. spreadYmin = fMin;
  196. spreadYmax = fMax;
  197. break;
  198. }
  199. }
  200.  
  201. void launchFirework() {
  202. //Firework(PVector _pos, PVector _vel, int _precision, int _radius, color _c, int _intensity, float _decaySpeed)
  203.  
  204. int precisionF = floor(random(8, 11));
  205. float radiusF = random(15, 30);
  206. color cF = retroColor();
  207. int intensityF = floor(random(5, 10));
  208. spread = new PVector(random(spreadXmin, spreadXmax), random(spreadYmin, spreadYmax));
  209.  
  210. fireworkL.add(new Firework(pos, spread, precisionF, radiusF, cF, intensityF));
  211. }
  212. }
  213.  
  214.  
  215. //_____________________________________________________
  216. //Const
  217. //Gravity
  218. final PVector GRAVITY = new PVector(0, 1.2);
  219. //_____________________________________________________
  220. class Firework {
  221.  
  222. //Children
  223. ArrayList<Firework> fireworkL;
  224.  
  225. //Physics (See "The Nature of Code", Daniel Shiffman, Force Chapter, can legaly be found for free on internet)
  226. // http://natureofcode.com/book/chapter-2-forces/
  227. //And the chapter 4 for the particle generator (In this sketch, Firework is a particle, while being a Particle
  228. // http://natureofcode.com/book/chapter-4-particle-systems/
  229. PVector pos;
  230. PVector vel;
  231. PVector acc = new PVector();
  232. float friction = 0.98;
  233.  
  234. //Triangle? Square? Hexagon?
  235. int precision;
  236.  
  237. color c;
  238. float radius;
  239.  
  240. //Determines if we take care of it, or the entities a floor lower, his Fireworks in the ArrayList
  241. boolean hasChildren;
  242.  
  243. //Was it the first firework entity to be launched into the sky?
  244. //TRUE: Trigger him on a drop on the y axis (y incrementing)
  245. //FALSE; Trigger it with a set amount of frames
  246. boolean original;
  247.  
  248. //TRUE
  249. int trigger_dropSize = 20;
  250. float highestPoint = height; //The highest point he reached, useful to calculate the drop height.
  251.  
  252. //FALSE
  253. float lifeSpan = 255; //The firework's remaining black powder
  254. float decaySpeed; //The speed at which the tank is being emptied
  255.  
  256. //A last variable, to make sure the Firework won't grow in a exponential manner.
  257. int intensity;
  258.  
  259. boolean finished = false;
  260.  
  261. Firework(PVector _pos, PVector _vel, int _precision, float _radius, color _c, int _intensity) {
  262. pos = _pos.copy();
  263. vel = _vel.copy();
  264. precision = _precision;
  265. radius = _radius;
  266. c = _c;
  267. intensity = _intensity;
  268. original = true;
  269.  
  270. fireworkL = new ArrayList<Firework>();
  271. }
  272.  
  273. Firework(PVector _pos, PVector _vel, int _precision, float _radius, color _c, int _intensity, float _decaySpeed) {
  274. pos = _pos.copy();
  275. vel = _vel.copy();
  276. precision = _precision;
  277. radius = _radius;
  278. c = _c;
  279. intensity = _intensity;
  280. decaySpeed = _decaySpeed;
  281. original = false;
  282.  
  283. fireworkL = new ArrayList<Firework>();
  284. }
  285.  
  286.  
  287. //In update, we will take care of the data, physics and such
  288. void update() {
  289. if (!hasChildren) {
  290. this.applyForce(GRAVITY);
  291. vel.add(acc);
  292. vel.mult(friction);
  293. pos.add(vel);
  294. acc.mult(0);
  295. if (highestPoint > pos.y) {
  296. highestPoint = pos.y;
  297. }
  298. } else {
  299. Iterator<Firework> it = fireworkL.iterator();
  300. while (it.hasNext()) {
  301. Firework f = it.next();
  302.  
  303. f.update();
  304.  
  305. if (f.isDone() && !f.children()) {
  306. f.createChildren();
  307. }
  308. }
  309. }
  310. }
  311.  
  312. void display() {
  313. if (!hasChildren) {
  314. pushMatrix();
  315. translate(pos.x, pos.y);
  316. noFill();
  317. stroke(c, lifeSpan);
  318. beginShape(LINES);
  319.  
  320. for (int i = 0; i < precision; i++) {
  321. float x = cos((TWO_PI/precision)*i)*radius;
  322. float y = sin((TWO_PI/precision)*i)*radius;
  323. float xp = cos((TWO_PI/precision)*(i+1))*radius;
  324. float yp = sin((TWO_PI/precision)*(i+1))*radius;
  325. vertex(x, y);
  326. vertex(xp, yp);
  327. }
  328.  
  329. endShape();
  330. popMatrix();
  331. } else {
  332. for (Firework f : fireworkL) {
  333. f.display();
  334. }
  335. }
  336. }
  337.  
  338. //The condition depends on the boolean "original", see the variables for more information
  339. boolean isDone() {
  340. if (original) {
  341. if (pos.y - trigger_dropSize > highestPoint) {
  342. return true;
  343. } else {
  344. return false;
  345. }
  346. } else {
  347. lifeSpan -= decaySpeed;
  348. if (lifeSpan < 0) {
  349. return true;
  350. } else {
  351. return false;
  352. }
  353. }
  354. }
  355.  
  356. boolean children() {
  357. return hasChildren;
  358. }
  359.  
  360. void createChildren() {
  361.  
  362. int intensityNew = intensity - 1;
  363.  
  364. if (intensity > 0) {
  365.  
  366. int children;
  367.  
  368. if (!original) {
  369. children = floor(random(intensity));
  370. } else {
  371. children = intensity;
  372. }
  373.  
  374. float radiusNew = radius - random(5);
  375. radiusNew = (radiusNew < 1) ? 1 : radiusNew; //We're making sure the radius isn't going to go under 1, so that we can still see the next fireworks
  376.  
  377. float velocity = radiusNew * 0.5;
  378.  
  379. int precisionNew = precision - 1;
  380. precisionNew = (precisionNew < 2) ? 2 : precisionNew;
  381.  
  382. color cNew = retroColor();
  383.  
  384.  
  385.  
  386. float decaySpeedNew;
  387.  
  388. if (!original) {
  389. decaySpeedNew = decaySpeed + random(10);
  390. } else {
  391. decaySpeedNew = 30;
  392. }
  393.  
  394. float angleOffset = random(TWO_PI);
  395.  
  396. //Firework(PVector _pos, PVector _vel, int _precision, int _radius, color _c, int _intensity, float _decaySpeed)
  397. for (int i = 0; i < children; i++) {
  398. float velX = cos((TWO_PI/children)*i + angleOffset)*velocity;
  399. float velY = sin((TWO_PI/children)*i + angleOffset)*velocity;
  400. fireworkL.add(new Firework(pos, new PVector(velX, velY), precisionNew, radiusNew, cNew, intensityNew, decaySpeedNew));
  401. }
  402.  
  403. hasChildren = true;
  404. } else {
  405. finished = true;
  406. }
  407. }
  408.  
  409. void applyForce(PVector _force) {
  410. acc.add(_force);
  411. }
  412.  
  413. boolean isFinished() {
  414. if (!hasChildren) {
  415. return finished;
  416. } else {
  417. int compt = 0;
  418. for (Firework f : fireworkL) {
  419. if (f.isFinished()) {
  420. compt++;
  421. }
  422. }
  423. if (compt == fireworkL.size()) {
  424. return true;
  425. } else {
  426. return false;
  427. }
  428. }
  429. }
  430. }
  431.  
  432.  
  433.  
  434.  
  435. color retroColor() {
  436. return color(random(255), random(255), random(255));
  437. /*if (random(1) < 0.5) {
  438. return color(255, 0, 255);
  439. } else {
  440. return color(0, 255, 255);
  441. }*/
  442. }
  443.  
  444.  
  445. class Grid {
  446.  
  447. float tileSize = 100;
  448.  
  449. Grid() {
  450. }
  451.  
  452. void display() {
  453. beginShape(LINES);
  454. stroke(255, 0, 255, 200);
  455. float sizeX = width;
  456. float sizeY = height;
  457. for (float x = 0; x <= sizeX; x += tileSize) {
  458. vertex(x, 0);
  459. vertex(x, sizeY);
  460. }
  461. for (float y = 0; y <= sizeY; y += tileSize) {
  462. vertex(0, y);
  463. vertex(sizeX, y);
  464. }
  465. endShape();
  466. }
  467. }
  468.  
  469.  
  470.  
  471. class Mountain {
  472.  
  473. float amp = 350;
  474. float yAverage = height*0.6;
  475. int nbPoint = 8;
  476. float n = random(255);
  477. float nSpeed = 0.01;
  478. float ex = 0.03;
  479.  
  480. Mountain() {
  481. }
  482.  
  483. void update() {
  484. n += nSpeed * (sin(frameCount*0.1) + 1);
  485. }
  486.  
  487. void display() {
  488. fill(0);
  489. stroke(255, 0, 255, 200);
  490. beginShape();
  491. vertex(width, 0);
  492. vertex(0, 0);
  493. for (float x = 0; x <= width; x += width/nbPoint) {
  494. float y = yAverage + map(noise(n, x*ex), 0, 1, -amp/2, amp/2);
  495. vertex(x, y);
  496. }
  497. endShape(CLOSE);
  498. }
  499.  
  500. void run() {
  501. this.update();
  502. this.display();
  503. }
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement