package scripts;

import com.stencyl.graphics.G;
import com.stencyl.graphics.BitmapWrapper;

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.models.Joystick;

import com.stencyl.Engine;
import com.stencyl.Input;
import com.stencyl.Key;
import com.stencyl.utils.Utils;

import openfl.ui.Mouse;
import openfl.display.Graphics;
import openfl.display.BlendMode;
import openfl.display.BitmapData;
import openfl.display.Bitmap;
import openfl.events.Event;
import openfl.events.KeyboardEvent;
import openfl.events.TouchEvent;
import openfl.net.URLLoader;

import box2D.common.math.B2Vec2;
import box2D.dynamics.B2Body;
import box2D.dynamics.B2Fixture;
import box2D.dynamics.joints.B2Joint;

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;

import com.stencyl.graphics.shaders.BasicShader;
import com.stencyl.graphics.shaders.GrayscaleShader;
import com.stencyl.graphics.shaders.SepiaShader;
import com.stencyl.graphics.shaders.InvertShader;
import com.stencyl.graphics.shaders.GrainShader;
import com.stencyl.graphics.shaders.ExternalShader;
import com.stencyl.graphics.shaders.InlineShader;
import com.stencyl.graphics.shaders.BlurShader;
import com.stencyl.graphics.shaders.SharpenShader;
import com.stencyl.graphics.shaders.ScanlineShader;
import com.stencyl.graphics.shaders.CSBShader;
import com.stencyl.graphics.shaders.HueShader;
import com.stencyl.graphics.shaders.TintShader;
import com.stencyl.graphics.shaders.BloomShader;



class Design_3_3_PlayerMovement extends ActorScript
{
	public var _MAXSPEED:Float;
	public var _dir:String;
	public var _coldir:String;
	public var _rollingcounter:Float;
	public var _rolldirT:String;
	public var _rolldirdegrees:Float;
	public var _RollDirMap:Map<String,Dynamic>;
	
	
	public function new(dummy:Int, actor:Actor, dummy2:Engine)
	{
		super(actor);
		nameMap.set("Actor", "actor");
		nameMap.set("MAX SPEED", "_MAXSPEED");
		_MAXSPEED = 0.0;
		nameMap.set("dir", "_dir");
		_dir = "";
		nameMap.set("col dir", "_coldir");
		_coldir = "";
		nameMap.set("rolling counter", "_rollingcounter");
		_rollingcounter = 0.0;
		nameMap.set("roll dir T", "_rolldirT");
		_rolldirT = "";
		nameMap.set("roll dir degrees", "_rolldirdegrees");
		_rolldirdegrees = 0.0;
		nameMap.set("Roll Dir Map", "_RollDirMap");
		
	}
	
