//suggestion: Drunk programming, works much better
//Current version: Not going to let the user dynamically select stuff yet, which SHOULD be fine, force them to play with the current load out
//TODO:Get feedback (Done)
//TODO:WHY DO THE GUYS KEEP TELEPORTING?
//TODO: I think i fixed it lol
//Since catapults are the only unit to have a 'manned' command, need to think of some way to get that without setting a new bool
//Using a new bool at the moment (what?  It's only 2 bits anyway)

package
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.*;
	import Flags.Flags;
	import com.greensock.*;
	import Math;
	import flash.utils.Timer;
	import Units.Units;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author Nathan
	 */
	public class Main extends MovieClip
	{
		private static var x:int;
		private var time:int;
		private var flags:Flags;
		private var selected:int;
		private var attackTime:Timer;
		private var nodeSelected:int;
		private var showHitBox:Boolean;
		//List of objects and their respective hitboxes, remember to null things, or clear them.
		private var objArrRed:Array;
		private var objArrBlu:Array;
		private var startButton:Loader;
		private var paths:MovieClip;
		private var clearButton:Loader;
		//Variables for showing range
		private var rangeDisp:MovieClip;
		private var showRange:Boolean = false;
		//Test variables, remove in final
		private var tField:TextField = new TextField();
		private var tempSel:int;
		private var btime:int;
		private var rtime:int;
		private var ctime:int;
		private var mtime:int = 0;
		//This SHOULD be 1 unless something really weird happens
		private var delay:int = 1;
		//variable used to notify timer event what is the max amount of moves used
		private var maxTime:int;
		private var tweenTime:Number;
		private var backGround:Loader;
		private var tempDist:int;
		private var blackscreen:MovieClip;
		private var stats:TextField;
		private var statStr:String;
		private var statStr2:String;
		private var defStr:String;
		private var winTeam:String;
		private var dispWin:TextField;
		private var flagIndex:int;
		public function Main():void
		{
			paths = new MovieClip();
			dispWin = new TextField();
			rangeDisp = new MovieClip();
			objArrRed = new Array();
			objArrBlu = new Array();
			stats = new TextField();
			ctime = 0;
			time = 5;
			btime = 0;
			rtime = 0;
			mtime = 0;
			stage.addEventListener(MouseEvent.CLICK, onMouseClick, false, 0 ,true);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//Stuff.
			//TODO: Dynamically load stuff, also, likely shove half of this crap into individual objects
			flags = new Flags;
			backGround = new Loader();
			backGround.load(new URLRequest("BG1000x800.png"));
			startButton = new Loader();
			startButton.load(new URLRequest("done.png"));
			clearButton = new Loader();
			clearButton.load(new URLRequest("undo.png"));
			addChild(backGround);
			addChild(paths);
			tField.autoSize = "left";
			tField.background = true;
			tField.border = true;
			tField.x = 20;
			tField.y = 75;
			dispWin.autoSize = "left";
			dispWin.background = true;
			dispWin.border = false;
			dispWin.x = 450;
			dispWin.y = 350;
			stats.autoSize = "left";
			stats.background = true;
			stats.border = true;
			stats.x = 600;
			stats.y = 10;
			reset();
			startButton.x = 650;
			startButton.y = 10;
			clearButton.x = 650;
			clearButton.y = 80;
			startButton.addEventListener(MouseEvent.CLICK, startBattle);;
			clearButton.addEventListener(MouseEvent.CLICK, clear);
			addChild(startButton);
			addChild(clearButton);
			tField.text = String(ctime);
			//addChild(stats);

			blackscreen = new MovieClip;
			blackscreen.graphics.beginFill(0x000000);
			blackscreen.graphics.drawRect(0, 0, 1000, 800);
			blackscreen.graphics.endFill();

		}

		public function genStat():void {
			var tempString:String = "";
			var tempString2:String = "";
			for ( var i:int = 0; i < objArrRed.length; i++) {
				tempString = tempString + objArrRed[i].getName() + ": " + objArrRed[i].getHP() + "/100 \n";
			}
			for ( var j:int = 0; j < objArrBlu.length; j++) {
				tempString2 = tempString2 + objArrBlu[j].getName() + ": " + objArrBlu[j].getHP() + "/100 \n";
			}
			statStr = tempString;
			statStr2 = tempString2;
		}
		//Mouse Event stuff
		//TODO: Make a back button.  Also, making a go button at some point might be nice.
		//Suggestion: Change to add some keyboard functionality?  A pure mouse game is nice but this section is going to be huge.
		public function onMouseClick(event:MouseEvent):void {

			//Renable this if I ever need to test values, get rid of the tFields within the flag conditions though
			if (flags.getFlag(0)) {
				for ( var i:int = 0; i < objArrBlu.length; i++) {
					if (objArrBlu[i].displayImg().hitTestPoint(mouseX, mouseY, true) && (time - ctime > 0) && objArrBlu[i].isEmpty()){
						flags.setFlag(0, false);
						flags.setFlag(1, true);
						selected = i;
						addChild(rangeDisp);
						//draws the range of movement possible for the unit based on amount of time left.
						//add ellipse twice or else it will override.  How flash works is that if you draw the same shape twice
						//It will MINUS them from each other.  It's actually fairly interesting.
						rangeDisp.graphics.lineStyle(1, 0x000000, 1);
						rangeDisp.graphics.beginFill(0x818181, .25);
						rangeDisp.graphics.drawEllipse(objArrBlu[selected].x + 25 - (objArrBlu[selected].getMove() * (time - ctime)), objArrBlu[selected].y + 25 -
						(objArrBlu[selected].getMove() * (time - ctime)), objArrBlu[selected].getMove() * (time - ctime) * 2, objArrBlu[selected].getMove() *
						(time - ctime) * 2);
						for ( var j:int = time - ctime - 1; j >= 1; j--) {
							for (var k:int = 0; k < 2; k++){
								rangeDisp.graphics.drawEllipse(objArrBlu[selected].x + 25 - (objArrBlu[selected].getMove() * j), objArrBlu[selected].y + 25 -
								(objArrBlu[selected].getMove() * j), objArrBlu[selected].getMove() * j * 2, objArrBlu[selected].getMove() * j  * 2);}
							}
							break;

						} else if (!objArrBlu[i].isEmpty()) {
							if (objArrBlu[i].getNode(objArrBlu[i].nodSz() - 1).hitTestPoint(mouseX, mouseY, true) && (time - ctime > 0)) {
								flags.setFlag(0, false);
								flags.setFlag(2, true);
								nodeSelected = objArrBlu[i].nodSz() - 1;
								selected = i;
								addChild(rangeDisp);
								rangeDisp.graphics.lineStyle(1, 0x000000, 1);
								rangeDisp.graphics.beginFill(0x818181, .25);
								trace("----");
								rangeDisp.graphics.drawEllipse(objArrBlu[selected].getNX(nodeSelected) + 25 - (objArrBlu[selected].getMove() * (time - ctime)),
								objArrBlu[selected].getNY(nodeSelected) + 25 - (objArrBlu[selected].getMove() * (time - ctime)),
								objArrBlu[selected].getMove() * (time - ctime) * 2, objArrBlu[selected].getMove() * (time - ctime) * 2);
								for ( var m:int = time - ctime - 1; m >= 1; m--) {
									for (var n:int = 0; n < 2; n++){
										rangeDisp.graphics.drawEllipse(objArrBlu[selected].getNX(nodeSelected) + 25 - (objArrBlu[selected].getMove()
										* m), objArrBlu[selected].getNY(nodeSelected) + 25 -(objArrBlu[selected].getMove() * m), objArrBlu[selected].
										getMove() * m * 2, objArrBlu[selected].getMove() * m  * 2);}
									}
									break;
								}
							}
						}
						//State is: You have selected something
					} else if (flags.getFlag(1)) {
						//checker if the unit is able to move that distance, somehow tell the user that clicking outside of the range will cancel the
						//move action.
						if (Math.sqrt(Math.pow(objArrBlu[selected].x + 25 - mouseX, 2) + (Math.pow(objArrBlu[selected].y + 25 - mouseY, 2))) <=
						objArrBlu[selected].getMove() * time - ctime) {
							addChild(objArrBlu[selected].createNode(mouseX - 25, mouseY - 25));
							removeChild(rangeDisp);
							flags.setFlag(0, true);
							flags.setFlag(1, false);
							rangeDisp.graphics.clear();

							tempDist = (Math.ceil(Math.sqrt((Math.pow(objArrBlu[selected].x + 25 - mouseX, 2)) + (Math.pow(objArrBlu[selected].y + 25 - mouseY, 2))) /
							objArrBlu[selected].getMove()));
							ctime = ctime + tempDist;
							trace(Math.ceil(Math.sqrt(Math.pow(objArrBlu[selected].x + 25 - mouseX, 2) + (Math.pow(objArrBlu[selected].y + 25 - mouseY, 2)))) /
							objArrBlu[selected].getMove());
							trace(ctime);
							trace("got here");
							objArrBlu[selected].moveDist(tempDist);
							if (mtime == 0) {
								mtime = tempDist;
							} else if (mtime < tempDist) {
								mtime = tempDist;
							}
							paths.graphics.beginFill(0xFF00FF);
							paths.graphics.moveTo(mouseX, mouseY);
							paths.graphics.lineStyle(3, 0x000000);
							paths.graphics.lineTo(objArrBlu[selected].displayImg().x + 25, objArrBlu[selected].displayImg().y + 25);
							paths.graphics.endFill();
						} else {
							trace("got here instead");
							removeChild(rangeDisp);
							flags.setFlag(0, true);
							flags.setFlag(1, false);
							rangeDisp.graphics.clear();
						}
						//code for attacking, should trigger every 1 minute as long as stuff is still moving.  Will end when the LONGEST time event finishes.
					} else if (flags.getFlag(2)) {
						if (Math.sqrt(Math.pow(objArrBlu[selected].getNX(nodeSelected) + 25 - mouseX, 2) +
						(Math.pow(objArrBlu[selected].getNY(nodeSelected) + 25 - mouseY, 2))) <=
						objArrBlu[selected].getMove() * (time - ctime)) {
							addChild(objArrBlu[selected].createNode(mouseX - 25, mouseY -25));
							rangeDisp.graphics.clear();
							flags.setFlag(2, false);
							flags.setFlag(0, true);
							tempDist = (Math.ceil(Math.sqrt((Math.pow(objArrBlu[selected].getNX(nodeSelected) + 25 - mouseX, 2)) +
							(Math.pow(objArrBlu[selected].getNY(nodeSelected) + 25 - mouseY, 2))) / objArrBlu[selected].getMove()));
							ctime = ctime + tempDist;
							trace("something here");
							trace(ctime);
							objArrBlu[selected].moveDist(tempDist);
							if (objArrBlu[selected].distMoved() > mtime) {
								mtime = objArrBlu[selected].distMoved();
							}
							paths.graphics.beginFill(0xFF00FF);
							paths.graphics.moveTo(mouseX, mouseY);
							paths.graphics.lineStyle(3, 0x000000);
							paths.graphics.lineTo(objArrBlu[selected].getNX(nodeSelected) + 25, objArrBlu[selected].getNY(nodeSelected) + 25);
							paths.graphics.endFill();
						} else {
							trace("got here instead");
							removeChild(rangeDisp);
							flags.setFlag(0, true);
							flags.setFlag(2, false);
							rangeDisp.graphics.clear();
						}
					} else if (flags.getFlag(3)) {
						for ( var a:int = 0; a < objArrRed.length; a++) {
							if (objArrRed[a].displayImg().hitTestPoint(mouseX, mouseY, true) && (time - ctime > 0) && objArrRed[a].isEmpty()){
								flags.setFlag(3, false);
								flags.setFlag(4, true);
								selected = a;
								addChild(rangeDisp);
								//draws the range of movement possible for the unit based on amount of time left.
								//add ellipse twice or else it will override.  How flash works is that if you draw the same shape twice
								//It will MINUS them from each other.  It's actually fairly interesting.
								rangeDisp.graphics.lineStyle(1, 0x000000, 1);
								rangeDisp.graphics.beginFill(0x818181, .25);
								rangeDisp.graphics.drawEllipse(objArrRed[selected].x + 25 - (objArrRed[selected].getMove() * (time - ctime)), objArrRed[selected].y + 25 -
								(objArrRed[selected].getMove() * (time - ctime)), objArrRed[selected].getMove() * (time - ctime) * 2, objArrRed[selected].getMove() *
								(time - ctime) * 2);
								for ( var b:int = time - ctime - 1; b >= 1; b--) {
									for (var c:int = 0; c < 2; c++){
										rangeDisp.graphics.drawEllipse(objArrRed[selected].x + 25 - (objArrRed[selected].getMove() * b), objArrRed[selected].y + 25 -
										(objArrRed[selected].getMove() * b), objArrRed[selected].getMove() * b * 2, objArrRed[selected].getMove() * b  * 2);}
									}
									break;
								} else if (!objArrRed[a].isEmpty()) {
									if (objArrRed[a].getNode(objArrRed[a].nodSz() - 1).hitTestPoint(mouseX, mouseY, true) && (time - ctime > 0)) {
										flags.setFlag(3, false);
										flags.setFlag(5, true);
										nodeSelected = objArrRed[a].nodSz() - 1;
										selected = a;
										addChild(rangeDisp);
										rangeDisp.graphics.lineStyle(1, 0x000000, 1);
										rangeDisp.graphics.beginFill(0x818181, .25);
										trace("----");
										trace(objArrRed[selected].getNX(nodeSelected));
										trace(nodeSelected);
										rangeDisp.graphics.drawEllipse(objArrRed[selected].getNX(nodeSelected) + 25 - (objArrRed[selected].getMove() * (time - ctime)),
										objArrRed[selected].getNY(nodeSelected) + 25 - (objArrRed[selected].getMove() * (time - ctime)),
										objArrRed[selected].getMove() * (time - ctime) * 2, objArrRed[selected].getMove() * (time - ctime) * 2);
										for ( var d:int = time - ctime - 1; d >= 1; d--) {
											for (var e:int = 0; e < 2; e++){
												rangeDisp.graphics.drawEllipse(objArrRed[selected].getNX(nodeSelected) + 25 - (objArrRed[selected].getMove()
												* d), objArrRed[selected].getNY(nodeSelected) + 25 -(objArrRed[selected].getMove() * d), objArrRed[selected].
												getMove() * d * 2, objArrRed[selected].getMove() * d  * 2);}
											}
											break;
										}
									}
								}
								//State is: You have selected something
							} else if (flags.getFlag(4)) {
								//checker if the unit is able to move that distance, somehow tell the user that clicking outside of the range will cancel the
								//move action.
								if (Math.sqrt(Math.pow(objArrRed[selected].x + 25 - mouseX, 2) + (Math.pow(objArrRed[selected].y + 25 - mouseY, 2))) <=
								objArrRed[selected].getMove() * time - ctime) {
									addChild(objArrRed[selected].createNode(mouseX - 25, mouseY - 25));
									removeChild(rangeDisp);
									flags.setFlag(3, true);
									flags.setFlag(4, false);
									rangeDisp.graphics.clear();
									tempDist = (Math.ceil(Math.sqrt((Math.pow(objArrRed[selected].x + 25 - mouseX, 2)) + (Math.pow(objArrRed[selected].y + 25 - mouseY, 2))) /
									objArrRed[selected].getMove()));
									ctime = ctime + tempDist;
									trace(Math.ceil(Math.sqrt(Math.pow(objArrRed[selected].x + 25 - mouseX, 2) + (Math.pow(objArrRed[selected].y + 25 - mouseY, 2)))) /
									objArrRed[selected].getMove());
									objArrRed[selected].moveDist(tempDist);
									trace(ctime);
									trace("got here");
									if (mtime == 0) {
										mtime = tempDist;
									} else if (mtime < tempDist) {
										mtime = tempDist;
									}
									paths.graphics.beginFill(0xFF00FF);
									paths.graphics.moveTo(mouseX, mouseY);
									paths.graphics.lineStyle(3, 0x000000);
									paths.graphics.lineTo(objArrRed[selected].displayImg().x + 25, objArrRed[selected].displayImg().y + 25);
									paths.graphics.endFill();
								} else {
									trace("got here instead");
									removeChild(rangeDisp);
									flags.setFlag(3, true);
									flags.setFlag(4, false);
									rangeDisp.graphics.clear();
								}
								//code for attacking, should trigger every 1 minute as long as stuff is still moving.  Will end when the LONGEST time event finishes.
							} else if (flags.getFlag(5)) {
								if (Math.sqrt(Math.pow(objArrRed[selected].getNX(nodeSelected) + 25 - mouseX, 2) +
								(Math.pow(objArrRed[selected].getNY(nodeSelected) + 25 - mouseY, 2))) <=
								objArrRed[selected].getMove() * (time - ctime)) {
									addChild(objArrRed[selected].createNode(mouseX - 25, mouseY -25));
									trace(objArrRed[selected].nodSz());
									rangeDisp.graphics.clear();
									flags.setFlag(5, false);
									flags.setFlag(3, true);
									tempDist = (Math.ceil(Math.sqrt((Math.pow(objArrRed[selected].getNX(nodeSelected) + 25 - mouseX, 2)) +
									(Math.pow(objArrRed[selected].getNY(nodeSelected) + 25 - mouseY, 2))) / objArrRed[selected].getMove()));
									trace("something here");
									ctime = ctime + tempDist;
									trace(ctime);
									objArrRed[selected].moveDist(tempDist);
									if (mtime < objArrRed[selected].distMoved()) {
										mtime = objArrRed[selected].distMoved();
									}
									paths.graphics.beginFill(0xFF00FF);
									paths.graphics.moveTo(mouseX, mouseY);
									paths.graphics.lineStyle(3, 0x000000);
									paths.graphics.lineTo(objArrRed[selected].getNX(nodeSelected) + 25, objArrRed[selected].getNY(nodeSelected) + 25);
									paths.graphics.endFill();
								} else {
									trace("got here instead");
									removeChild(rangeDisp);
									flags.setFlag(3, true);
									flags.setFlag(5, false);
									rangeDisp.graphics.clear();
								}
							}
						}
						public function startBattle(event:MouseEvent):void {
							paths.graphics.clear();
							if (flags.getFlag(0)) {
								flags.setFlag(7, true);
								flags.setFlag(0, false);
								for (var ab:int = 0; ab < objArrBlu.length; ab++) {
									objArrBlu[ab].clearSq();
								}
								addChild(blackscreen);
							} else if (flags.getFlag(3)) {
								trace("TESTING SOMETHING HERE " + mtime);
								if (mtime == 0) {
									mtime = 1;
								}
								attackTime = new Timer(1000, mtime);
								attackTime.start();
								//attack();
								attackTime.addEventListener(TimerEvent.TIMER, attack);
								attackTime.addEventListener(TimerEvent.TIMER_COMPLETE, getResult);
								trace(String(objArrBlu[0].nodSz()));
								for (var hate:int = 0; hate < objArrBlu.length; hate++){
									objArrBlu[hate].clearSq();
									if (objArrBlu[hate].nodSz() > 0 && objArrBlu[hate].nodSz() < 2) {
										trace("SOEMTHING HERE");
										tweenTime = Math.sqrt(Math.pow(objArrBlu[hate].x - objArrBlu[hate].getNX(0), 2) +
										(Math.pow(objArrBlu[hate].y - objArrBlu[hate].getNY(0), 2))) / objArrBlu[hate].getMove();
										TweenLite.to(objArrBlu[hate], tweenTime, { x:objArrBlu[hate].getNX(0), y:objArrBlu[hate].getNY(0), onComplete:test } );
										TweenLite.to(objArrBlu[hate].displayImg(), tweenTime, { x:objArrBlu[hate].getNX(0), y:objArrBlu[hate].getNY(0) } );
										TweenLite.to(objArrBlu[hate].loadHitBox(), tweenTime, { x:objArrBlu[hate].getNX(0), y:objArrBlu[hate].getNY(0) } );
										TweenLite.to(objArrBlu[hate].getHPbar(), tweenTime, { x:objArrBlu[hate].getNX(0), y:objArrBlu[hate].getNY(0) } );
									} else if (objArrBlu[hate].nodSz() > 1) {
										tweenTime = Math.sqrt(Math.pow(objArrBlu[hate].x - objArrBlu[hate].getNX(0), 2) +
										(Math.pow(objArrBlu[hate].y - objArrBlu[hate].getNY(0), 2))) / objArrBlu[hate].getMove();
										TweenLite.to(objArrBlu[hate], tweenTime, { x:objArrBlu[hate].getNX(0), y:objArrBlu[hate].getNY(0), onComplete:continueTw, onCompleteParams:[objArrBlu[hate], 0]} );
										TweenLite.to(objArrBlu[hate].displayImg(), tweenTime, { x:objArrBlu[hate].getNX(0), y:objArrBlu[hate].getNY(0) } );
										TweenLite.to(objArrBlu[hate].loadHitBox(), tweenTime, { x:objArrBlu[hate].getNX(0), y:objArrBlu[hate].getNY(0) } );
										TweenLite.to(objArrBlu[hate].getHPbar(), tweenTime, { x:objArrBlu[hate].getNX(0), y:objArrBlu[hate].getNY(0) } );
									}
								}
								for (var p:int = 0; p < objArrRed.length; p++) {
									objArrRed[p].clearSq();
									if (objArrRed[p].nodSz() > 0 && objArrRed[p].nodSz() < 2) {
										tweenTime = Math.sqrt(Math.pow(objArrRed[p].x - objArrRed[p].getNX(0), 2) +
										(Math.pow(objArrRed[p].y - objArrRed[p].getNY(0), 2))) / objArrRed[p].getMove();
										TweenLite.to(objArrRed[p], tweenTime, { x:objArrRed[p].getNX(0), y:objArrRed[p].getNY(0), onComplete:test } );
										TweenLite.to(objArrRed[p].displayImg(), tweenTime, { x:objArrRed[p].getNX(0), y:objArrRed[p].getNY(0) } );
										TweenLite.to(objArrRed[p].loadHitBox(), tweenTime, { x:objArrRed[p].getNX(0), y:objArrRed[p].getNY(0) } );
										TweenLite.to(objArrRed[p].getHPbar(), tweenTime, { x:objArrRed[p].getNX(0), y:objArrRed[p].getNY(0) } );
									} else if (objArrRed[p].nodSz() > 1 ) {
										tweenTime = Math.sqrt(Math.pow(objArrRed[p].x - objArrRed[p].getNX(0), 2) +
										(Math.pow(objArrRed[p].y - objArrRed[p].getNY(0), 2))) / objArrRed[p].getMove();
										TweenLite.to(objArrRed[p], tweenTime, { x:objArrRed[p].getNX(0), y:objArrRed[p].getNY(0), onComplete:continueTw, onCompleteParams:[objArrRed[p], 0] } );
										TweenLite.to(objArrRed[p].displayImg(), tweenTime, { x:objArrRed[p].getNX(0), y:objArrRed[p].getNY(0) } );
										TweenLite.to(objArrRed[p].loadHitBox(), tweenTime, { x:objArrRed[p].getNX(0), y:objArrRed[p].getNY(0) } );
										TweenLite.to(objArrRed[p].getHPbar(), tweenTime, { x:objArrRed[p].getNX(0), y:objArrRed[p].getNY(0) } );}
									}
								}
							}
							//Note for self in case I forget due to tiredness
							//Pass the int value of the array rather than simply obj to prevent weird shit from happening
							public function continueTw(obj:Object, index:int):void {
								if (obj.nodeSize() < index + 3) {
									tweenTime = Math.sqrt(Math.pow(obj.getNX(index + 1) - obj.getNX(index), 2) +
									(Math.pow(obj.getNY(index + 1) - obj.getNY(index), 2))) / obj.getMove();
									TweenLite.to(obj, tweenTime, { x:obj.getNX(index + 1), y:obj.getNY(index + 1), onComplete:test } );
									TweenLite.to(obj.displayImg(), tweenTime, { x:obj.getNX(index + 1), y:obj.getNY(index + 1) } );
									TweenLite.to(obj.loadHitBox(), tweenTime, { x:obj.getNX(index + 1), y:obj.getNY(index + 1) } );
									TweenLite.to(obj.getHPbar(), tweenTime, { x:obj.getNX(index + 1), y:obj.getNY(index + 1)} );
									trace(tweenTime);
								} else {
									tweenTime = Math.sqrt(Math.pow(obj.getNX(index) - obj.getNX(index + 1), 2) +
									(Math.pow(obj.getNY(index) - obj.getNY(index + 1), 2))) / obj.getMove();
									TweenLite.to(obj, tweenTime, { x:obj.getNX(index + 1), y:obj.getNY(index + 1), onComplete:continueTw, onCompleteParams:[obj, index + 1] } );
									TweenLite.to(obj.displayImg(), tweenTime, { x:obj.getNX(index + 1), y:obj.getNY(index + 1) } );
									TweenLite.to(obj.loadHitBox(), tweenTime, { x:obj.getNX(index + 1), y:obj.getNY(index + 1) } );
									TweenLite.to(obj.getHPbar(), tweenTime, { x:obj.getNX(index + 1), y:obj.getNY(index + 1)} );
									trace(tweenTime);
								}
							}
							public function test():void {
								trace(objArrBlu[0].x);
								if (btime < 0) {
									btime = 0;
								}
								ctime = 0 - btime;
								for (var x:int = 0; x < objArrBlu.length; x++) {
									objArrBlu[x].clearNode();
								}
								for (var y:int = 0; y < objArrRed.length; y++) {
									objArrRed[y].clearNode();
								}
							}
							//I can theoretically put both attack checks in here... but I think it would start looking really weird (on the other hand
							//it works better since I can handle clashes that way.  On the other hand it starts getting really weird. I can just put
							//the checker at the end of the function?
							public function attack(e:TimerEvent = null):void {
								var tempDmg:Number = 0;
								trace("GOT HERE?");
								for (var i:int = 0; i < objArrBlu.length; i++) {
									for (var j:int = 0; j < objArrRed.length; j++) {
										if(objArrBlu[i].getName() != "FLAGBEAR"){
											if (objArrBlu[i].hitTest(objArrRed[j].x + 25, objArrRed[j].y + 25) && !objArrBlu[i].isDead()) {
												tempDmg = 0;
												if (objArrBlu[i].xtraDmg()) {
													tempDmg = tempDmg + 10;
												}
												tempDmg = tempDmg +  objArrBlu[i].getAtk();
												if (objArrBlu[i].isStrong(objArrRed[j].getName())) {
													btime++;
												} else if (objArrBlu[i].isWeak(objArrRed[j].getName())) {
													btime--;
												}
												if (objArrRed[j].beenAtkd()) {
													btime++;
												}
												objArrRed[j].takeDamage(tempDmg);
												trace("DAMAGE BLU: " + tempDmg);
											}} else {
												for (var bo:int = 0; bo < objArrBlu.length; bo++) {
													if ( bo != i && objArrBlu[i].hitTest(objArrBlu[bo].x + 25, objArrBlu[bo].y + 25)) {
														objArrBlu[bo].enableDmg();
													}
												}
											}
											if(objArrRed[j].getName() != "FLAGBEAR"){
												if (objArrRed[j].hitTest(objArrBlu[i].x + 25, objArrBlu[i].y + 25) && !objArrRed[j].isDead()) {
													tempDmg = 0;
													if (objArrRed[j].xtraDmg()) {
														tempDmg = tempDmg + 10;
													}
													tempDmg = tempDmg + objArrRed[j].getAtk();
													if (objArrRed[j].isStrong(objArrBlu[i].getName())) {
														rtime++;
													} else if (objArrRed[j].isWeak(objArrBlu[i].getName())) {
														rtime--;
													}
													if (objArrBlu[i].beenAtkd()) {
														rtime++;
													}
													trace("DAMAGE RED: " + tempDmg);
													objArrBlu[i].takeDamage(tempDmg);
												}
											} else {
												for (var po:int = 0; po < objArrRed.length; po++) {
													if ( po != j && objArrRed[j].hitTest(objArrRed[po].x + 25, objArrRed[po].y + 25)) {
														objArrRed[po].enableDmg();
														trace("TEST " + j + ": " + String(po) + ": GAVE BONUS TO: " + objArrRed[po].getName());
													}
												}
											}
										}
									}
									//checking for dead units
									for (var a:int = 0; a < objArrBlu.length; a++) {
										if (objArrBlu[a].isDead()) {
											objArrBlu[a].removeImg();
											objArrBlu[a].clearHitBox();
											objArrBlu[a].removeHpBar();
										}
									}
									for (var b:int = 0; b < objArrRed.length; b++) {
										if (objArrRed[b].isDead()) {
											objArrRed[b].removeImg();
											objArrRed[b].clearHitBox();
											objArrRed[b].removeHpBar();
										}
									}
								}
								public function getResult(e:TimerEvent):void {
									for (var i:int = 0; i < objArrBlu.length; i++) {
										if(objArrBlu[i].isDead()){
											objArrBlu.splice(i, 1);
											i--;
										}
									}
									for (var j:int = 0; j < objArrRed.length; j++) {
										if (objArrRed[j].isDead()) {
											objArrRed.splice(j, 1);
											j--;
										}
									}
									mtime = 0;
									if (objArrBlu.length == 0 && objArrRed.length == 0) {
										flags.setFlag(8, true);
										dispWin.text = "NEITHER WINS PRESS r TO RETRY";
										addChild(blackscreen);
										addChild(dispWin);
									} else if (objArrBlu.length == 0) {
										flags.setFlag(8, true);
										dispWin.text = "RED WINS PRESS r TO RETRY";
										addChild(blackscreen);
										addChild(dispWin);
									} else if (objArrRed.length == 0) {
										flags.setFlag(8, true);
										dispWin.text = "BLU WINS PRESS r TO RETRY";
										addChild(blackscreen);
										addChild(dispWin);
									} else {
										flags.setFlag(3, false);
										flags.setFlag(0, true);
										genStat();
										defStr = "Pink Button To Switch Turns/Go \n Press ENTER at the black screen \n RED units:  \n"
										stats.text = defStr + statStr + "BLU units: \n" + statStr2;
									}
								}
								//Should only be triggering when flag 1 is tripped.  If it is functioning outside of that, something is broke.
								public function onMouseMove(event:MouseEvent):void {
								}
								public function reset():void {
									clearAll();
									for (var i:int = 0; i < objArrBlu.length; i++) {
										objArrBlu.pop();
									}
									for (var j:int = 0; j < objArrRed.length; j++) {
										objArrRed.pop();
									}
									var red1:Units = new Units(true, "FLAGBEAR", "RED", "FLAGBEAR");
									var red2:Units = new Units(true, "ARCHER", "RED", "ARCHER");
									var red3:Units = new Units(true, "KNIGHT", "RED", "KNIGHT");
									var red4:Units = new Units(true, "ARCHER", "RED", "ARCHER");
									var red5:Units = new Units(true, "KNIGHT", "RED", "KNIGHT");
									red3.setLocale(50, 0);
									red2.setLocale(0, 50);
									red4.setLocale(100, 0);
									red5.setLocale(0, 100);
									var Blu1:Units = new Units(true, "FLAGBEAR", "BLU", "FLAGBEAR");
									var Blu2:Units = new Units(true, "ARCHER", "BLU", "ARCHER");
									var Blu3:Units = new Units(true, "KNIGHT", "BLU", "KNIGHT");
									var Blu4:Units = new Units(true, "ARCHER", "BLU", "ARCHER");
									var Blu5:Units = new Units(true, "KNIGHT", "BLU", "KNIGHT");
									Blu1.setLocale(750, 550);
									Blu2.setLocale(700, 550);
									Blu3.setLocale(750, 500);
									Blu4.setLocale(650, 550);
									Blu5.setLocale(750, 450);
									objArrBlu.push(Blu1, Blu2, Blu3, Blu4, Blu5);
									objArrRed.push(red1, red2, red3, red4, red5);
									for (var a:int = 0; a < objArrBlu.length; a++) {
										addChild(objArrBlu[a].loadHitBox());
									}
									for (var b:int = 0; b < objArrRed.length; b++) {
										addChild(objArrRed[b].loadHitBox());
									}
									for (var c:int = 0; c < objArrBlu.length; c++) {
										addChild(objArrBlu[c].displayImg());
										addChild(objArrBlu[c].getHPbar());
									}
									for (var d:int = 0; d < objArrRed.length; d++) {
										addChild(objArrRed[d].displayImg());
										addChild(objArrRed[d].getHPbar());
									}
									btime = 0;
									rtime = 0;
									genStat();
									defStr = "(BLU GOES FIRST) \nPink Button To Switch Turns/Go \nPress ENTER at the black screen \nRED units:  \n"
									stats.text = defStr + statStr + "BLU units: \n" + statStr2;
									ctime = 0;
									flags.setDefault();
								}
								public function onKeyDown(event:KeyboardEvent):void {
									if (event.keyCode == 13 && flags.getFlag(7)) {
										removeChild(blackscreen);
										flags.setFlag(7, false);
										flags.setFlag(3, true);
										if (rtime < 0) {
											rtime = 0;
										}
										ctime = 0 - rtime;
										trace(rtime);
									} else if (event.keyCode == 82 && flags.getFlag(8)) {
										removeChild(blackscreen);
										reset();
										genStat();
										removeChild(dispWin);
									}
								}
								public function clearAll():void {
									for (var i:int = 0; i < objArrBlu.length; i++) {
										objArrBlu[i].removeImg();
										objArrBlu[i].removeHitBox();
										objArrBlu[i].removeHpBar();
									}
									for (var j:int = 0; j < objArrRed.length; j++) {
										objArrRed[i].removeImg();
										objArrRed[i].removeHitBox();
										objArrRed[i].removeHpBar();
									}
								}
								public function clear(event:MouseEvent):void {
									paths.graphics.clear();
									if (flags.getFlag(0)) {
										ctime = 0 - btime;
										for (var i:int = 0; i < objArrBlu.length; i++) {
											objArrBlu[i].clearSq();
											objArrBlu[i].clearNode();
										}
									} else if (flags.getFlag(3)) {
										ctime = 0 - rtime;
										for (var j:int = 0; j < objArrRed.length; j++) {

											objArrRed[j].clearSq();
											objArrRed[j].clearNode();
										}
									}
								}
							}
						}

