Advertisement
joemckay

snow_at_night

Dec 6th, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. ArrayList<FlakeClass> Flakeslist = new ArrayList<FlakeClass>();
  2. ArrayList<GustClass> gustslist = new ArrayList<GustClass>();
  3. ArrayList<LineClass> lineslist = new ArrayList<LineClass>();
  4.  
  5. color b1, b2, c1, c2;
  6. PGraphics pg;
  7. float lightY = 170;
  8. boolean lightYDir;
  9. float lightYSpeed = .25;
  10. float movingForwardSpeed = .2;
  11. void setup() {
  12. size(800, 600);
  13. //fullScreen();
  14. Flakeslist = new ArrayList();
  15. gustslist = new ArrayList();
  16. lineslist = new ArrayList();
  17. newgust();
  18. newFlake(2000);
  19. pg = createGraphics(width, 260);
  20. c1 = color( 0);
  21. c2 = color(180);
  22. setGradient(0, 0, width, 180, c1, c2);
  23. noStroke();
  24. newline();
  25. }
  26. void newline() {
  27. lineslist.add(new LineClass(height - 160));
  28. lineslist.add(new LineClass(height - 60));
  29.  
  30. //lineslist.add(new LineClass(height - 158));
  31. //lineslist.add(new LineClass(height - 92));
  32. //lineslist.add(new LineClass(height - 26));
  33. }
  34.  
  35. void newgust() {
  36. for (int i = 0; i < 4; i ++) {
  37. float gH = random (height);
  38. gustslist.add(new GustClass());
  39. }
  40. }
  41.  
  42. void newFlake( int flakeNum) {
  43. for (int i = 0; i < flakeNum; i ++) {
  44. float s = random(2, 4);
  45. Flakeslist.add(new FlakeClass(random(width), random(-120, height - 120), s));
  46. }
  47. }
  48.  
  49. void mouseReleased() {
  50. newFlake(20);
  51. }
  52.  
  53. void draw() {
  54. background(1);
  55. if (lightYDir) { // gives the light a little moevement on the ground
  56. lightY += lightYSpeed;
  57. } else {
  58. lightY -= lightYSpeed;
  59. }
  60. if ( lightY >=176) {// lightY <= 156){
  61. lightYDir = !lightYDir;
  62. lightY = 176;
  63. lightYSpeed = random(.1, .3);
  64. }
  65. if ( lightY <= 156) {// lightY <= 156){
  66. lightYDir = !lightYDir;
  67. lightY = 156;
  68. lightYSpeed = random(.1, .3);
  69. }
  70.  
  71. image(pg, 0, height - lightY); // put the light on the ground
  72. for (int i = Flakeslist.size()-1; i >= 0; i--) {
  73. FlakeClass dFlake = Flakeslist.get(i);
  74. dFlake.display(); // show every flake
  75. dFlake.fall(); // move every flake
  76. }
  77. for (int i = gustslist.size()-1; i >= 0; i--) {
  78. GustClass dgust = gustslist.get(i);
  79. // dgust.display(); //uncomment to "see" the gusts of wind
  80. dgust.moveGust(); // move the gusts of wind.
  81. }
  82.  
  83. for (int i = lineslist.size()-1; i >= 0; i--) {
  84. LineClass dline = lineslist.get(i);
  85. dline.display();
  86. }
  87. }
  88.  
  89. // make the light gradient PGraphic. This way we only create the gradient once.
  90. //Less work for computer means more snowflakes!
  91. void setGradient(int x, int y, float w, float h, color c1, color c2) {
  92. pg.beginDraw();
  93. pg.noFill();
  94. for (int i = 0; i <= height; i++) {
  95. float inter = map(i, y, y+h, 0, 1);
  96. color c = lerpColor(c2, c1, inter);
  97. pg.stroke(c);
  98. pg.strokeWeight(3);
  99. pg.ellipse(width/2, 200, i*4, i*2.3);
  100. }
  101. pg.endDraw();
  102. }
  103.  
  104. // Snowflake class
  105. class FlakeClass {
  106. float x, y, fallSpeed; // declare the variables used in the class
  107. float flakeSize, originalflakeSize, originalflakeX;
  108. float gustInfluence = random(.1, 3); // how much the wind affects the snowflake
  109. int alph = 255;
  110. boolean falling = true;
  111. int hitHeight;
  112. FlakeClass(float _x, float _y, float _flakeSize) {
  113. x = _x;
  114. y = _y;
  115. flakeSize = _flakeSize;
  116. originalflakeSize = _flakeSize;
  117. originalflakeX = x;
  118. fallSpeed = map(flakeSize, 2, 6, .5, 2);
  119. hitHeight = int(map(fallSpeed, .5, 2, height-10, height - 240));
  120. }
  121. void fall() {
  122. y += fallSpeed;
  123. if (y > hitHeight) {
  124. falling = false;
  125. }
  126. float wboost = map(x, 0, width, -1, 1); // move away from center while falling.
  127. if (falling == false) {
  128. alph -= 3;
  129. fallSpeed = movingForwardSpeed; // moves the flakes forward constant speed after they land.
  130. wboost = map(x, 0, width, -.25, .25); // move away from center while on ground.
  131. }
  132. if (alph <= 0 ) { //reset back to top
  133. y = -10;
  134. falling = true;
  135. flakeSize = originalflakeSize;
  136. x = originalflakeX;
  137. fallSpeed = map(flakeSize, 2, 6, .5, 2);
  138. alph = 255;
  139. }
  140. flakeSize += .005;
  141. x += wboost; // make it "zoom"
  142. if (falling) {
  143. for (int i = gustslist.size()-1; i >= 0; i--) { // look through every gust
  144. GustClass dgust = gustslist.get(i);
  145. float d = dist(dgust.x, dgust.y, x, y);
  146. float gboost = 0;
  147. if (d < dgust.gustSize) {
  148. gboost = map(d, 0, 200, gustInfluence, 0);
  149. }
  150. if (dgust.gustDir) {
  151. x -= gboost;
  152. } else {
  153. x += gboost;
  154. }
  155. }
  156. }
  157. }
  158. void display() {
  159. float snowValue = 60;
  160. int lightFalloff = 300;
  161. float dis = dist(x, y, width/2, height - 100);
  162.  
  163. if (dis < lightFalloff) {
  164. snowValue = map(dis, lightFalloff, 0, 60, 255);
  165. }
  166. float moveLight = map(lightY, 156, 176, -20, 20);
  167. snowValue += moveLight;
  168. // if( lightY >=176 || lightY <= 156){
  169. fill(snowValue, alph);
  170. if (falling) {
  171. ellipse(x, y, flakeSize, flakeSize);
  172. } else {
  173. float perspectiveFlake = map(y, height - 100, height, .7, 1.2);
  174. ellipse(x, y, (flakeSize * perspectiveFlake) * 1.2, (flakeSize * perspectiveFlake)/1.2);
  175. }
  176. }
  177. }
  178.  
  179. // wind class
  180. class GustClass {
  181. float x, y, speed; // declare the variables used in the class
  182. int counter = 200;
  183. int gustSize;
  184. boolean gustDir;
  185. GustClass() {
  186. //gustDir = _dir;
  187. resetGust();
  188. }
  189.  
  190. void resetGust() {
  191. y = random(height);
  192. gustDir = false;
  193. if (random(2) > 1) {
  194. gustDir = true;
  195. }
  196. if (!gustDir) {
  197. x = random(-100, -20);
  198. }
  199. else{
  200. x = random(width + 20, width + 100);
  201. }
  202. speed = random(.5, 3.25);
  203. gustSize = int(random(100, 300));
  204. }
  205. void moveGust() {
  206. if (gustDir) {
  207. x -= speed;
  208. } else {
  209. x += speed;
  210. }
  211. if (x > width + 120 || x < -120 ) {
  212. resetGust();
  213. }
  214. }
  215. void display() {
  216. stroke(0, 0, 255);
  217. noFill();
  218. ellipse(x, y, gustSize, gustSize);
  219. }
  220. }
  221.  
  222. /// line class
  223. class LineClass {
  224. float x, y; // declare the variables used in the class
  225. int counter = 200;
  226. float speed = movingForwardSpeed;
  227. LineClass( float _y) {
  228. y = _y; // height - 160
  229. }
  230. boolean finished() {
  231. counter --;
  232. if ( counter <= 0 ) {
  233. return true;
  234. } else {
  235. return false;
  236. }
  237. }
  238. void display() {
  239. float displaceX = map(y, height - 160, height, 30, 186);
  240. x = width/2 - 83 - displaceX;
  241. y += speed;
  242. float lineValue = 60;
  243. int lightFalloff = 300;
  244. float dis = dist(x, y, width/2 - 60, height - 160);
  245. if (dis < lightFalloff) {
  246. lineValue = map(dis, lightFalloff, 0, 180, 0);
  247. }
  248. if (y > height + 40) {
  249. y = height - 160;
  250. speed = movingForwardSpeed;
  251. }
  252. float moveLight = map(lightY, 156, 176, -20, 20);
  253. fill(lineValue, 180);
  254. pushMatrix();
  255. translate(x, y);
  256. float displaceScale = map(y, height - 160, height, 2, 10);
  257. scale(displaceScale);
  258. beginShape();
  259. vertex(1, -2);
  260. vertex(3, -2);
  261. vertex(-1, 2);
  262. vertex(-4, 2);
  263. endShape(CLOSE);
  264. popMatrix();
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement