
using System;

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

using Math2D;
using Math2D.Shapes;

namespace PlanetExpress.items.shells {
		
	public class LaserShell : CustomShellClass {
		
		Vector2D vPos;
		Vector2D vMove;
		float z;
		static Random shootRnd = new Random();
		
		public LaserShell(float x, float y, float a) {
			vPos = new Vector2D(x, y);
			vMove = new Vector2D(
			                    (float)Math.Cos(a),
			                    (float)Math.Sin(a));
			vMove.Length *= 40;			
			vPos += vMove;
			
			z = (float)(shootRnd.NextDouble() - 0.5f) * 0.01f;
		}
		
		bool noCollision = true;
		public override bool Action (double time) {
			base.Action(time);
			vPos += vMove * (float)time * 10f;
			FX = vPos.X;
			FY = vPos.Y;
			return 
				(noCollision && X > - 10 && Y > - 10 && X < GameApp.ScreenWidth + 10 && Y < GameApp.ScreenHeight + 10);
		}
		
		protected override Polygon2D[] GetShape () {
			float sz = 40f;
			return new Polygon2D[] {
				new Polygon2D(
				              new Vector2D(X - sz, Y-2f),
				              new Vector2D(X, Y-2f),
				              new Vector2D(X, Y+2f)),
				new Polygon2D(
				              new Vector2D(X - sz, Y-2f),
				              new Vector2D(X - sz, Y+2f),
				              new Vector2D(X, Y+2f)),
			};
		}
		
		public override void OnCollision (CustomGameObject with) {
			if (CollisionClass == with.CollisionClass) return;
			with.Demage(1);
			noCollision = false;
		}
		
		public override void Draw () {
			GL.Disable(EnableCap.Texture2D);
			
			GL.LineWidth(2f);
			GL.Enable(EnableCap.LineSmooth);
			if (CollisionClass == 0)
				GL.Color4(new float[] {1f, 0f, 0f, 0.8f});
			else
				GL.Color4(new float[] {0f, 0.8f, 1f, 0.8f});
			
			GL.Begin(BeginMode.Lines); {
				GL.Vertex3(vPos.X, vPos.Y, z);
				
			if (CollisionClass == 0)
				GL.Color4(new float[] {1f, 0f, 0f, 0f});
			else
				GL.Color4(new float[] {0f, 0.8f, 1f, 0f});
				
				GL.Vertex3(vPos.X - vMove.X, vPos.Y - vMove.Y, z);
			} GL.End();
			GL.Enable(EnableCap.Texture2D);
		}

	}
}
