
using System;

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

using PlanetExpress.Tools;
using Math2D;

namespace PlanetExpress.items.enemys {
	
	
	public class MetheorClass : EnemyClass {
		
		protected TexClass Metheor;
		
		private static Random rnd = new Random();
		
		float MapScale = 1f;
		public MetheorClass() : base() {
			ScaleTick = (float)rnd.NextDouble()*10f;
			Metheor = 
				TexMan.LoadTexture(
					GameApp.GameFileName("data", "items", "metheor.gif"));
			LoadMap(GameApp.GameFileName("data", "items", "metheor.map"));
			BaseSize = 20f;
			MapScale = (BaseSize / Metheor.Width)*2;
			HP = 1;
		}
		
		public override void OnCollision (CustomGameObject with) {
			if (CollisionClass == with.CollisionClass) return;						
			if (with.GetType() == typeof(PlayerClass)) {
				with.Demage(1);
				HP = 0;
			}
		}

		
		public override bool Action (double time){						
			ScaleTick += 16f * (float) time;
			bool res = (base.Action(time)) && (HP > 0);
			if (!res &&  HP <= 0) {
				PlanetExpress.items.effects.BoomEffect.Show(X, Y);	
			}
			return res;
		}

		protected override Matrix2D GetMapTransformation () {
			if (Metheor == null)
				return base.GetMapTransformation();
			float s = MapScale * SizeScale;
			return 
				Matrix2D.GetTranslate(-Metheor.Width/2, -Metheor.Height/2) * 
				Matrix2D.GetRotate(aRotate) * 
				Matrix2D.GetScale(s, s) *
					Matrix2D.GetTranslate(X, Y);
		}

		
		float ScaleTick;
		float aRotate = 0f;
		public override void Draw () {
			GL.PushMatrix(); {
				GL.Translate(X, Y, 0f);							
				GL.Scale(SizeScale, SizeScale, 1f);		
				aRotate = ScaleTick / 10f;
				GL.Rotate(aRotate/(float)Math.PI * 180f, 0f, 0f, 1f);
				
				Metheor.Bind();
				GL.Begin(BeginMode.Quads); {
					GL.Color3(new float[]{ColorR, ColorG, ColorB});
					GL.TexCoord2(0f, 0f); GL.Vertex2(-BaseSize, -BaseSize);
					GL.TexCoord2(1f, 0f); GL.Vertex2(BaseSize, -BaseSize);
					GL.TexCoord2(1f, 1f); GL.Vertex2(BaseSize, BaseSize);
					GL.TexCoord2(0f, 1f); GL.Vertex2(-BaseSize, BaseSize);
				} GL.End();					
			} GL.PopMatrix();			
			DrawMap();
		}

	}
}
