//Base game function, more precisesly combat stuff.  Should have some more crap around here eventually including constructors
//Nathan Tsang 02/03/11
//TODO: STOP LISTENING TO CCS OP1 LALALALA (also, um, make more object files.  Objects are fun.  Oh, and make a launcher .as)
//Suggestions: Maybe make a list of dummy MovieClip objects for "check points" and crap?  Just a suggestion.  Treat everyone as a node, elimates most
//cases where a back button is needed.
package
{
	import flash.display.MovieClip;
	import flash.geom.ColorTransform;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import Flags.Flags;
	import com.greensock.*;
	import Math;
	import flash.geom.ColorTransform;

	public class Game extends MovieClip
	{
		private var tField:TextField = new TextField();;
		private static var x:int;
		private var time:int;
		private var btime:int;
		private var ctime:int;
		private var aquare:MovieClip;
		private var bquare:MovieClip;
		private var flags:Flags;
		private var aspeed:int;
		private var bspeed:int;
		private var selected:int;
		private var objArr:Array;
		private var nodArr:Array;
		private var tempSel:int;

		public function Game ()
		{
			//Array of objects
			//TODO: Create a function to dynamically load objects, also, is there a better way to do mousemove?
			objArr = new Array();
			nodArr = new Array();
			ctime = 0;
			stage.addEventListener(MouseEvent.CLICK, onMouseClick);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			myTest();
		}
		private function myTest():void
		{
			//Stuff.
			//TODO: Dynamically load stuff, also, likely shove half of this crap into individual objects
			nodArr = new Array();
			for (var i:int = 0; i < 11; i++) {
				var m:MovieClip = new MovieClip();
				nodArr.push(m);
			}
			flags = new Flags;
			aquare = new MovieClip();
			bquare = new MovieClip();
			aspeed = 50;
			bspeed = 40;
			time = 5;
			aquare.graphics.beginFill(0x0000FF);
			aquare.graphics.drawRect(0, 0, 50, 50);
			aquare.graphics.endFill();
			addChild(aquare);
			bquare.graphics.beginFill(0xFF00FF);
			bquare.graphics.drawRect(0, 0, 50, 50);
			bquare.graphics.endFill();
			bquare.x = 100;
			bquare.y = 0;
			addChild(bquare);
			tField.autoSize = "left";
			tField.background = true;
			tField.border = true;
			tField.x = 20;
			tField.y = 75;
			tField.text = "Some random information. \nSome more text";
			addChild(tField);
			objArr.push(aquare);
			objArr.push(bquare);
		}

		//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

			tField.text = "aQuare x " + String(objArr[0].x) +"\naQuare y " + String(objArr[0].y) +
			"\nbQuare x " + String(objArr[1].x) +"\nbQuare y " + String(objArr[1].y) +
			"\nMouse x " + String(mouseX) + "\nMouse y " +String(mouseY);
			if (flags.getFlag(0)) {
				tField.text = "Nothing selected";
				trace("test");
				for ( var i:int = 0; i < objArr.length; i++) {
					if (mouseX >= objArr[i].x  && mouseX < objArr[i].x + 50 && mouseY > objArr[i].y && mouseY < objArr[i].y + 50) {
						flags.setFlag(0, false);
						flags.setFlag(1, true);
						selected = i;
						if (selected == 1) {
							tempSel = 0;
						} else {
							tempSel = 1;
						}
						tField.text ="WHAT";
						removeChild(objArr[i]);
						addChild(objArr[i]);
						addChild(nodArr[ctime]);
						nodArr[ctime].graphics.lineStyle(1, 0x000000, 1);
						nodArr[ctime].graphics.beginFill(0x818181, .25);
						nodArr[ctime].graphics.drawEllipse(objArr[selected].x + 25 - (aspeed * 5), objArr[selected].y + 25 - (aspeed * 5), aspeed * 10, aspeed * 10);
												nodArr[ctime].graphics.beginFill(0x818181, .25);
						nodArr[ctime].graphics.drawEllipse(objArr[selected].x + 25 - (aspeed * 4), objArr[selected].y + 25 - (aspeed * 4), aspeed * 8, aspeed * 8);
						nodArr[ctime].graphics.drawEllipse(objArr[selected].x + 25 - (aspeed * 3), objArr[selected].y + 25 - (aspeed * 3), aspeed * 6, aspeed * 6);
						nodArr[ctime].graphics.drawEllipse(objArr[selected].x + 25 - (aspeed * 2), objArr[selected].y + 25 - (aspeed * 2), aspeed * 4, aspeed * 4);
						nodArr[ctime].graphics.drawEllipse(objArr[selected].x + 25 - aspeed, objArr[selected].y + 25 - aspeed, aspeed * 2, aspeed * 2);
						break;
					}
				}
			} else if (flags.getFlag(1)) {
				if (Math.sqrt(Math.pow(objArr[selected].x + 25 - mouseX, 2) + (Math.pow(objArr[selected].y + 25 - mouseY, 2))) <= aspeed * 4) {
/*					objArr[selected].x = mouseX - 25;
					objArr[selected].y = mouseY - 25;*/
					var tempX:int = mouseX - 25;
					var tempY:int = mouseY - 25;
					TweenLite.to(objArr[selected], 1, {x:tempX, y:tempY, onUpdate:attackTest(objArr[selected])});
					removeChild(nodArr[ctime]);
					flags.setFlag(0, true);
					flags.setFlag(1, false);
					nodArr[ctime].graphics.clear();
				} else {
					removeChild(nodArr[ctime]);
					flags.setFlag(0, true);
					flags.setFlag(1, false);
					nodArr[ctime].graphics.clear();					
				}
				
			}
		}
		
		public function attackTest(obj:Object):void {
			trace("TEST");
			if (obj.hitTestObject(objArr[tempSel])) {
				removeChild(objArr[tempSel]);
			}
		}
		//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 {
			if (flags.getFlag(1)) {

				/*				nodArr[ctime].graphics.clear();
				nodArr[ctime].graphics.lineStyle(1, 0x0000FF, 1);
				nodArr[ctime].x = objArr[selected].x + 25;
				nodArr[ctime].y = objArr[selected].y + 25;
				nodArr[ctime].graphics.lineTo(mouseX - (objArr[selected].x + 25), mouseY - (objArr[selected].y + 25));*/
			}

		}
		
//		public function 
	}

}

