PageRenderTime 244ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/tests/ParticleExplorer.as

https://github.com/tonkoh/nd2d
ActionScript | 323 lines | 239 code | 54 blank | 30 comment | 4 complexity | 0c99a74b3f2919bdfe7a7d1b613e98f8 MD5 | raw file
  1. /*
  2. * ND2D - A Flash Molehill GPU accelerated 2D engine
  3. *
  4. * Author: Lars Gerckens
  5. * Copyright (c) nulldesign 2011
  6. * Repository URL: http://github.com/nulldesign/nd2d
  7. * Getting started: https://github.com/nulldesign/nd2d/wiki
  8. *
  9. *
  10. * Licence Agreement
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to deal
  14. * in the Software without restriction, including without limitation the rights
  15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. * copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. * THE SOFTWARE.
  29. */
  30. package tests {
  31. import com.bit101.components.ColorChooser;
  32. import com.bit101.components.HUISlider;
  33. import de.nulldesign.nd2d.display.ParticleSystem2D;
  34. import de.nulldesign.nd2d.display.Scene2D;
  35. import de.nulldesign.nd2d.materials.BlendModePresets;
  36. import de.nulldesign.nd2d.utils.ParticleSystemPreset;
  37. import flash.display.BitmapData;
  38. import flash.display.Sprite;
  39. import flash.events.Event;
  40. import flash.events.TimerEvent;
  41. import flash.utils.Timer;
  42. public class ParticleExplorer extends Scene2D {
  43. [Embed(source="/assets/particle_small.png")]
  44. private var particleClass:Class;
  45. private var bmp:BitmapData;
  46. private var maxParticles:uint = 2000;
  47. private var timer:Timer = new Timer(2000, 0);
  48. private var particles:ParticleSystem2D;
  49. private var preset:ParticleSystemPreset = new ParticleSystemPreset();
  50. private var panel:Sprite;
  51. public function ParticleExplorer() {
  52. bmp = new particleClass().bitmapData;
  53. particles = new ParticleSystem2D(bmp, maxParticles, preset);
  54. particles.blendMode = BlendModePresets.ADD;
  55. timer.addEventListener(TimerEvent.TIMER, updateSystem);
  56. addChild(particles);
  57. addEventListener(Event.ADDED_TO_STAGE, addedToStage);
  58. addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
  59. }
  60. private function removedFromStage(e:Event):void {
  61. if(panel) {
  62. stage.removeChild(panel);
  63. }
  64. }
  65. protected function addedToStage(event:Event):void {
  66. if(!panel) {
  67. panel = new Sprite();
  68. panel.graphics.beginFill(0x000000, 1.0);
  69. panel.graphics.drawRect(0, 0, 200, 450);
  70. panel.graphics.endFill();
  71. var s:HUISlider;
  72. var c:ColorChooser;
  73. var nextY:Number = 5;
  74. s = new HUISlider(panel, 0, nextY, "minStartX", changeHandler);
  75. s.minimum = -stage.stageWidth / 2;
  76. s.maximum = stage.stageWidth / 2;
  77. nextY += 20;
  78. s = new HUISlider(panel, 0, nextY, "maxStartX", changeHandler);
  79. s.minimum = -stage.stageWidth / 2;
  80. s.maximum = stage.stageWidth / 2;
  81. nextY += 20;
  82. s = new HUISlider(panel, 0, nextY, "minStartY", changeHandler);
  83. s.minimum = -stage.stageHeight / 2;
  84. s.maximum = stage.stageHeight / 2;
  85. nextY += 20;
  86. s = new HUISlider(panel, 0, nextY, "maxStartY", changeHandler);
  87. s.minimum = -stage.stageHeight / 2;
  88. s.maximum = stage.stageHeight / 2;
  89. nextY += 20;
  90. s = new HUISlider(panel, 0, nextY, "minSpeed", changeHandler);
  91. s.minimum = 0;
  92. s.maximum = 1000;
  93. s.value = preset.minSpeed;
  94. nextY += 20;
  95. s = new HUISlider(panel, 0, nextY, "maxSpeed", changeHandler);
  96. s.minimum = 0;
  97. s.maximum = 1000;
  98. s.value = preset.maxSpeed;
  99. nextY += 20;
  100. s = new HUISlider(panel, 0, nextY, "minEmitAngle", changeHandler);
  101. s.minimum = 0;
  102. s.maximum = 360;
  103. s.value = preset.minEmitAngle;
  104. nextY += 20;
  105. s = new HUISlider(panel, 0, nextY, "maxEmitAngle", changeHandler);
  106. s.minimum = 0;
  107. s.maximum = 360;
  108. s.value = preset.maxEmitAngle;
  109. nextY += 20;
  110. c = new ColorChooser(panel, 0, nextY, preset.startColor, changeHandler);
  111. c.tag = 0;
  112. nextY += 20;
  113. c = new ColorChooser(panel, 0, nextY, preset.startColorVariance, changeHandler);
  114. c.tag = 1;
  115. nextY += 20;
  116. s = new HUISlider(panel, 0, nextY, "startAlpha", changeHandler);
  117. s.minimum = 0;
  118. s.maximum = 1;
  119. s.value = preset.startAlpha;
  120. nextY += 20;
  121. c = new ColorChooser(panel, 0, nextY, preset.endColor, changeHandler);
  122. c.tag = 2;
  123. nextY += 20;
  124. c = new ColorChooser(panel, 0, nextY, preset.endColorVariance, changeHandler);
  125. c.tag = 3;
  126. nextY += 20;
  127. s = new HUISlider(panel, 0, nextY, "endAlpha", changeHandler);
  128. s.minimum = 0;
  129. s.maximum = 1;
  130. s.value = preset.endAlpha;
  131. nextY += 20;
  132. s = new HUISlider(panel, 0, nextY, "spawnDelay", changeHandler);
  133. s.minimum = 0;
  134. s.maximum = 100;
  135. s.value = preset.spawnDelay;
  136. nextY += 20;
  137. s = new HUISlider(panel, 0, nextY, "minLife", changeHandler);
  138. s.minimum = 0;
  139. s.maximum = 10000;
  140. s.value = preset.minLife;
  141. nextY += 20;
  142. s = new HUISlider(panel, 0, nextY, "maxLife", changeHandler);
  143. s.minimum = 0;
  144. s.maximum = 10000;
  145. s.value = preset.maxLife;
  146. nextY += 20;
  147. s = new HUISlider(panel, 0, nextY, "minStartSize", changeHandler);
  148. s.minimum = 0;
  149. s.maximum = 10;
  150. s.value = preset.minStartSize;
  151. nextY += 20;
  152. s = new HUISlider(panel, 0, nextY, "maxStartSize", changeHandler);
  153. s.minimum = 0;
  154. s.maximum = 10;
  155. s.value = preset.maxStartSize;
  156. nextY += 20;
  157. s = new HUISlider(panel, 0, nextY, "minEndSize", changeHandler);
  158. s.minimum = 0;
  159. s.maximum = 10;
  160. s.value = preset.minEndSize;
  161. nextY += 20;
  162. s = new HUISlider(panel, 0, nextY, "maxEndSize", changeHandler);
  163. s.minimum = 0;
  164. s.maximum = 10;
  165. s.value = preset.maxEndSize;
  166. nextY += 20;
  167. s = new HUISlider(panel, 0, nextY, "maxParticles", changeHandler);
  168. s.minimum = 0;
  169. s.maximum = 10000;
  170. s.value = maxParticles;
  171. }
  172. stage.addChild(panel);
  173. }
  174. private function changeHandler(e:Event):void {
  175. var s:HUISlider = e.target as HUISlider;
  176. var c:ColorChooser = e.target as ColorChooser;
  177. //drrty switch ;)
  178. if(s) {
  179. switch(s.label) {
  180. case "minStartX":
  181. preset.minStartPosition.x = s.value;
  182. break;
  183. case "minStartY":
  184. preset.minStartPosition.y = s.value;
  185. break;
  186. case "maxStartX":
  187. preset.maxStartPosition.x = s.value;
  188. break;
  189. case "maxStartY":
  190. preset.maxStartPosition.y = s.value;
  191. break;
  192. case "minSpeed":
  193. preset.minSpeed = s.value;
  194. break;
  195. case "maxSpeed":
  196. preset.maxSpeed = s.value;
  197. break;
  198. case "minEmitAngle":
  199. preset.minEmitAngle = s.value;
  200. break;
  201. case "maxEmitAngle":
  202. preset.maxEmitAngle = s.value;
  203. break;
  204. case "startAlpha":
  205. preset.startAlpha = s.value;
  206. break;
  207. case "endAlpha":
  208. preset.endAlpha = s.value;
  209. break;
  210. case "spawnDelay":
  211. preset.spawnDelay = s.value;
  212. break;
  213. case "minLife":
  214. preset.minLife = s.value;
  215. break;
  216. case "maxLife":
  217. preset.maxLife = s.value;
  218. break;
  219. case "minStartSize":
  220. preset.minStartSize = s.value;
  221. break;
  222. case "maxStartSize":
  223. preset.maxStartSize = s.value;
  224. break;
  225. case "minEndSize":
  226. preset.minEndSize = s.value;
  227. break;
  228. case "maxEndSize":
  229. preset.maxEndSize = s.value;
  230. break;
  231. case "maxParticles":
  232. maxParticles = s.value;
  233. break;
  234. }
  235. }
  236. if(c) {
  237. switch(c.tag) {
  238. case 0:
  239. preset.startColor = c.value;
  240. break;
  241. case 1:
  242. preset.startColorVariance = c.value;
  243. break;
  244. case 2:
  245. preset.endColor = c.value;
  246. break;
  247. case 3:
  248. preset.endColorVariance = c.value;
  249. break;
  250. }
  251. }
  252. timer.reset();
  253. timer.start();
  254. }
  255. private function updateSystem(e:TimerEvent):void {
  256. removeChild(particles);
  257. particles = new ParticleSystem2D(bmp, maxParticles, preset);
  258. particles.blendMode = BlendModePresets.ADD;
  259. addChild(particles);
  260. timer.stop();
  261. }
  262. override protected function step(t:Number):void {
  263. particles.x = stage.stageWidth / 2;
  264. particles.y = stage.stageHeight / 2;
  265. particles.gravity.x = (stage.mouseX / stage.stageWidth * 2.0 - 1.0) * 2000.0;
  266. particles.gravity.y = (stage.mouseY / stage.stageHeight * 2.0 - 1.0) * 2000.0;
  267. }
  268. }
  269. }