	override public function init()
	{
		
		/* ======================== When Creating ========================= */
		_MAXSPEED = asNumber(9);
		propertyChanged("_MAXSPEED", _MAXSPEED);
		_RollDirMap = new Map<String, Dynamic>();
		propertyChanged("_RollDirMap", _RollDirMap);
		_RollDirMap.set("n", -90);
		_RollDirMap.set("ne", -45);
		_RollDirMap.set("e", 0);
		_RollDirMap.set("se", 45);
		_RollDirMap.set("s", 90);
		_RollDirMap.set("sw", 135);
		_RollDirMap.set("w", 180);
		_RollDirMap.set("nw", -135);
		
		/* ======================== When Updating ========================= */
		addWhenUpdatedListener(null, function(elapsedTime:Float, list:Array<Dynamic>):Void
		{
			if(wrapper.enabled)
			{
				if(!(actor.getActorValue("rolling")))
				{
					/* "Animation" */ if((!((actor.getAnimation() == "unduck")) || ((actor.getAnimation() == "unduck") && (actor.getCurrentFrame() == 3))))
					{
						if((Math.abs(Math.sqrt((Math.pow(actor.getXVelocity(), 2) + Math.pow(actor.getYVelocity(), 2)))) > 1))
						{
							actor.setAnimation("" + ("" + "walk"));
							actor.setActorValue("camDir", _dir);
						}
						else
						{
							actor.setAnimation("" + ("" + "stop"));
							actor.setActorValue("camDir", 0);
						}
					}
					actor.setActorValue("dir", _dir);
					if(Engine.engine.getGameAttribute("disable movement"))
					{
						actor.setXVelocity(0);
						actor.setYVelocity(0);
						actor.setAnimation("" + ("" + "stop"));
						return;
					}
					if((isKeyDown("up") && (((!(isKeyDown("right")) && !(isKeyDown("left"))) || (isKeyDown("right") && isKeyDown("left"))) && !(isKeyDown("down")))))
					{
						/* N */
						actor.setVelocity(-90, _MAXSPEED);
						_rolldirT = "n";
						propertyChanged("_rolldirT", _rolldirT);
						_dir = "n";
						propertyChanged("_dir", _dir);
					}
					else if((isKeyDown("right") && ((!(isKeyDown("up")) && !(isKeyDown("down"))) && !(isKeyDown("left")))))
					{
						/* E */
						actor.setVelocity(0, _MAXSPEED);
						_rolldirT = "e";
						propertyChanged("_rolldirT", _rolldirT);
						_dir = "e";
						propertyChanged("_dir", _dir);
					}
					else if((isKeyDown("down") && (((!(isKeyDown("right")) && !(isKeyDown("left"))) || (isKeyDown("right") && isKeyDown("left"))) && !(isKeyDown("up")))))
					{
						/* S */
						actor.setVelocity(90, _MAXSPEED);
						_rolldirT = "s";
						propertyChanged("_rolldirT", _rolldirT);
						_dir = "s";
						propertyChanged("_dir", _dir);
					}
					else if((isKeyDown("left") && ((!(isKeyDown("down")) && !(isKeyDown("up"))) && !(isKeyDown("right")))))
					{
						/* W */
						actor.setVelocity(180, _MAXSPEED);
						_rolldirT = "w";
						propertyChanged("_rolldirT", _rolldirT);
						_dir = "w";
						propertyChanged("_dir", _dir);
					}
					else if(((isKeyDown("up") && isKeyDown("right")) && (!(isKeyDown("left")) && !(isKeyDown("down")))))
					{
						/* NE */
						_rolldirT = "ne";
						propertyChanged("_rolldirT", _rolldirT);
						if((_coldir == "e"))
						{
							actor.setVelocity(-90, _MAXSPEED);
							_dir = "n";
							propertyChanged("_dir", _dir);
						}
						else if((_coldir == "n"))
						{
							actor.setVelocity(0, _MAXSPEED);
							_dir = "e";
							propertyChanged("_dir", _dir);
						}
						else
						{
							actor.setVelocity(-45, _MAXSPEED);
							_dir = "ne";
							propertyChanged("_dir", _dir);
						}
					}
					else if(((isKeyDown("down") && isKeyDown("right")) && (!(isKeyDown("left")) && !(isKeyDown("up")))))
					{
						/* SE */
						_rolldirT = "se";
						propertyChanged("_rolldirT", _rolldirT);
						if((_coldir == "e"))
						{
							actor.setVelocity(90, _MAXSPEED);
							_dir = "s";
							propertyChanged("_dir", _dir);
						}
						else if((_coldir == "s"))
						{
							actor.setVelocity(0, _MAXSPEED);
							_dir = "e";
							propertyChanged("_dir", _dir);
						}
						else
						{
							actor.setVelocity(45, _MAXSPEED);
							_dir = "se";
							propertyChanged("_dir", _dir);
						}
					}
					else if(((isKeyDown("down") && isKeyDown("left")) && (!(isKeyDown("right")) && !(isKeyDown("up")))))
					{
						/* SW */
						_rolldirT = "sw";
						propertyChanged("_rolldirT", _rolldirT);
						if((_coldir == "s"))
						{
							actor.setVelocity(180, _MAXSPEED);
							_dir = "w";
							propertyChanged("_dir", _dir);
						}
						else if((_coldir == "w"))
						{
							actor.setVelocity(90, _MAXSPEED);
							_dir = "s";
							propertyChanged("_dir", _dir);
						}
						else
						{
							actor.setVelocity(135, _MAXSPEED);
							_dir = "sw";
							propertyChanged("_dir", _dir);
						}
					}
					else if(((isKeyDown("up") && isKeyDown("left")) && (!(isKeyDown("right")) && !(isKeyDown("down")))))
					{
						/* NW */
						_rolldirT = "nw";
						propertyChanged("_rolldirT", _rolldirT);
						if((_coldir == "n"))
						{
							actor.setVelocity(180, _MAXSPEED);
							_dir = "w";
							propertyChanged("_dir", _dir);
						}
						else if((_coldir == "w"))
						{
							actor.setVelocity(-90, _MAXSPEED);
							_dir = "n";
							propertyChanged("_dir", _dir);
						}
						else
						{
							actor.setVelocity(-135, _MAXSPEED);
							_dir = "nw";
							propertyChanged("_dir", _dir);
						}
					}
					else
					{
						actor.setXVelocity(0);
						actor.setYVelocity(0);
						_rolldirT = "none";
						propertyChanged("_rolldirT", _rolldirT);
						_dir = "none";
						propertyChanged("_dir", _dir);
					}
				}
				_coldir = "0";
				propertyChanged("_coldir", _coldir);
			}
		});
		
		/* ======================== Something Else ======================== */
		addCollisionListener(actor, function(event:Collision, list:Array<Dynamic>):Void
		{
			if(wrapper.enabled)
			{
				if(event.thisFromTop)
				{
					_coldir = "n";
					propertyChanged("_coldir", _coldir);
				}
				else if(event.thisFromRight)
				{
					_coldir = "e";
					propertyChanged("_coldir", _coldir);
				}
				else if(event.thisFromBottom)
				{
					_coldir = "s";
					propertyChanged("_coldir", _coldir);
				}
				else if(event.thisFromLeft)
				{
					_coldir = "w";
					propertyChanged("_coldir", _coldir);
				}
			}
		});
		
	}
	
	override public function forwardMessage(msg:String)
	{
		
	}
}