context: /**
   * RawBots: an awesome robot game
   * 
   * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
   * Copyright 2012 Mark Foo Bonasoro <foo_mark@q8ismobile.com>
   * 
   * This Source Code Form is subject to the terms of the Mozilla Public
   * License, v. 2.0. If a copy of the MPL was not distributed with this file,
   * You can obtain one at http://mozilla.org/MPL/2.0/.
   */
  
  using System;
  
  using OpenTK;
  using OpenTK.Input;
  using OpenTK.Graphics;
  using OpenTK.Graphics.OpenGL;
  
  namespace Rawbots
  {
  	public class Robot : Model
  	{
  		Chassis chassis;
  		Weapon[] weapons;
  		Electronics electronics;
  
  		private Light light;
  		private bool lightOn;
  
  		private float posX;
  		private float posY;
  		private int mapPosX;
  		private int mapPosY;
  		private float finalTotalHeight = 0.0f;
  
  		private int life = 100; //life meter
  
  		private bool friend = false; //Identifies whether friend or foe
  
  		private int waitTime = 4000; //Wait for 4 seconds
  		private int startTime = 0;
  
  		private const int INIT = 0;
  		private const int INIT_WAITING = 1;
  		private const int WAITING = 2;
  		private const int INIT_MOVING = 3;
  		private const int MOVING = 4;
  		private const int POSSESSED = 5;
  		private int state = INIT;
  
  		private const int UP = 0;	//0 degrees
  		private const int DOWN = 1; //180 degrees
  		private const int LEFT = 2; //-90 degrees
  		private const int RIGHT = 3; //90 degrees
  		private const int MAX_DIRECTIONS = 4;
  		private float[] DirectionAngles = new float[] { 0.0f, 180.0f, -90.0f, 90.0f};
  		private float currAngle = 0; //keep track of the current angle
  		private float angleToTurn = 0;
  		private int prevFacingDirection = UP;
  		private int currFacingDirection = UP;
  
  		private int currFrame = 0;
  		private int MAX_FRAMES = 100;
  
  		public Random random;
  
  		public int MapPosX
  		{
  			get { return mapPosX; }
  			
  			set
  			{
  				mapPosX = value;
  				posX = (float) mapPosX;
  			}
  		}
  
  		public int MapPosY
  		{
  			get { return mapPosY; }
  
  			set
  			{
  				mapPosY = value;
  				posY = (float) (-1 * mapPosY);
  			}
  		}
  
  		public float PosX
  		{
  			get { return posX; }
  
  			set
  			{
  				posX = value;
  				mapPosX = (int) posX;
  			}
  		}
  
  		public float PosY
  		{
  			get { return posY; }
  
  			set
  			{
  				posY = value;
  				mapPosY = (int) ((-1.0) * posY);
  			}
  		}
  
  		public float Angle { get; set; }
  
  		private void Init()
  		{
  			chassis = null;
  			weapons = null;
  			electronics = null;
  			lightOn = false;
  			Angle = 0.0f;
  
  			light = new Light(LightName.Light2);
  		}
  		
  		public Robot()
  		{
  			Init();
  			MapPosX = 0;
  			MapPosY = 0;
  			random = new Random(Environment.TickCount & Int32.MaxValue);
  		}
  		
  		public Robot(int x, int y)
  		{
  			Init();			
  			MapPosX = x;
  			MapPosY = y;
  			random = new Random(Environment.TickCount & Int32.MaxValue);
  		}
  
  		public void UpdateState()
  		{
  			int currTime = 0;
  
  			switch (state)
  			{ 
  				case INIT:
  					state = INIT_WAITING;
  					break;
  				case INIT_WAITING:
  					startTime = Environment.TickCount & Int32.MaxValue;
  					state = WAITING;
  					break;
  				case WAITING:
  					currTime = Environment.TickCount & Int32.MaxValue;
  					if (currTime - startTime > waitTime)
  						state = INIT_MOVING;
  					break;
  				case INIT_MOVING:
  					//Here we try to find the shortest path to our friend
  					//We will use Dijktra's algorithm to find it
  					prevFacingDirection = currFacingDirection;
  					int dir = random.Next(3);
  					currFacingDirection = ((dir+1) + prevFacingDirection)%MAX_DIRECTIONS;
  					
  					switch (currFacingDirection)
  					{ 
  						case UP:
  							angleToTurn = DirectionAngles[prevFacingDirection] - DirectionAngles[UP];
  							break;
  						case DOWN:
  							angleToTurn = DirectionAngles[prevFacingDirection] - DirectionAngles[DOWN];
  							break;
  						case LEFT:
  							angleToTurn = DirectionAngles[prevFacingDirection] - DirectionAngles[LEFT];
  							break;
  						case RIGHT:
  							angleToTurn = DirectionAngles[prevFacingDirection] - DirectionAngles[RIGHT];
  							break;
  					}
  
  					state = MOVING;
  					break;
  				case MOVING:
  					Angle = linearTween(currFrame, DirectionAngles[currFacingDirection], angleToTurn, MAX_FRAMES);
  					if (currFrame < MAX_FRAMES)
  						currFrame++;
  					else
  						state = INIT_WAITING;
  					break;
  				case POSSESSED:
  					break;
  			}
  		}
  
  <<<<<<< HEAD
  		public float linearTween(float t, float b, float c, float d) 
  		{
  			return c*t/d + b;
  =======
  		public int GetALife()
  		{
  			return life;
  >>>>>>> b8f5b9c6547ded96314d6b4338395af7187c8fa8
  		}
  
  		public void ToggleLight()
  		{
  			lightOn = (lightOn) ? false : true;
  		}
  
  		public void AddChassis(Chassis chassis)
  		{
  			this.chassis = chassis;
  		}
  		
  		public void AddWeapon(Weapon weapon)
  		{
  			if (weapons == null)
  			{
  				weapons = new Weapon[1];
  				weapons[0] = weapon;
  			}
  			else
  			{
  				Array.Resize(ref weapons, weapons.Length + 1);
  				weapons[weapons.Length - 1] = weapon;
  			}
  		}
  		
  		public void AddElectronics(Electronics electronics)
  		{
  			this.electronics = electronics;
  		}
  
          public void AddLight(Light l)
          {
              light = l;
              lightOn = true;
          }
  
  		public void Push()
  		{
  			GL.PushMatrix();
  		}
  		
  		public void Pop()
  		{
  			GL.PopMatrix();
  		}
  		
  		public void RenderAll()
  		{
  			if (!show)
  				return;
  
  			Push();
  			Render();
  			Pop();
  		}
  
          public override void SetRenderMode(RenderMode renderMode)
          {
              if (chassis != null)
                  chassis.SetRenderMode(renderMode);
  
              if (weapons != null)
  			{
                  for (int i = 0; i < weapons.Length; i++)
                      weapons[i].SetRenderMode(renderMode);
  			}
              
              if (electronics != null)
                  electronics.SetRenderMode(renderMode);
          }
  
  		public override void ShowTextures()
  		{
  			if (chassis != null)
  				chassis.ShowTextures();
  
  			if (weapons != null)
  				for (int i = 0; i < weapons.Length; i++)
  					weapons[i].ShowTextures();
  
  			if (electronics != null)
  				electronics.ShowTextures();
  		}
  
  		public override void HideTextures()
  		{
  			if (chassis != null)
  				chassis.HideTextures();
  
  			if (weapons != null)
  				for (int i = 0; i < weapons.Length; i++)
  					weapons[i].HideTextures();
  
  
gt: 			if (electronics != null)