Particles2d.hx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import h3d.mat.Texture;
  2. import h2d.Graphics;
  3. import h2d.Particles;
  4. import hxd.Res;
  5. class Particles2d extends SampleApp {
  6. var g : ParticleGroup;
  7. var particles : Particles;
  8. var movableParticleGroup : ParticleGroup;
  9. var time : Float;
  10. var arrow : Texture;
  11. var square : Texture;
  12. var moving : Bool = false;
  13. override function init() {
  14. super.init();
  15. square = null;// h2d.Tile.fromColor(0xFFFFFF, 16, 16).getTexture();
  16. arrow = Res.arrow.toTexture();
  17. particles = new Particles(s2d);
  18. g = new ParticleGroup(particles);
  19. particles.addGroup(g);
  20. addSlider("Amount", function() return g.nparts, function(v) g.nparts = Std.int(v), 1, 1000);
  21. addCheck("Sort", function() return g.sortMode == Dynamic, function(v) g.sortMode = v ? Dynamic : None);
  22. addCheck("Loop", function() return g.emitLoop, function(v) { g.emitLoop = v; });
  23. addCheck("Move", function() return moving, function(v) moving = v);
  24. addCheck("Relative", function() return g.isRelative, function(v) g.isRelative = v);
  25. addCheck("Dir as Angle", function() return g.emitDirectionAsAngle, function(v) { g.emitDirectionAsAngle = v; g.texture = v ? arrow : square; });
  26. addCheck("RebuildMode", function() return g.rebuildOnChange, function(v) { g.rebuildOnChange = v; });
  27. addChoice("EmitMode", ["Point A", "Point B", "Cone", "Box", "Dir A", "Dir B"], function (v) {
  28. switch(v) {
  29. case 0: changeToPointDemo();
  30. case 1: changeToPointAndDirectionAsAngleDemo();
  31. case 2: changeToConeDemo();
  32. case 3: changeToBoxDemo();
  33. case 4: changeToDirectionDemo();
  34. case 5: changeToDirectionAndDirectionAsAngleDemo();
  35. }
  36. });
  37. // addButton("PartEmitMode.Point Demo", changeToPointDemo);
  38. // addButton("PartEmitMode.Point + emitDirectionAsAngle Demo", changeToPointAndDirectionAsAngleDemo);
  39. // addButton("PartEmitMode.Cone Demo", changeToConeDemo);
  40. // addButton("PartEmitMode.Box Demo", changeToBoxDemo);
  41. // addButton("PartEmitMode.Direction Demo", changeToDirectionDemo);
  42. // addButton("PartEmitMode.Direction + emitDirectionAsAngle Demo", changeToDirectionAndDirectionAsAngleDemo);
  43. changeToPointDemo();
  44. }
  45. function changeToPointDemo() {
  46. // clear();
  47. reset();
  48. // g = new ParticleGroup(particles);
  49. g.sizeRand = .2;
  50. g.life = 1;
  51. g.speed = 100;
  52. g.speedRand = 3;
  53. g.rotSpeed = 2;
  54. g.emitMode = PartEmitMode.Point;
  55. g.emitDist = 0;
  56. g.fadeIn = 0;
  57. g.fadeOut = 0;
  58. g.dx = cast s2d.width / 2;
  59. g.dy = cast s2d.height / 2;
  60. // particles.addGroup(g);
  61. }
  62. function changeToPointAndDirectionAsAngleDemo() {
  63. // clear();
  64. reset();
  65. // g = new ParticleGroup(particles);
  66. g.size = .8;
  67. g.sizeRand = .2;
  68. g.life = .5;
  69. g.speed = 100;
  70. g.speedRand = 3;
  71. g.emitMode = PartEmitMode.Point;
  72. g.emitDist = 0;
  73. g.fadeIn = 0;
  74. g.fadeOut = 0;
  75. g.dx = cast s2d.width / 2;
  76. g.dy = cast s2d.height / 2;
  77. // particles.addGroup(g);
  78. }
  79. function changeToConeDemo() {
  80. // clear();
  81. reset();
  82. // g = new ParticleGroup(particles);
  83. g.size = .2;
  84. g.gravity = 1;
  85. g.life = 5;
  86. g.speed = 100;
  87. g.speedRand = 3;
  88. g.emitMode = PartEmitMode.Cone;
  89. g.emitAngle = Math.PI;
  90. g.emitDist = 0;
  91. g.emitDistY = 0;
  92. g.fadeIn = 1;
  93. g.fadeOut = 1;
  94. g.dx = cast s2d.width / 2;
  95. g.dy = cast s2d.height / 2;
  96. // particles.addGroup(g);
  97. }
  98. function changeToBoxDemo() {
  99. // clear();
  100. reset();
  101. // g = new ParticleGroup(particles);
  102. g.size = .2;
  103. g.gravity = 1;
  104. g.life = 5;
  105. g.speed = 100;
  106. g.speedRand = 3;
  107. g.emitMode = PartEmitMode.Box;
  108. g.emitAngle = Math.PI;
  109. g.emitDist = s2d.width;
  110. g.emitDistY = s2d.height;
  111. g.dx = cast s2d.width / 2;
  112. // particles.addGroup(g);
  113. }
  114. function changeToDirectionDemo() {
  115. // clear();
  116. reset();
  117. // g = new ParticleGroup(particles);
  118. g.size = .2;
  119. g.gravity = 1;
  120. g.life = 5;
  121. g.speed = 100;
  122. g.speedRand = 3;
  123. g.emitMode = PartEmitMode.Direction;
  124. g.emitDist = s2d.width;
  125. g.emitAngle = Math.PI / 2;
  126. g.fadeOut = .5;
  127. // particles.addGroup(g);
  128. }
  129. function changeToDirectionAndDirectionAsAngleDemo() {
  130. // clear();
  131. reset();
  132. // g = new ParticleGroup(particles);
  133. g.size = .8;
  134. g.sizeRand = .2;
  135. g.life = 6;
  136. g.speed = 200;
  137. g.speedRand = 3;
  138. g.emitMode = PartEmitMode.Direction;
  139. g.emitDist = s2d.height;
  140. g.emitAngle = Math.PI / 4;
  141. g.fadeIn = 0;
  142. g.fadeOut = 0;
  143. // particles.addGroup(g);
  144. }
  145. static function main() {
  146. Res.initEmbed();
  147. new Particles2d();
  148. }
  149. function reset() {
  150. g.dx = 0;
  151. g.dy = 0;
  152. g.emitDist = 50.;
  153. g.emitDistY = 50.;
  154. g.emitAngle = -.5;
  155. g.emitSync = 0;
  156. g.emitDelay = 0;
  157. g.emitStartDist = 0.;
  158. g.life = 1;
  159. g.lifeRand = 0;
  160. g.sizeIncr = 0;
  161. g.incrX = true;
  162. g.incrY = true;
  163. g.size = 1;
  164. g.sizeRand = 0;
  165. g.speed = 50.;
  166. g.speedRand = 0;
  167. g.speedIncr = 0;
  168. g.gravity = 0;
  169. g.gravityAngle = 0;
  170. g.rotInit = 0;
  171. g.rotSpeed = 0;
  172. g.rotSpeedRand = 0;
  173. g.rotAuto = false;
  174. g.fadeIn = .2;
  175. g.fadeOut = .8;
  176. g.fadePower = 1;
  177. particles.x = 0;
  178. particles.y = 0;
  179. }
  180. function clear():Void {
  181. time = 0;
  182. if (movableParticleGroup != null) movableParticleGroup = null;
  183. if (g != null) particles.removeGroup(g);
  184. }
  185. override function update(dt:Float) {
  186. super.update(dt);
  187. if( moving ) {
  188. time += dt * 0.6;
  189. particles.x = Math.cos(time) * (s2d.width / 4);
  190. particles.y = Math.sin(time) * (s2d.height / 4);
  191. }
  192. // if (movableParticleGroup != null)
  193. // {
  194. // time += dt * 0.01;
  195. // movableParticleGroup.dx = cast s2d.width / 2 + Math.cos(time) * 200;
  196. // movableParticleGroup.dy = cast s2d.height / 2 + Math.sin(time) * 200;
  197. // }
  198. }
  199. }