Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Stencyl exclusively uses the Haxe programming language.
- Haxe is similar to ActionScript and JavaScript.
- Want to use native code or make something reusable? Use the Extensions Framework instead.
- http://www.stencyl.com/help/view/engine-extensions/
- Learn more about Haxe and our APIs
- http://www.stencyl.com/help/view/haxe/
- */
- package scripts;
- //==========================================================
- // Imports
- //==========================================================
- import com.stencyl.graphics.G;
- import com.stencyl.behavior.Script;
- import com.stencyl.behavior.Script.*;
- import com.stencyl.behavior.ActorScript;
- import com.stencyl.behavior.SceneScript;
- import com.stencyl.behavior.TimedTask;
- import com.stencyl.models.Actor;
- import com.stencyl.models.GameModel;
- import com.stencyl.models.actor.Animation;
- import com.stencyl.models.actor.ActorType;
- import com.stencyl.models.actor.Collision;
- import com.stencyl.models.actor.Group;
- import com.stencyl.models.Scene;
- import com.stencyl.models.Sound;
- import com.stencyl.models.Region;
- import com.stencyl.models.Font;
- import com.stencyl.Engine;
- import com.stencyl.Input;
- import com.stencyl.utils.Utils;
- import nme.ui.Mouse;
- import nme.display.Graphics;
- import nme.display.BitmapData;
- import nme.geom.Rectangle;
- import nme.geom.Point;
- import nme.utils.ByteArray;
- import motion.Actuate;
- import motion.easing.Back;
- import motion.easing.Cubic;
- import motion.easing.Elastic;
- import motion.easing.Expo;
- import motion.easing.Linear;
- import motion.easing.Quad;
- import motion.easing.Quart;
- import motion.easing.Quint;
- import motion.easing.Sine;
- class Distortion_Effect extends ActorScript
- {
- //Expose your attributes like this
- //Need further help? See: http://www.stencyl.com/help/view/code-mode/
- //@:attribute("id='1' name='Display Name' desc='An Attribute'")
- //public var attributeName:String;
- @:attribute("id='1' name='amplitude' desc='Distortion amplitude, the peak of offset. Larger values distort more'")
- public var Amp:Float;
- @:attribute("id='2' name='frequency' desc='distortion frequency, the number of oscillations'")
- public var Freq:Float;
- @:attribute("id='3' name='frameskip' desc='frameskip for setting speed of distortion, maybe'")
- public var Speed:Float;
- @:attribute("id='4' name='direction' desc='Wave direction of motion. 0 is horizontal, all else is vertical'")
- public var dir:Int;
- @:attribute("id='5' name='orientation' desc='Wave orientation. 0 is horizontal, all else is vertical'")
- public var orient:Int;
- public var sWidth:Int;
- public var sHeight:Int;
- public var bmp:BitmapData;
- public var bmpModded:BitmapData;
- public var linePixels:ByteArray;
- public var rect:Rectangle;
- public var pt:Point;
- public var time:Float;
- override public function init()
- {
- #if cpp
- bmp = actor.currAnimation.tilesheet.__bitmap.clone();
- #else
- bmp = actor.currAnimation.bitmapData.clone();
- #end
- sWidth = Std.int(bmp.width);
- sHeight = Std.int(bmp.height);
- bmpModded = new BitmapData(sWidth, sHeight, true, 0);
- rect = new Rectangle(0,0,sWidth,sHeight);
- pt = new Point(0,0);
- bmpModded = bmp.clone();
- time = 0;
- }
- public inline function update(elapsedTime:Float)
- {
- #if cpp
- bmp = actor.currAnimation.tilesheet.__bitmap.clone();
- #else
- bmp = actor.currAnimation.bitmapData.clone();
- #end
- sWidth = Std.int(bmp.width);
- sHeight = Std.int(bmp.height);
- bmpModded = new BitmapData(sWidth, sHeight, true, 0);
- rect = new Rectangle(0,0,sWidth,sHeight);
- pt = new Point(0,0);
- bmpModded = bmp.clone();
- time += elapsedTime/1000;
- if(orient == 0) //horizontal orientation of waves
- {
- for(i in 0...bmp.height) //lets loop all rows of the bitmap data
- {
- var o:Int = getOffset(i, time); //get an offset amount for the line
- var lineRect:Rectangle = new Rectangle(0,i,sWidth,1); //create a rectangle for the line we want to tweak
- if(dir == 0) //horizontal motion
- {
- var p = new Point(o,i); //get a point that's offset horizontally
- bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
- }
- else //vertical motion
- {
- var p = new Point(0,i-o); //get a point that's offset vertically
- bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
- }
- }
- }
- else //vertical oriention of waves
- {
- for(i in 0...bmp.width) //lets loop all columns of the bitmap data
- {
- var o:Int = getOffset(i, time); //get an offset amount for the line
- var lineRect:Rectangle = new Rectangle(i,0,1,sHeight); //create a rectangle for the line we want to tweak
- if(dir == 0) //horizontal motion
- {
- var p = new Point(i-o,0); //get a point that's offset horizontally
- bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
- }
- else //vertical motion
- {
- var p = new Point(i,o); //get a point that's offset vertically
- bmpModded.copyPixels(bmp,lineRect,p); //copy the pixels to their new location
- }
- }
- }
- #if cpp
- actor.currAnimation.tilesheet.__bitmap.copyPixels(bmpModded, rect, pt);
- #else
- actor.currAnimation.bitmapData.copyPixels(bmpModded, rect, pt);
- #end
- }
- private function getOffset(y:Float, t:Float):Int
- {
- var offset:Int = 0;
- offset = Std.int(Amp*Math.sin( Freq*y + Speed*t ));
- return offset;
- }
- public inline function draw(g:G)
- {
- }
- //==========================================================
- // Don't edit below unless you know what you're doing
- //==========================================================
- public function new(dummy:Int, actor:Actor, dummy2:Engine)
- {
- super(actor);
- addWhenUpdatedListener(null, onUpdate);
- addWhenDrawingListener(null, onDraw);
- }
- public function onUpdate(elapsedTime:Float, list:Array<Dynamic>)
- {
- if(wrapper.enabled)
- {
- update(elapsedTime);
- }
- }
- public function onDraw(g:G, x:Float, y:Float, list:Array<Dynamic>)
- {
- if(wrapper.enabled)
- {
- draw(g);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement