

#include <iostream>
#include "SDL.h"
#include "Sprite.h"
#include "info.h"
#include <conio.h>


#define SCREEN_WIDTH  1024
#define SCREEN_HEIGHT 768
#define SPRITE_SITE    32
#define MAX_NUMBER_OF_ZOMBIES 50

enum ENEMIES   {ZOMBIE = 1};
enum DIRECTION {DOWN = 0, UP = 1, LEFT = 2, RIGHT = 3};

int main(int argc, char *argv[])
{
	
	SDL_Init(SDL_INIT_VIDEO); //initialize SDL
  SDL_WM_SetCaption("Millennium Crisis", "Millennium Crisis"); //title bar
  
	SDL_Surface *screen, *bg;
	SDL_Rect  rcPlayer, rcZombie[MAX_NUMBER_OF_ZOMBIES], rcBg;
	SDL_Event event; 
  Uint8 *keystate;
	int gameLoop = 1, loop = 1, colorKey;

	Info player, zombie[MAX_NUMBER_OF_ZOMBIES], area, bullet;
	Sprite sprite;


	area.setNumberOfZombies(10);
	area.setAreaNumber(1);

  screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_FULLSCREEN); //create window
	colorKey = SDL_MapRGB(screen->format, 255, 0, 255);
	SDL_ShowCursor(false);

  
	/*TITLE SCREEN//
	bg = sprite.loadIMG("title.bmp");
	SDL_BlitSurface(bg, NULL, screen, NULL);
	SDL_UpdateRect(screen, 0, 0, 0, 0);
	while (loop == 1)
	{
   if (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
				case SDL_QUIT:
				  loop = 0;
				  break;
	      case SDL_KEYDOWN:
				  switch (event.key.keysym.sym)
					{
				    case SDLK_SPACE:
							loop = 0;
							break;
					}
					break;
			}
	  }
	}
	//TITLE SCREEN END*/

	//Loading up sprites and such that are to be used.
	
	//loading player images...
	
	player.initImages('p');


	//loading zombie images into memory....
  
	for (int i = 0; i < area.returnNumberOfZombies(); i++)
	{
	  zombie[i].initImages('z');
		SDL_SetColorKey(zombie[i].returnImage('z', UP), SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey);
    SDL_SetColorKey(zombie[i].returnImage('z', DOWN), SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey);
	  SDL_SetColorKey(zombie[i].returnImage('z', LEFT), SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey);
	  SDL_SetColorKey(zombie[i].returnImage('z', RIGHT), SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey);

	}


	//set up color key for transparency

	SDL_SetColorKey(player.returnImage('p', UP), SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey);
	SDL_SetColorKey(player.returnImage('p', DOWN), SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey);
	SDL_SetColorKey(player.returnImage('p', LEFT), SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey);
	SDL_SetColorKey(player.returnImage('p', RIGHT), SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey);

	
	//set player position
	rcPlayer.x = 280;
	rcPlayer.y = 400;

	for (i = 0; i < area.returnNumberOfZombies(); i++)
	{
		rcZombie[i].x = (i + 1) * 20;
		rcZombie[i].y = 0;
	}
	
	
	//background load 
  bg = sprite.loadIMG("area1.bmp");


	for (i = 0; i < area.returnNumberOfZombies(); i++)
	{
		SDL_BlitSurface(zombie[i].returnImage('z', DOWN), NULL, screen, &rcZombie[i]); 
		zombie[i].Init();
	}


  player.setPlayerInRange(false);

	



	
	/* ----------------------- GAME ENGINE STARTS HERE ---------------------------------*/




	while (gameLoop)
	{
		if (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case SDL_QUIT:
				gameLoop = 0;
				break;

			case SDL_KEYDOWN:
				switch (event.key.keysym.sym)
				{
				  case SDLK_ESCAPE:
				  case SDLK_q:
					  gameLoop = 0;
	    			break;
  
				}
			}
		}


		//player movement
    keystate = SDL_GetKeyState(NULL);

		if (keystate[SDLK_LEFT])
		{	
			rcPlayer.x -= 3;
			player.setDirection('p', LEFT);
		}

		if (keystate[SDLK_RIGHT])
		{
			rcPlayer.x += 3;
			player.setDirection('p', RIGHT);
		}

		if (keystate[SDLK_UP])
		{	
			rcPlayer.y -= 3;
			player.setDirection('p', UP);
		}

		if (keystate[SDLK_DOWN])
		{
			rcPlayer.y += 3;
			player.setDirection('p', DOWN);
		}

	  
		////////////////////////////////////////////////////////////////////////////

												// THIS IS WHERE I'M WORKING //
		
		/////////////////////////////////////////////////////////////////////////////

		if (keystate[SDLK_SPACE])
		{
			for (i = 0; i < area.returnNumberOfZombies(); i++)
			{
			  if (player.canHitEnemy(rcPlayer.x, rcPlayer.y, rcZombie[i].x, rcZombie[i].y) == true && zombie[i].returnZombieThere() == true)				
					zombie[i].setHealth('z', 1);
			}
		}
					
    


    SDL_BlitSurface(bg, NULL, screen, &rcBg); //draw background on screen.

 
		for (int i = 0; i < area.returnNumberOfZombies(); i++) //for loop for the zombie enemies
		{
		  //finds the distance between sprites
		  zombie[i].setDistance('z', sprite.getDistance(rcZombie[i].x, rcZombie[i].y, rcPlayer.x, rcPlayer.y));

	    player.setPlayerInRange(sprite.inRange(zombie[i].returnDistance(), ZOMBIE)); //test to see if he's in range

		  if (rcZombie[i].x > rcPlayer.x && player.returnPlayerInRange() == true) 
			{
			  rcZombie[i].x -= 2;
			  zombie[i].setDirection('z', LEFT);
			}
		  if (rcZombie[i].x < rcPlayer.x && player.returnPlayerInRange() == true)
			{
			  rcZombie[i].x += 2; 
			  zombie[i].setDirection('z', RIGHT);
			}

		  if (rcZombie[i].y > rcPlayer.y && player.returnPlayerInRange() == true)
			{
			  rcZombie[i].y -= 2; 
			  zombie[i].setDirection('z', UP);
			}
		  if (rcZombie[i].y < rcPlayer.y && player.returnPlayerInRange() == true)
			{
			  rcZombie[i].y += 2; 
			  zombie[i].setDirection('z', DOWN);
			}

      //end of pain in the ass zombie chasing.

	
		
		  // Draw Zombie

		  if (zombie[i].returnDirection('z') == DOWN && zombie[i].returnZombieThere() == true)
		    SDL_BlitSurface(zombie[i].returnImage('z', DOWN), NULL, screen, &rcZombie[i]); 
		  if (zombie[i].returnDirection('z') == UP && zombie[i].returnZombieThere() == true)
			  SDL_BlitSurface(zombie[i].returnImage('z', UP), NULL, screen, &rcZombie[i]);
		  if (zombie[i].returnDirection('z') == LEFT && zombie[i].returnZombieThere() == true)
			  SDL_BlitSurface(zombie[i].returnImage('z', LEFT), NULL, screen, &rcZombie[i]);
		  if (zombie[i].returnDirection('z') == RIGHT && zombie[i].returnZombieThere() == true)
			  SDL_BlitSurface(zombie[i].returnImage('z', RIGHT), NULL, screen, &rcZombie[i]);
		
			
		}

		

		if (player.returnDirection('p') == UP)
		  SDL_BlitSurface(player.returnImage('p', UP), NULL, screen, &rcPlayer); //draw player
		if (player.returnDirection('p') == DOWN)
		  SDL_BlitSurface(player.returnImage('p', DOWN), NULL, screen, &rcPlayer); 
		if (player.returnDirection('p') == LEFT)
			SDL_BlitSurface(player.returnImage('p', LEFT), NULL, screen, &rcPlayer);
		if (player.returnDirection('p') == RIGHT)
			SDL_BlitSurface(player.returnImage('p', RIGHT), NULL, screen, &rcPlayer);


	  SDL_UpdateRect(screen, 0, 0, 0, 0); //update the screen
		

		

	//	SDL_Flip(screen);
		
	}

	/* ----------------------GAME ENGINE ENDS HERE -----------------------------------*/

	SDL_FreeSurface(bg);
	
	SDL_FreeSurface(player.returnImage('p', UP));
	SDL_FreeSurface(player.returnImage('p', DOWN));
	SDL_FreeSurface(player.returnImage('p', LEFT));
	SDL_FreeSurface(player.returnImage('p', RIGHT));
	
	for (i = 0; i < area.returnNumberOfZombies(); i++)
	{
	  SDL_FreeSurface(zombie[i].returnImage('z', UP));
	  SDL_FreeSurface(zombie[i].returnImage('z', DOWN));
	  SDL_FreeSurface(zombie[i].returnImage('z', LEFT));
	  SDL_FreeSurface(zombie[i].returnImage('z', RIGHT));
	}

	SDL_Quit();

	return 0;
}


/*

10=7-04: Ok, I got the game so you can kill the zombies. Only problem, when one dies, they
         all die. I'm going to have to make an array for every zombies health and make one
				 also for their direction so that they move independently from eachother. Oof this
				 is a lot of work.

9-30-04: Lots of work done today. Redid the whole engine, and in the middle of redoing the 
         image loading and handling. This files starting to get more cumbersome, but I believe
				 that the more complex way that I'm doing things will hopefully make it easier in the
				 end. Need to start working on shooting and health after I finish the image handling.
				 Oh don't forget to redo how things are drawn! (w/o leaving the trail)

9-29-04: Need to neaten the source. Started to create more classes to reduce clutter of the
         main() function. Also, I did the title screen, w/ the correct spelling of "Millennium"
				 only problem is that now it has to be redone in 1024x768. Damn. Created the correct size
				 background. Other than that.. Nothing new.
	
9-23-04: Moved the file loading into the sprite class so it's less messy when loading all the
         images. Working on collision detection before going any further w/ the game. Still
				 need to start working on ideas for engine and how to change areas.

	       Also. Need to work more on using the SDL graphics engine. I managed to weasel into
				 doing the cheap way of everything I have to change that so it runs nicer.

9-21-04: Started to make the movement facing thing. There has to be a more memory efficient
         way of doing it than loading everything into memory but it works for now. 

	*/
