
using System;
using System.Drawing;	

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

using Math2D;

using PlanetExpress.Tools;
using PlanetExpress.items.weapon;

namespace PlanetExpress.items  {
		
	public class PlayerClass : PersonClass {
		
		private TexClass Ship;
		private TexClass ShipE;
		private TexClass ShipFlame;		
		public PlayerClass() {
			CollisionClass = COLLISION_CLASS_PLAYER;
			Ship = TexMan.LoadTexture(GameApp.GameFileName("data", "items", "ship.png"));
			ShipE = TexMan.LoadTexture(GameApp.GameFileName("data", "items", "shipelement.png"));
			ShipFlame = TexMan.LoadTexture(GameApp.GameFileName("data", "items", "shipeflame.png"));
			LoadMap(GameApp.GameFileName("data", "items", "ship.map"));
			Weapon = new BaseLaser(this);	
			Weapon2 = new RoundLaser(this);
		}
		
		private Vector2D va = new Vector2D();
		float NewEnergy = 0;
		public override bool Action (double time) {
			base.Action(time);
			
			NewEnergy += 0.1f * (float)time;
			if (NewEnergy > 1) {
				if (Energy<5) Energy++;
				NewEnergy=0f;
			}
			
			X = TargetX;
			Y = TargetY;
			return HP > 0;
		}
		
		public float TargetX = 0;
		public float TargetY = GameApp.ScreenHeight/2;
		public int HP = 5;
		public int Energy = 5;
		
		float ShipAngle = 0;
		float ShipOffseX = 0;
		float ShipOffseY = 0;
		public override void Draw () {
			if (HP <= 0) return;
			double a = Environment.TickCount / 800f;			
			GL.PushMatrix(); {
				ShipOffseX = (float)Math.Cos(a)*4;
				ShipOffseY = (float)Math.Sin(a)*4;		
				GL.Translate(X + ShipOffseX,
				             Y + ShipOffseY,
				             0f);
				//
				ShipAngle = (float)Math.Cos(a)*2;
				GL.Rotate(ShipAngle, 0f, 0f, 1f);
				GL.Translate(-Ship.Width/2f, -Ship.Height/2f, 0f);
				
				GL.Color3(new float[]{0.4f, 0.4f, 0.4f});			
				#region заднее крыло				
				GL.PushMatrix(); {
					GL.Translate(50f, 45f, 0f);
					GL.Scale(1f, Math.Sin(a)*0.15f+0.8f, 1f);
					ShipE.Render(0, 0);
				}GL.PopMatrix();
				#endregion				
				
				GL.Color4(new float[]{1f, 1f, 1f, 1f});
				#region пламя
				GL.PushMatrix(); {
					GL.Translate(38-Math.Sin(a*16)*2, 34f, 0f);
					ShipFlame.Render(0, 0);				
				} GL.PopMatrix();
				GL.PushMatrix(); {
					GL.Translate(38-Math.Sin(a*16 + Math.PI)*2, 32f, 0f);
					ShipFlame.Render(0, 0);				
				} GL.PopMatrix();
				#endregion
				
				//Корабль
				GL.Color3(new float[]{1f, 1f, 1f});
				Ship.Render(0, 0);
				
				#region переднее крыло
				GL.PushMatrix(); {
					GL.Translate(50f, 45f, 0f);
					GL.Scale(1f, Math.Sin(a+Math.PI)*0.15f+0.8f, 1f);
					ShipE.Render(0, 0);				
				} GL.PopMatrix();
				#endregion
			}GL.PopMatrix();
			DrawMap();
		}
		
		protected override Matrix2D GetMapTransformation () {
			if (Ship==null)
				return base.GetMapTransformation();
			return 
				Matrix2D.GetTranslate(-Ship.Width/2, -Ship.Height/2) *
					Matrix2D.GetRotate(ShipAngle/180f * (float)Math.PI) *
					Matrix2D.GetTranslate(X + ShipOffseX, Y + ShipOffseY);
		}

		
		Random shootRnd = new Random();
		public override bool Shoot() {
			return Weapon.Shoot(X+30, Y-20, (float)(shootRnd.NextDouble()-0.5)*0.1f);
		}
		
		public override bool Shoot2 () {
			if (Energy <= 0) return false;
			if (Weapon2.Shoot(X, Y, (float)(shootRnd.NextDouble()-0.5)*0.1f)) {
				Energy--;
				return true;
			}
			return false;
		}
		
		public override void Demage (int v) {
			HP -= v;
			if (HP >= 0) {
				PlanetExpress.items.effects.HeardEffect.Show(
				                                             48f * (HP+1),
				                                             GameApp.ScreenHeight - 48 / 2);
			}
			if (HP == 0) {
				PlanetExpress.items.effects.BoomEffect.Show(X-20, Y-20);
				PlanetExpress.items.effects.BoomEffect.Show(X+20, Y-20);
				PlanetExpress.items.effects.BoomEffect.Show(X-20, Y+20);
				PlanetExpress.items.effects.BoomEffect.Show(X+20, Y+20);
			}
		}
	}
}
