context: /*	
   * Copyright (C) 2015  Steffen Lim and Nicolas Villanueva
   * 
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published 
   * the Free Software Foundation, either version 3 of the License, 
   * (at your option) any later 
   * 
   * This program is distributed in the hope that it will be 
   * but WITHOUT ANY WARRANTY; without even the implied warranty 
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See 
   * GNU General Public License for more 
   * 
   * You should have received a copy of the GNU General Public 
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  using System;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.IO;
  using Sce.PlayStation.Core;
  using Sce.PlayStation.Core.Environment;
  using Sce.PlayStation.Core.Graphics;
  using Sce.PlayStation.Core.Input;
  using Sce.PlayStation.HighLevel.UI;
  
  namespace SteamPunkWasteLand
  {
  	public class AppMain
  	{
  		
  		public static void Main (string[] args)
  		{
  			Stopwatch s = new Stopwatch ();
  			s.Start ();
  			Initialize ();
  
  			while (Game.Running) {
  				SystemEvents.CheckEvents ();
  				float time = s.ElapsedMilliseconds / 1000f;
  				s.Reset ();
  				s.Start ();
  				Update (time * Game.TimeSpeed);
  				Render ();
  				Console.WriteLine("FPS: "+(int)(1/time));
  			}
  		}
  		
  		#region Main Methods
  		public static void Initialize ()
  		{
  			Game.Graphics = new GraphicsContext ();
  			UISystem.Initialize (Game.Graphics);
  			Game.Running = true;
  			Game.Rand = new Random ();
  			Game.GameState = States.MainMenu;
  			Game.Music = 1f;
  			Game.Sound = 1f;
  			Game.MusicBox = new MusicBox();
  			Game.Textures = new List<Texture2D> ();
  			Game.HighScores = new List<HighScore> ();
  			InitTextures ();
  			InitHighScores ();
  			
  			NewMenu ();
  		}
  
  		public static void Update (float time)
  		{
  			var gamePadData = GamePad.GetData (0);
  			
  			Game.MusicBox.Update();
  			switch (Game.GameState) {
  			case States.MainMenu:
  				MenuUpdate (gamePadData);
  				break;
  			case States.Play:
  				InGameUpdate (time, gamePadData);
  				break;
  			case States.HighScore:
  				HighScoreUpdate (gamePadData);
  				break;
  			case States.Name:
  				NamingUpdate (gamePadData);
  				break;
  			case States.Instruction:
  				InstructionUpdate (gamePadData);
  				break;
  			default:
  				break;
  			}
  			
  		}
  		
  		public static void Render ()
  		{
  			Game.Graphics.SetClearColor (0.0f, 0.0f, 0.0f, 0.0f);
  			Game.Graphics.Clear ();
  			
  			switch (Game.GameState) {
  			case States.MainMenu:
  				MenuRender ();
  				break;
  			case States.Play:
  				InGameRender ();
  				break;
  			case States.HighScore:
  				HighScoreRender ();
  				break;
  			case States.Name:
  				NamingRender ();
  				break;
  			case States.Instruction:
  				InstructionRender();
  				break;
  			default:
  				break;
  			}
  			
  			Game.Graphics.SwapBuffers ();
  		}
  		#endregion
  		
  		#region Instanciate States
  		public static void NewMenu ()
  		{
  			Game.BgMenu = new Background (Game.Textures [0]);
  			
  			float X = Game.Graphics.Screen.Width / 2f;
  			float Y = Game.Graphics.Screen.Height / 2f;
  			
  			Game.Title = new Sprite(Game.Graphics,Game.Textures[36]);
  			Game.Title.Center = new Vector2(0.5f,0);
  			Game.Title.Position = new Vector3(X,20,0);
  			
  			Game.ToInstructions = new Text(0,2*Y,400,50,-1,1,"Press 'X' to see instructions");
  			
  			Game.MenuButtons = new ButtonSet (Game.Textures [22], new Vector2 (200, 75));
  			Game.MenuButtons.AddButton (new Vector3 (X, Y, 0));				//play
  			Game.MenuButtons.AddButton (new Vector3 (X, Y + 100, 0));		//high score
  			Game.MenuButtons.AddButton (new Vector3 (X, Y + 200, 0));		//quit
  		}
  		
  		public static void NewGame ()
  		{
  			Game.TimeSpeed = 1f;
  			Game.Level = 0;
  			Game.Score = 0;
  			Game.Money = 0;
  			
  			Game.BgSky = new Background (Game.Textures [0]);
  			Game.BgGround = new BackgroundGround (Game.Textures [1]);
  			Game.BgCloud = new BackgroundClouds (Game.Textures [2]);
  			
  			Game.Upgrades = new float[3, 5];
  			for (int i = 0; i < 5; i++) {
  				Game.Upgrades [0, i] = 0;
  			}
  			Game.Upgrades [1, 0] = 20;
  			Game.Upgrades [1, 1] = 1;
  			Game.Upgrades [1, 2] = 0.5f;
  			Game.Upgrades [1, 3] = 0.1f;
  			Game.Upgrades [1, 4] = 50;
  			
  			Game.Upgrades [2, 0] = 300;
  			Game.Upgrades [2, 1] = 1000;
  			Game.Upgrades [2, 2] = 500;
  			Game.Upgrades [2, 3] = 500;
  			Game.Upgrades [2, 4] = 500;
  			
  			Game.PBullets = new List<Bullet> ();
  			Game.ObtainedWeapons = new List<Weapon> ();
  			Game.Enemies = new List<Enemy> ();
  			Game.EBullets = new List<Bullet> ();
  			Game.Loots = new List<Loot> ();
  			Game.AnimatedMoney = new List<Coins> ();
  			
  			Game.Pause = new Pause ();
  			
  			NextLevel ();
  		}
  
  		public static void NewHighScore ()
  		{
  			Game.BgMenu = new Background (Game.Textures [0]);
  			Game.HSD = new HighScoresDisplay ();
  			
  			Game.ToInstructions = new Text(0,Game.Graphics.Screen.Height,400,50,-1,1,"Press 'X' to go back to Main Menu");
  		}
  		
  		public static void NewName ()
  		{
  			Game.BgMenu = new Background (Game.Textures [0]);
  			Game.NameDisplay = new EnterNameDisplay ();
  		}
  		
  		public static void NewInstruction ()
  		{
  			Game.BgMenu = new Background(Game.Textures[0]);
  			Game.InstructionSprite = new Sprite(Game.Graphics,Game.Textures[40]);
  			Game.InstructionSprite.Center = new Vector2(0.5f,0);
  			Game.InstructionSprite.Position = new Vector3(Game.Graphics.Screen.Width/2f,20,0);
  			
  			Game.ToInstructions = new Text(0,Game.Graphics.Screen.Height,400,50,-1,1,"Press 'X' to go back to Main Menu");
  		}
  		#endregion
  		
  		#region Other Methods
  		public static void InitTextures ()
  		{
  			Game.Textures.Add (new Texture2D ("/Application/assets/Backgrounds/Sky1.png", false));				//0		Sky
  			Game.Textures.Add (new Texture2D ("/Application/assets/Backgrounds/Ground1.png", false));			//1		Ground
  			Game.Textures.Add (new Texture2D ("/Application/assets/Backgrounds/Cloud1.png", false));			//2		Clouds
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Player/Tophat_Sheet.png", false));			//3		Player
  			Game.Textures.Add (new Texture2D ("/Application/assets/Player/arm.png", false));					//4		Player arm
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Enemies/Zeppelin.png", false));				//5		Zeppelin
  			Game.Textures.Add (new Texture2D ("/Application/assets/Enemies/Dragon.png", false));				//6		Dragon
  			Game.Textures.Add (new Texture2D ("/Application/assets/Enemies/Guard.png", false));					//7		Imperial Guards
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Weapons/Cannon.png", false));				//8		Cannon
  			Game.Textures.Add (new Texture2D ("/Application/assets/Weapons/Flamethrower.png", false));			//9		Flamethrower
  			Game.Textures.Add (new Texture2D ("/Application/assets/Weapons/Crossbow.png", false));				//10	Crossbow
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Weapons/CannonBall.png", false));			//11	Cannon Ball
  			Game.Textures.Add (new Texture2D ("/Application/assets/Weapons/Flame.png", false));					//12	Flame Particle
  			Game.Textures.Add (new Texture2D ("/Application/assets/Weapons/Arrow.png", false));					//13	Arrows
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Weapons/explosion.png", false));				//14	Explosions
  			Game.Textures.Add (new Texture2D ("/Application/assets/Enemies/DragonHead.png", false));			//15	DragonHead
  			Game.Textures.Add (new Texture2D ("/Application/assets/Other/White.png", false));					//16	white pixel
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Other/BetterGear.png", false));				//17	HUD gear
  			Game.Textures.Add (new Texture2D ("/Application/assets/Other/hpTube.png", false));					//18	HP Tube
  			Game.Textures.Add (new Texture2D ("/Application/assets/Other/Coin.png", false));					//19	Coin
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Backgrounds/Sky2.png", false));				//20	Sky2
  			Game.Textures.Add (new Texture2D ("/Application/assets/Backgrounds/Cloud2.png", false));			//21	Cloud2
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Menu/MenuButtonsSheet.png", false));			//22	Menu Buttons
  			Game.Textures.Add (new Texture2D ("/Application/assets/Menu/ABC1.png", false));						//23	ABC Buttons 1
  			Game.Textures.Add (new Texture2D ("/Application/assets/Menu/ABC2.png", false));						//24	ABC Buttons 2
  			Game.Textures.Add (new Texture2D ("/Application/assets/Menu/EnterKeys.png", false));				//25	Extra Keys
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Enemies/AirShip.png", false));				//26	AirShip
  			Game.Textures.Add (new Texture2D ("/Application/assets/Enemies/E_CrossBow.png", false));			//27	E_CrossBow
  			Game.Textures.Add (new Texture2D ("/Application/assets/Enemies/E_Cannon.png", false));				//28	E_cannon
  			Game.Textures.Add (new Texture2D ("/Application/assets/Enemies/GuardArms.png", false));				//29	Guard Arms
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/PauseMenu/SettingsBackground.png", false));	//30	Pause settings backgound
  			Game.Textures.Add (new Texture2D ("/Application/assets/PauseMenu/ShopBackground.png", false));		//31	Pause shop backgound
  			Game.Textures.Add (new Texture2D ("/Application/assets/PauseMenu/PauseTab.png", false));			//32	Pause settings and shop tabs
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/PauseMenu/SettingsButtons.png", false));		//33	Resume / Exit
  			Game.Textures.Add (new Texture2D ("/Application/assets/PauseMenu/Slider.png", false));				//34	slider
  			Game.Textures.Add (new Texture2D ("/Application/assets/PauseMenu/Icons.png", false));				//35	Upgrade Icons
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Menu/Title.png", false));					//36	Title menu
  			Game.Textures.Add (new Texture2D ("/Application/assets/Menu/Highscores.png", false));				//37	Highscores text
  			Game.Textures.Add (new Texture2D ("/Application/assets/Menu/NewHishscorePromp.png", false));		//38	new highscores Text
  			Game.Textures.Add (new Texture2D ("/Application/assets/Menu/DeathMessage.png", false));				//39	Death Message
  			Game.Textures.Add (new Texture2D ("/Application/assets/Menu/Instructions.png", false));				//40	Instructions
  			
  			Game.Textures.Add (new Texture2D ("/Application/assets/Other/indicatorLeft.png", false));			//41	Indicator Left
  			Game.Textures.Add (new Texture2D ("/Application/assets/Other/indicatorRight.png", false));			//42	Indicator Right
  		}
  
  		public static void InitHighScores ()
  		{
  			StreamReader sr = null;
  			try {
  				sr = new StreamReader ("Documents/highscores.txt");
  				for (int i = 0; i < 10; i++) {
  					string name = "---";
  					string score = "--";
  					string[] line = sr.ReadLine ().Split (',');
  					if (line.Length == 2 && (line [0] != "" || line [1] != "")) {
  						name = line [0];
  						score = line [1];
  					}
  					Game.HighScores.Add (new HighScore (name, score));
  				}
  			} catch (FileNotFoundException) {
  				File.CreateText ("Documents/highscores.txt");
  
gt: 				StreamWriter sw = new StreamWriter ("Documents/highscores.txt");