Advertisement
Guest User

Untitled

a guest
Mar 10th, 2015
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. /*
  2. Stencyl exclusively uses the Haxe programming language.
  3. Haxe is similar to ActionScript and JavaScript.
  4.  
  5. Want to use native code or make something reusable? Use the Extensions Framework instead.
  6. http://www.stencyl.com/help/view/engine-extensions/
  7.  
  8. Learn more about Haxe and our APIs
  9. http://www.stencyl.com/help/view/haxe/
  10. */
  11.  
  12. package scripts;
  13.  
  14.  
  15. //==========================================================
  16. // Imports
  17. //==========================================================
  18.  
  19. import com.stencyl.graphics.G;
  20.  
  21. import com.stencyl.behavior.Script;
  22. import com.stencyl.behavior.Script.*;
  23. import com.stencyl.behavior.ActorScript;
  24. import com.stencyl.behavior.SceneScript;
  25. import com.stencyl.behavior.TimedTask;
  26.  
  27. import com.stencyl.models.Actor;
  28. import com.stencyl.models.GameModel;
  29. import com.stencyl.models.actor.Animation;
  30. import com.stencyl.models.actor.ActorType;
  31. import com.stencyl.models.actor.Collision;
  32. import com.stencyl.models.actor.Group;
  33. import com.stencyl.models.Scene;
  34. import com.stencyl.models.Sound;
  35. import com.stencyl.models.Region;
  36. import com.stencyl.models.Font;
  37.  
  38. import com.stencyl.Engine;
  39. import com.stencyl.Input;
  40. import com.stencyl.utils.Utils;
  41.  
  42. import nme.ui.Mouse;
  43. import nme.display.Graphics;
  44. import nme.display.BitmapData;
  45. import nme.geom.Rectangle;
  46. import nme.geom.Point;
  47. import nme.utils.ByteArray;
  48.  
  49.  
  50. import motion.Actuate;
  51. import motion.easing.Back;
  52. import motion.easing.Cubic;
  53. import motion.easing.Elastic;
  54. import motion.easing.Expo;
  55. import motion.easing.Linear;
  56. import motion.easing.Quad;
  57. import motion.easing.Quart;
  58. import motion.easing.Quint;
  59. import motion.easing.Sine;
  60.  
  61.  
  62. class Design_12_12_Distortion extends ActorScript
  63. {
  64. //Expose your attributes like this
  65. //Need further help? See: http://www.stencyl.com/help/view/code-mode/
  66. //@:attribute("id='1' name='Display Name' desc='An Attribute'")
  67. //public var attributeName:String;
  68. @:attribute("id='1' name='amplitude' desc='Distortion amplitude, the peak of offset. Larger values distort more'")
  69. public var Amp:Float;
  70. @:attribute("id='2' name='frequency' desc='distortion frequency, the number of oscillations'")
  71. public var Freq:Float;
  72. @:attribute("id='3' name='frameskip' desc='frameskip for setting speed of distortion, maybe'")
  73. public var Speed:Float;
  74. @:attribute("id='4' name='direction' desc='Wave direction of motion. 0 is horizontal, all else is vertical'")
  75. public var dir:Int;
  76. @:attribute("id='5' name='orientation' desc='Wave orientation. 0 is horizontal, all else is vertical'")
  77. public var orient:Int;
  78. public var sWidth:Int;
  79. public var sHeight:Int;
  80. public var bmp:BitmapData;
  81. public var bmpModded:BitmapData;
  82. public var linePixels:ByteArray;
  83. public var rect:Rectangle;
  84. public var pt:Point;
  85. public var time:Float;
  86.  
  87. override public function init()
  88. {
  89. #if cpp
  90. bmp = actor.currAnimation.tilesheet.__bitmap.clone();
  91. #else
  92. bmp = actor.currAnimation.bitmapData.clone();
  93. #end
  94. sWidth = Std.int(bmp.width);
  95. sHeight = Std.int(bmp.height);
  96. bmpModded = new BitmapData(sWidth, sHeight, true, 0);
  97. rect = new Rectangle(0,0,sWidth,sHeight);
  98. pt = new Point(0,0);
  99.  
  100. bmpModded = bmp.clone();
  101. time = 0;
  102. }
  103.  
  104. public inline function update(elapsedTime:Float)
  105. {
  106. time += elapsedTime/1000;
  107. if(orient == 0) //horizontal orientation of waves
  108. {
  109.  
  110. for(i in 0...bmp.height) //lets loop all rows of the bitmap data
  111. {
  112.  
  113. var o:Int = getOffset(i, time); //get an offset amount for the line
  114. var lineRect:Rectangle = new Rectangle(0,i,sWidth,1); //create a rectangle for the line we want to tweak
  115. if(dir == 0) //horizontal motion
  116. {
  117. var p = new Point(o,i); //get a point that's offset horizontally
  118. bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
  119. }
  120. else //vertical motion
  121. {
  122. var p = new Point(0,i-o); //get a point that's offset vertically
  123. bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
  124. }
  125.  
  126. }
  127. }
  128. else //vertical oriention of waves
  129. {
  130. for(i in 0...bmp.width) //lets loop all columns of the bitmap data
  131. {
  132.  
  133. var o:Int = getOffset(i, time); //get an offset amount for the line
  134. var lineRect:Rectangle = new Rectangle(i,0,1,sHeight); //create a rectangle for the line we want to tweak
  135. if(dir == 0) //horizontal motion
  136. {
  137. var p = new Point(i-o,0); //get a point that's offset horizontally
  138. bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
  139. }
  140. else //vertical motion
  141. {
  142. var p = new Point(i,o); //get a point that's offset vertically
  143. bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
  144. }
  145.  
  146. }
  147. }
  148. #if cpp
  149. actor.currAnimation.tilesheet.__bitmap.copyPixels(bmpModded, rect, pt);
  150. #else
  151. actor.currAnimation.bitmapData.copyPixels(bmpModded, rect, pt);
  152. #end
  153.  
  154.  
  155. }
  156. private function getOffset(y:Float, t:Float):Int
  157. {
  158.  
  159. var offset:Int = 0;
  160. offset = Std.int(Amp*Math.sin( Freq*y + Speed*t ));
  161. return offset;
  162. }
  163. public inline function draw(g:G)
  164. {
  165. }
  166.  
  167. //Need to handle collisions and other event types? Visit TODO
  168.  
  169.  
  170.  
  171.  
  172.  
  173. //==========================================================
  174. // Don't edit below unless you know what you're doing
  175. //==========================================================
  176.  
  177. public function new(dummy:Int, actor:Actor, dummy2:Engine)
  178. {
  179. super(actor);
  180.  
  181. addWhenUpdatedListener(null, onUpdate);
  182. addWhenDrawingListener(null, onDraw);
  183. }
  184.  
  185. public function onUpdate(elapsedTime:Float, list:Array<Dynamic>)
  186. {
  187. if(wrapper.enabled)
  188. {
  189. update(elapsedTime);
  190. }
  191. }
  192.  
  193. public function onDraw(g:G, x:Float, y:Float, list:Array<Dynamic>)
  194. {
  195. if(wrapper.enabled)
  196. {
  197. draw(g);
  198. }
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement