context: using System;
  
  using com.robotacid.engine;
  using com.robotacid.gfx;
  using flash.display;
  using flash.events;
  using flash.system;
  using Microsoft.Xna.Framework.Graphics;
  using Microsoft.Xna.Framework.Storage;
  using pumpkin;
  using Renderer = com.robotacid.gfx.Renderer;
  using FX = com.robotacid.gfx.FX;
  //using PNGEncoder = com.robotacid.gfx.PNGEncoder;
  using SoundManager = com.robotacid.sound.SoundManager;
  using SoundQueue = com.robotacid.sound.SoundQueue;
  using com.robotacid.ui.editor;
  //using FileManager = com.robotacid.ui.FileManager;
  //using ProgressBar = com.robotacid.ui.ProgressBar;
  using TextBox = com.robotacid.ui.TextBox;
  //using Key = com.robotacid.ui.Key;
  using TitleMenu = com.robotacid.ui.TitleMenu;
  using Transition = com.robotacid.ui.Transition;
  using UIManager = com.robotacid.ui.UIManager;
  using XorRandom = com.robotacid.util.XorRandom;
  
  //import flash.display.Bitmap;
  //import flash.display.BitmapData;
  //import flash.display.Graphics;
  //import flash.display.MovieClip;
  //import flash.display.Shape;
  using Sprite = flash.display.Sprite;
  //import flash.display.StageQuality;
  //import flash.events.Event;
  //import flash.events.KeyboardEvent;
  //import flash.events.MouseEvent;
  //import flash.events.TouchEvent;
  //import flash.external.ExternalInterface;
  //import flash.geom.ColorTransform;
  //import flash.geom.Point;
  //import flash.geom.Rectangle;
  //import flash.media.Sound;
  //import flash.net.SharedObject;
  //import flash.system.Capabilities;
  //import flash.text.TextField;
  //import flash.ui.Keyboard;
  //import flash.ui.Mouse;
  //import flash.utils.getTimer;
  
  using flash;
  using Microsoft.Xna.Framework;
  using Array = flash.Array;
  using Math = System.Math;
  
  /// <summary>
  /// Ending
  /// 
  /// A roguelike puzzle game
  /// 
  /// This is a god-object singleton. 
  /// 
  /// @author Aaron Steed, robotacid.com
  /// @conversion Paul Cunningham, pumpkin-games.net
  /// </summary>
  public class Game : Sprite
  {
      //render.main() is a NOP now so override here to do the main rendering
      protected internal override void OnDraw(RenderTarget2D sceneRenderTarget, GameTime gameTime) {
          renderer.main(sceneRenderTarget);           
      }
  
      public const Boolean TEST_BED_INIT = false;
      #if WINDOWS_PHONE
  	public static Boolean MOBILE = true;
      #else
      public static Boolean MOBILE = false;
      #endif
  	public static Game game;
  	public static Renderer renderer;
  		
  	// core engine objects
  	public TitleMenu titleMenu;
  	public Level level;
  	public RoomPainter roomPainter;
  	public SoundQueue soundQueue;
  	public Transition transition;
  
      // ui
  	public Sprite focusPrompt;
  
      // states
  	public int state;
  	public int focusPreviousState;
  	public int frameCount;
  	public Boolean fireButtonPressed;
  	public int fireButtonPressedCount;
  	public int keyPressedCount;
  	public Boolean mousePressed;
  	public int mousePressedCount;
  	public int mouseReleasedCount;
  	public double mouseVx;
  	public double mouseVy;
  	public int mouseSwipeSent;
  	public int mouseSwipeCount;
  	public int mouseSwipeDelay;
  	private double lastMouseX;
  	private double lastMouseY;
  	private double mouseDownX;
  	private double mouseDownY;
  	public Boolean paused;
  	public int shakeDirX;
  	public int shakeDirY;
  	public Boolean forceFocus = false;
  	public int mouseCorner;
  	public Boolean regainedFocus = false;
  	public int currentLevel;
  	public int currentLevelType;
  	public Library.LevelData currentLevelObj;
  	public Boolean editing;
  	public Boolean modifiedLevel;
  	public double scaleRatio;
  	public double defaultStageRatio;
  	public double stageRatio;
  	public double realStageWidth;
  	public double realStageHeight;
  	public int hideMouseFrames;
  		
  	public const int MOUSE_SWIPE_DELAY_DEFAULT = 4;
  		
  	// temp variables
  	private int i;
  	public static Point point = new Point();
  		
  	// CONSTANTS
  		
  	public readonly static double SCALE = 8;
  	public readonly static double INV_SCALE = 1.0 / 8;
  		
  	public readonly static double DEFAULT_SCREEN_RATIO = 480 / 320;
  	public static double SCREEN_RATIO;
  		
  	// states
  	public const int GAME = 0;
  	public const int MENU = 1;
  	public const int DIALOG = 2;
  	public const int SEGUE = 3;
  	public const int UNFOCUSED = 4;
  		
  	public static double WIDTH = 120;
  	public static double HEIGHT = 80;
  		
  	// game key properties
  	public const int UP_KEY = 0;
  	public const int DOWN_KEY = 1;
  	public const int LEFT_KEY = 2;
  	public const int RIGHT_KEY = 3;
  		
  	public const int MAX_LEVEL = 20;
  		
  	public const int TURN_FRAMES = 2;
  	public const int HIDE_MOUSE_FRAMES = 120;
  	public const int DEATH_FADE_COUNT = 20; //10 for 30 fps, 20 for 60 
  		
  	public static Boolean fullscreenToggled;
  	public static Boolean allowScriptAccess;
  		
  	public const int SOUND_DIST_MAX = 8;
  	public readonly static double INV_SOUND_DIST_MAX = 1.0 / SOUND_DIST_MAX;
  
      public Game() {
  			
  		visible = false;
  			
  		game = this;
  		UserData.game = this;
  		FX.game = this;
  		Level.game = this;
  		TitleMenu.game = this;
  		RoomPainter.game = this;
  		RoomPalette.game = this;
  			
          //CONVERSION - nothing really to do here
  		// detect allowScriptAccess for tracking
          //allowScriptAccess = ExternalInterface.available;
          //if(allowScriptAccess){
          //    try{
          //        ExternalInterface.call("");
          //    } catch( Exception e){
          //        allowScriptAccess = false;
          //    }
          //}
  			
  		Library.initLevels();
  			
  		// init UserData
  		UserData.initSettings();
  		UserData.initGameState();
  
          //CONVERSION - too early for this - need to load from StorageManager
          //UserData.pull();
          //// check the game is alive
          //if(UserData.gameState.dead) UserData.initGameState();
  			
  		state = TEST_BED_INIT ? GAME : MENU;
  			
  		// init sound
  		SoundManager.init();
  		soundQueue = new SoundQueue();
  			
  
gt: 		if (stage != null) addedToStage();