Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 7.37 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 Distortion_Effect 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.                 #if cpp
  107.           bmp = actor.currAnimation.tilesheet.__bitmap.clone();
  108.           #else
  109.           bmp = actor.currAnimation.bitmapData.clone();
  110.           #end
  111.         sWidth = Std.int(bmp.width);
  112.         sHeight = Std.int(bmp.height);
  113.         bmpModded = new BitmapData(sWidth, sHeight, true, 0);
  114.         rect = new Rectangle(0,0,sWidth,sHeight);
  115.           pt = new Point(0,0);
  116.  
  117.           bmpModded = bmp.clone();
  118.                 time += elapsedTime/1000;
  119.                
  120.                 if(orient == 0) //horizontal orientation of waves
  121.                 {
  122.                        
  123.                         for(i in 0...bmp.height) //lets loop all rows of the bitmap data
  124.                 {
  125.                        
  126.                         var o:Int = getOffset(i, time); //get an offset amount for the line
  127.                         var lineRect:Rectangle = new Rectangle(0,i,sWidth,1); //create a rectangle for the line we want to tweak
  128.                         if(dir == 0) //horizontal motion
  129.                         {
  130.                         var p = new Point(o,i); //get a point that's offset horizontally
  131.                                 bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
  132.                         }
  133.                         else //vertical motion
  134.                         {
  135.                                 var p = new Point(0,i-o); //get a point that's offset vertically
  136.                                 bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
  137.                         }
  138.                
  139.                 }
  140.                 }
  141.                 else //vertical oriention of waves
  142.                 {
  143.                         for(i in 0...bmp.width) //lets loop all columns of the bitmap data
  144.                 {
  145.                        
  146.                         var o:Int = getOffset(i, time); //get an offset amount for the line
  147.                         var lineRect:Rectangle = new Rectangle(i,0,1,sHeight); //create a rectangle for the line we want to tweak
  148.                         if(dir == 0) //horizontal motion
  149.                         {
  150.                         var p = new Point(i-o,0); //get a point that's offset horizontally
  151.                                 bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
  152.                         }
  153.                         else //vertical motion
  154.                         {
  155.                                 var p = new Point(i,o); //get a point that's offset vertically
  156.                                 bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
  157.                         }
  158.                
  159.                 }
  160.                 }
  161.           #if cpp
  162.           actor.currAnimation.tilesheet.__bitmap.copyPixels(bmpModded, rect, pt);
  163.           #else
  164.           actor.currAnimation.bitmapData.copyPixels(bmpModded, rect, pt);
  165.           #end
  166.  
  167.                
  168.         }
  169.         private function getOffset(y:Float, t:Float):Int
  170.         {
  171.                
  172.                 var offset:Int = 0;
  173.                 offset = Std.int(Amp*Math.sin( Freq*y + Speed*t ));
  174.                 return offset;
  175.         }
  176.         public inline function draw(g:G)
  177.         {
  178.         }
  179.  
  180.    
  181.     //==========================================================
  182.     // Don't edit below unless you know what you're doing
  183.     //==========================================================
  184.    
  185.     public function new(dummy:Int, actor:Actor, dummy2:Engine)
  186.     {
  187.         super(actor);
  188.        
  189.         addWhenUpdatedListener(null, onUpdate);
  190.         addWhenDrawingListener(null, onDraw);
  191.     }
  192.    
  193.     public function onUpdate(elapsedTime:Float, list:Array<Dynamic>)
  194.     {
  195.         if(wrapper.enabled)
  196.         {
  197.             update(elapsedTime);
  198.         }
  199.     }
  200.    
  201.     public function onDraw(g:G, x:Float, y:Float, list:Array<Dynamic>)
  202.     {
  203.         if(wrapper.enabled)
  204.         {
  205.             draw(g);
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement