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_15_15_RoomCollisionData extends ActorScript
{
	public var _collision:Array<Dynamic>;
	public var _currentrow:Float;
	public var _currentcolumn:Float;
	public var _childmarkers:Array<Dynamic>;
	
	/* ========================= Custom Block ========================= */
	public function _customBlock_checkFurnPlaceable():Bool
	{
		var __Self:Actor = actor;
		_currentrow = asNumber(0);
		propertyChanged("_currentrow", _currentrow);
		for(item in cast(_collision, Array<Dynamic>))
		{
			_currentcolumn = asNumber(0);
			propertyChanged("_currentcolumn", _currentcolumn);
			for(item in cast(("" + item).split(""), Array<Dynamic>))
			{
				if((item == "x"))
				{
					for(actorOfType in getActorsOfType(getActorType(22)))
					{
						if(actorOfType != null && !actorOfType.dead && !actorOfType.recycled){
							if(((actorOfType.getX() == (actor.getX() + (_currentcolumn * 16))) && (actorOfType.getY() == (actor.getY() + (_currentrow * 16)))))
							{
								return false;
							}
						}
					}
					_currentcolumn += 1;
					propertyChanged("_currentcolumn", _currentcolumn);
				}
			}
			_currentrow += 1;
			propertyChanged("_currentrow", _currentrow);
		}
		return true;
	}
	
	/* ========================= Custom Event ========================= */
	public function _customEvent_placeMarkers():Void
	{
		_childmarkers = new Array<Dynamic>();
		propertyChanged("_childmarkers", _childmarkers);
		_currentrow = asNumber(0);
		propertyChanged("_currentrow", _currentrow);
		for(item in cast(_collision, Array<Dynamic>))
		{
			_currentcolumn = asNumber(0);
			propertyChanged("_currentcolumn", _currentcolumn);
			for(item in cast(("" + item).split(""), Array<Dynamic>))
			{
				if((item == "x"))
				{
					createRecycledActor(getActorType(22), (actor.getX() + (_currentcolumn * 16)), (actor.getY() + (_currentrow * 16)), Script.FRONT);
					getLastCreatedActor().setActorValue("parent", actor);
					_childmarkers.push(getLastCreatedActor());
					getLastCreatedActor().disableActorDrawing();
					_currentcolumn += 1;
					propertyChanged("_currentcolumn", _currentcolumn);
				}
			}
			_currentrow += 1;
			propertyChanged("_currentrow", _currentrow);
		}
	}
	
	/* ========================= Custom Event ========================= */
	public function _customEvent_destroyMarkers():Void
	{
		for(item in cast(_childmarkers, Array<Dynamic>))
		{
			recycleActor(item);
		}
		Utils.clear(_childmarkers);
	}
	
	
	public function new(dummy:Int, actor:Actor, dummy2:Engine)
	{
		super(actor);
		nameMap.set("Actor", "actor");
		nameMap.set("collision", "_collision");
		nameMap.set("current row", "_currentrow");
		_currentrow = 0.0;
		nameMap.set("current column", "_currentcolumn");
		_currentcolumn = 0.0;
		nameMap.set("child markers", "_childmarkers");
		
	}
	
	override public function init()
	{
		
	}
	
	override public function forwardMessage(msg:String)
	{
		
	}
}