package com.terry.engine {
	import flash.display.*;
	import flash.geom.*;
  import flash.events.*;
  import flash.net.*;
	import gamecontrol.control;
	//import flash.filesystem.*;
		
	public class game {		
  	include "../../../config/scriptcommands.as";
		
		public static function init():void {			
			infocus = true; paused = false; muted = false; globalsound = 1;
			gamestate = def.PACKINGMODE; completestop = false;
	    hascontrol = true; jumpheld = false; jumppressed = 0;
			roomname = ""; roomnamemode = 0;
			pausescript = false; speaker = "";
			parsetext = false;
			
			deathseq = 0;
			
			test = false; teststring = "TEST = True";
			
			//Init scripting
			var i:int;
			for (i = 0; i < 500; i++) {
				commands.push(new String);
			}
			for (i = 0; i < 100; i++) {
				words.push(new String);
				txt.push(new String);
			}
			position = 0; scriptlength = 0; scriptdelay = 0;
			running = false;
		}
		
		public static function justpressed_up():Boolean {
			if (key.justPressed("UP") || key.justPressed("W")) return true;
			return false;
		}
		
		public static function justpressed_down():Boolean {
			if (key.justPressed("DOWN") || key.justPressed("S")) return true;
			return false;
		}
		
		public static function justpressed_left():Boolean {
			if (key.justPressed("LEFT") || key.justPressed("A")) return true;
			return false;
		}
		
		public static function justpressed_right():Boolean {
			if (key.justPressed("RIGHT") || key.justPressed("D")) return true;
			return false;
		}
		
		public static function justpressed_action():Boolean {
			if (key.justPressed("Z") || key.justPressed("SPACE")) return true;
			return false;
		}
		
		public static function justpressed_map():Boolean {
			if (key.justPressed("ENTER") || key.justPressed("X")) return true;
			return false;
		}
		
		public static function keypoll():void {
			press_up = false; press_down = false; 
			press_left = false; press_right = false; press_action = false; press_map = false;
			
			if (key.pressed("LEFT") || key.pressed("A") ||
					key.pressed("RIGHT") || key.pressed("D") ||
					key.pressed("UP") || key.pressed("W") ||
					key.pressed("DOWN") || key.pressed("S") ||
					key.pressed("Z") || key.pressed("SPACE") ||
					key.pressed("ENTER") || key.pressed("X")) control.showcontrols = 120; 
			
			if (mouse.leftpressed()) press_action = true;
			
			if ((key.pressed("ALT") && key.pressed("ENTER")) && !fullscreentoggleheld) {
				//Toggle fullscreen
				fullscreentoggleheld = true;
				if (fullscreen) {fullscreen = false;
				}else {fullscreen = true;}
				updategraphicsmode();
					
				//savestats(map, dwgfx);
			}
			
			if (keyheld) {
				if (press_action || press_right || press_left || press_map ||
						press_down || press_up) {
					press_action = false;
					press_map = false;
					press_up = false;
					press_down = false;
					press_left = false;
					press_right = false;
				}else {
					keyheld = false;
				}
			}
		}
		
		public static function setzoom(t:int):void {
			gfx.screen.width = gfx.screenwidth * t;
			gfx.screen.height = gfx.screenheight * t;
			gfx.screen.x = (gfx.screenviewwidth - (gfx.screenwidth * t)) / 2;
			gfx.screen.y = (gfx.screenviewheight - (gfx.screenheight * t)) / 2;
		}
		
		public static function updategraphicsmode():void {
		 	if (fullscreen) {
				//trace("changing to fullscreen");
			  //gfx.context.configureBackBuffer(game.fullscreenwidth, game.fullscreenheight, 0, true);
				//stage.fullScreenSourceRect = new Rectangle(0, 0, gfx.screenwidth, gfx.screenheight);
				//Stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
				//Mouse.hide();
			}else {
				//trace("changing to window");
				//gfx.context.configureBackBuffer(gfx.screenwidth, gfx.screenheight, 0, true);
				//Stage.displayState = StageDisplayState.NORMAL;
			  //Mouse.show();
			}
		}
		
		public static function loadfile(t:String):void {
			/*
			file = new File(File.applicationDirectory.nativePath).resolvePath("./../src/scripts/" + t + ".txt");
			
			if(file.exists){
				stream = new FileStream();
				stream.open(file, FileMode.READ);
				filestring = stream.readUTFBytes(stream.bytesAvailable);
				stream.close();
			}else {
				trace("Error: File not found or cannot access.");
			}
			*/
		}
		
		public static function add(t:String):void {
			commands[scriptlength] = t;
			scriptlength++;
		}
		
		public static function loadexternalscript(t:String):void {
			/*
			loadfile(t);
			
			tempword = "";
			
			for (var i:int = 0; i < filestring.length; i++) {
				currentletter = filestring.substr(i, 1);
				if (currentletter == "\n") {
					add(tempword);
					tempword = "";
				}else {
					tempword += currentletter;
				}
			}
			
			if (tempword != "") add(tempword);
			*/
		}
		
		public static function tokenize(t:String):void {
			var i:int;
			numwords = 0; tempword = "";
			
			if (parsetext) {
				words[0] = t;
			}
			
			if (!parsetext) {
				for (i = 0; i < t.length; i++) {
					currentletter = t.substr(i, 1);
					if (currentletter == "(" || currentletter == ")" || currentletter == ",") {
						words[numwords] = tempword;
						//If it's an instruction to talk, then we do something different
						if (numwords == 0 && words[0] == "say") {
							i = t.length;
							words[1] = help.getbrackets(t);
						}else if (numwords == 0 && words[0] == "talk") {
							i = t.length;
							words[1] = help.getbrackets(t);
						}else if (numwords == 0 && words[0] == "speaker") {
							i = t.length;
							words[1] = help.getbrackets(t);
						}else if (numwords == 0 && words[0] == "call") {
							i = t.length;
							words[1] = help.getbrackets(t);
						}else {
							words[numwords] = words[numwords].toLowerCase();
						}
						numwords++; tempword = "";
					}else if (currentletter == " ") {
						//don't do anything - i.e. strip out spaces.
					}else {
						tempword += currentletter;
					}
				}
				
				if (tempword != "") {
					words[numwords] = tempword;
					numwords++;
				}
			}
			
			if (words[0].charAt(0) == "}") {
				words[0] = "endsay";
				parsetext = false;
			}
		}
		
		public static function releasekeys():void {
			keyheld = true;
			
			press_action = false;
			press_map = false;
			
			press_up = false;
			press_down = false;
			press_left = false;
			press_right = false;
		}
		
    public static var fullscreen:Boolean;
		public static var fullscreentoggleheld:Boolean = false;
		
		public static var press_up:Boolean, press_down:Boolean, press_left:Boolean;
		public static var press_right:Boolean, press_action:Boolean, press_map:Boolean;
		
		public static var keypriority:int = 0;
		public static var haspriority:Boolean = false;
		public static var keyheld:Boolean;
		public static var keydelay:int;
		
		public static var gamestate:int, pausescript:Boolean;
    public static var hascontrol:Boolean, jumpheld:Boolean, jumppressed:int;
		public static var roomname:String, roomnamemode:int;
		
		public static var mx:int, my:int;
		public static var test:Boolean, teststring:String;
		
		public static var infocus:Boolean, paused:Boolean;
		public static var muted:Boolean; 
		public static var mutebutton:int;
		public static var globalsound:int;
		public static var deathseq:int, completestop:Boolean;
		
		//File Access
		//public static var file:File, stream:FileStream, filestring:String;
		//Script contents
		public static var commands:Vector.<String> = new Vector.<String>;
		public static var words:Vector.<String> = new Vector.<String>;
		public static var txt:Vector.<String> = new Vector.<String>;
		public static var scriptname:String;
		public static var position:int, scriptlength:int;
		public static var looppoint:int, loopcount:int;
		
		public static var scriptdelay:int, running:Boolean;
		public static var tempword:String, currentletter:String, numwords:int;
		public static var parsetext:Boolean;
		public static var speaker:String;
	}
}