#include "stdafx.h"
#include <allegro5\allegro.h>
#include <allegro5\allegro_native_dialog.h>
#include  <allegro5\allegro_primitives.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_font.h>
#include <iostream>
#include <time.h>
#include <windows.h>
#define width 700
#define height 500
bool collision(float &x, float &y, float tX, float tY, int widthh, int heightt)
{
	if (x > tX - widthh / 7 && x < tX + widthh / 7 && y > tY - heightt / 7 && y < tY + heightt / 7) {
		return true;
	}
	return false;
}
int main()
{
	srand(time(NULL));
	al_init();
	if (!al_init())
	{
		std::cout << "Failed to initialize Allegro";
		return -1;
	}
	ALLEGRO_DISPLAY *myDisplay = al_create_display(width, height);
	if (!myDisplay)
	{
		std::cout << "Failed to display your window";
	}

	//Variables
	enum direction { DOWN, LEFT, RIGHT, UP };
	enum direction2 { DOWN2, LEFT2, RIGHT2, UP2 };

	bool fireballUpLV2 = false, fireballDownLV2 = false, fireballLeftLV2 = false, fireballRightLV2 = false;
	bool finish = false, draw = true, active = false, fireballLeft = false, fireballRight = false, fireballUp = false, fireballDown = false, fireballFollow = false, destroyBG = false;
	bool fireballFollow2 = false, fireballLeft2 = false, fireballUp2 = false, fireballDown2 = false, fireballRight2 = false;
	int directions = UP;
	int directions2 = UP2;
	int fireballDirection = LEFT2;
	int directionLevel2 = UP2;
	int fireballdirLV2 = LEFT2;
	int X3 = 200, Y3 = 200;
	float X = 10, Y = 10, speed = 5, sX = 32, sY = 0, sX2 = 32, sY2 = 0, X2 = 200, Y2 = 200, cX = 10, cY = 10, cX2 = 10, cY2 = 10, hp = 100;
	float sX3 = 32, sY3 = 0, cY3 = 0, cX3 = 0;
	float FPS = 30;
	float curX = 0, curY = 0;
	int countCircle = 0, countMoves = 0, countCircle2 = 0, countMovesLV2 = 0, countCircleLV2 = 0, directionLV2 = 0, fireballFollowLV2 = 0;

	//Install
	al_init_primitives_addon();
	al_install_keyboard();
	al_init_image_addon();
	al_init_font_addon(); // initialize the font addon
	al_init_ttf_addon();// initialize the ttf (True Type Font) addon

						//Events and Queues
	ALLEGRO_FONT *font = al_load_ttf_font("yardsale.ttf", 72, 0);
	ALLEGRO_KEYBOARD_STATE keyboardstate;
	ALLEGRO_BITMAP *player = al_load_bitmap("carlo.png");
	ALLEGRO_BITMAP *player2 = al_load_bitmap("carlo.png");
	ALLEGRO_BITMAP *player3 = al_load_bitmap("carlo.png");
	ALLEGRO_BITMAP *bg = al_load_bitmap("grass.jpg");
	ALLEGRO_BITMAP *bg2 = al_load_bitmap("bg2.png");
	ALLEGRO_EVENT_QUEUE *myqueue = al_create_event_queue();
	ALLEGRO_TIMER *mytimer = al_create_timer(1 / FPS);
	ALLEGRO_TIMER *mytimer2 = al_create_timer(1 / FPS);
	al_register_event_source(myqueue, al_get_keyboard_event_source());
	al_register_event_source(myqueue, al_get_timer_event_source(mytimer));
	al_register_event_source(myqueue, al_get_display_event_source(myDisplay));
	al_register_event_source(myqueue, al_get_timer_event_source(mytimer2));
	cX = X;
	cY = Y;
	cX2 = X2;
	cY2 = Y2;

	al_start_timer(mytimer);

	while (!finish)
	{
		countMoves++;
		countCircle++;
		countCircle2++;
		countMovesLV2++;
		countCircleLV2++;
		if (X > 640)
			X -= 10;
		if (X < -20)
			X += 10;
		if (Y > 420)
			Y -= 10;
		if (Y < -20)
			Y += 10;
		if (X2 > 640)
			directions2 = direction2(rand() % 4);
		if (X2 < -20)
			directions2 = direction2(rand() % 4);
		if (Y2 > 420)
			directions2 = direction2(rand() % 4);
		if (Y2 < -20)
			directions2 = direction2(rand() % 4);

		if (X3 > 640)
			directionLV2 = direction2(rand() % 4);
		if (X3 < -20)
			directionLV2 = direction2(rand() % 4);
		if (Y3 > 420)
			directionLV2 = direction2(rand() % 4);
		if (Y3 < -20)
			directionLV2 = direction2(rand() % 4);
		ALLEGRO_EVENT curEvents;
		al_wait_for_event(myqueue, &curEvents);
		al_get_keyboard_state(&keyboardstate);
		if (curEvents.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
			finish = true;
		else if (curEvents.type == ALLEGRO_EVENT_TIMER)
		{
			active = true;

			if (al_key_down(&keyboardstate, ALLEGRO_KEY_ESCAPE))
				finish = true;
			if (al_key_down(&keyboardstate, ALLEGRO_KEY_DOWN))
			{
				Y += speed;
				directions = DOWN;
			}
			else if (al_key_down(&keyboardstate, ALLEGRO_KEY_LEFT))
			{
				X -= speed;
				directions = LEFT;
			}
			else if (al_key_down(&keyboardstate, ALLEGRO_KEY_RIGHT))
			{
				X += speed;
				directions = RIGHT;
			}
			else if (al_key_down(&keyboardstate, ALLEGRO_KEY_UP))
			{
				Y -= speed;
				directions = UP;
			}
			else if (al_key_down(&keyboardstate, ALLEGRO_KEY_ENTER))
			{
				Y2 = -100000;
				X2 = -10000000;
				destroyBG = true;
			}
			else if (al_key_down(&keyboardstate, ALLEGRO_KEY_A) && countCircle < 30)
			{
				std::cout << "Cooldown";
			}
			else if (al_key_down(&keyboardstate, ALLEGRO_KEY_A) && countCircle > 30)
			{
				countCircle = 0;
				if (directions == DOWN)
				{
					fireballDown = true;
					fireballFollow = true;
					fireballUp = false;
					fireballLeft = false;
					fireballRight = false;


				}
				else if (directions == UP)
				{
					fireballFollow = true;
					fireballUp = true;
					fireballDown = false;
					fireballLeft = false;
					fireballRight = false;

				}
				else  if (directions == LEFT)
				{
					fireballLeft = true;
					fireballFollow = true;
					fireballUp = false;
					fireballDown = false;
					fireballRight = false;
				}
				else if (directions == RIGHT)
				{
					fireballRight = true;
					fireballFollow = true;
					fireballUp = false;
					fireballLeft = false;
					fireballDown = false;
				}
			}
			else
			{
				active = false;
			}
			if (collision(cX, cY, X2, Y2, al_get_bitmap_width(player2), al_get_bitmap_height(player2))) {
				hp -= 2;
				if (hp == 0)
				{
					std::cout << "Player2 is dead" << std::endl;
					Y2 = -100000;
					X2 = -10000000;
					destroyBG = true;

				}

				std::cout << "HP: " << hp
					<< std::endl;
			}
			if (collision(X, Y, X2, Y2, al_get_bitmap_width(player2), al_get_bitmap_height(player2))) {
				if (directions == DOWN)
					Y -= speed;
				else if (directions == UP)
					Y += speed;
				else if (directions == RIGHT)
					X -= speed;
				else if (directions == LEFT)
					X += speed;
			}
			if (collision(X2, Y2, X, Y, al_get_bitmap_width(player), al_get_bitmap_height(player))) {
				if (directions == DOWN)
					Y2 += speed;
				else if (directions == UP)
					Y2 -= speed;
				else if (directions == RIGHT)
					X2 += speed;
				else if (directions == LEFT)
					X2 -= speed;
			}
			if (active)
			{
				sX += al_get_bitmap_width(player) / 3;
				sX2 += al_get_bitmap_width(player2) / 3;
				sX3 += al_get_bitmap_width(player3) / 3;

			}
			else
			{
				sX = 80;
				sX2 = 80;
				sX3 = 80;

			}
			if (sX >= al_get_bitmap_width(player))
				sX = 0;
			if (sX >= al_get_bitmap_width(player))
				sX = 0;
			if (sX2 >= al_get_bitmap_width(player2))
				sX2 = 0;
			if (sX2 >= al_get_bitmap_width(player2))
				sX2 = 0;
			if (sX3 >= al_get_bitmap_width(player3))
				sX3 = 0;
			if (sX3 >= al_get_bitmap_width(player3))
				sX3 = 0;

			sY = directions;
			sY2 = directions2;
			sY3 = directionLV2;
			draw = true;

		}
		if (fireballFollow)
		{
			cY = Y;
			cX = X;
			cY += 50;
			cX += 50;
			fireballFollow = false;

		}
		if (fireballFollow2)
		{
			cY2 = Y2;
			cX2 = X2;
			cY2 += 50;
			cX2 += 50;
			fireballFollow2 = false;
		}
		if (fireballFollowLV2)
		{
			cY3 = Y3;
			cX3 = X3;
			cY3 += 50;
			cX3 += 50;
			fireballFollowLV2 = false;
		}
		if (draw)
		{
			draw = false;
			al_draw_bitmap(bg, 1, 1, NULL);

			if (destroyBG)
			{
				bg = al_load_bitmap("bg2.png");
				al_draw_bitmap_region(player3, sX3, sY3 * al_get_bitmap_height(player3) / 4, 80, 80, X3, Y3, NULL);
				al_draw_bitmap_region(player, sX, sY * al_get_bitmap_height(player) / 4, 80, 80, X, Y, NULL);
				if (countMovesLV2 > 30)
				{
					directionLV2 = direction2(rand() % 4);
					countMoves = 0;
				}
				if (countCircleLV2 > 30)
				{
					if (X > X3 || X < X3)
					{

						fireballdirLV2 = RIGHT2;
						countCircleLV2 = 0;
					}
					else if (Y < Y3 || Y > Y3)
					{
						fireballdirLV2 = UP2;
						countCircleLV2 = 0;
					}
				}

				if (fireballdirLV2 == RIGHT2)
				{
					fireballRightLV2 = true;
					fireballLeftLV2 = true;
					fireballFollowLV2 = true;
					fireballDownLV2 = false;
					fireballUpLV2 = false;
					if (countCircleLV2 < 30)
						fireballFollowLV2 = false;

				}
				else if (fireballdirLV2 == UP2)
				{
					fireballRightLV2 = false;
					fireballFollowLV2 = true;
					fireballLeftLV2 = false;
					fireballDownLV2 = true;
					fireballUpLV2 = true;
					if (countCircleLV2 < 30)
						fireballFollowLV2 = false;

				}
				if (directionLV2 == UP2)
				{
					Y3 -= 5;

				}
				else if (directionLV2 == DOWN2)
				{
					Y3 += 5;


				}
				else if (directionLV2 == LEFT2)
				{
					X3 -= 5;


				}
				else if (directionLV2 == RIGHT2)
				{
					X3 += 5;

				}


				if (fireballUpLV2)
				{
					al_draw_circle(cX3, cY3, 20, al_map_rgb(200, 200, 50), 5);
					cY3 -= speed + 5;

				}
				else if (fireballDownLV2)
				{
					al_draw_circle(cX3, cY3, 20, al_map_rgb(200, 200, 50), 5);
					cY3 += speed + 5;

				}
				 else if (fireballLeftLV2)
				{
					al_draw_circle(cX3, cY3, 20, al_map_rgb(200, 200, 50), 5);
					cX3 -= speed + 5;

				}
				 else if (fireballRight2)
				{
					al_draw_circle(cX3, cY3, 20, al_map_rgb(200, 200, 50), 5);
					cX3 += speed + 5;
				}

			}
			al_draw_bitmap_region(player, sX, sY * al_get_bitmap_height(player) / 4, 80, 80, X, Y, NULL);
			if (countMoves > 30)
			{
				directions2 = direction2(rand() % 4);
				countMoves = 0;
			}
			if (countCircle2 > 30)
			{
				if (X > X2)
				{
					fireballDirection = RIGHT2;
					countCircle2 = 0;
				}
				if (X < X2)
				{
					fireballDirection = LEFT2;
					countCircle2 = 0;
				}
			}

			if (fireballDirection == LEFT2)
			{
				fireballLeft2 = true;
				fireballFollow2 = true;
				fireballDown = false;
				fireballRight2 = false;
				fireballUp2 = false;
				if (countCircle2 < 30)
					fireballFollow2 = false;

			}
			else if (fireballDirection == RIGHT2)
			{
				fireballRight2 = true;
				fireballFollow2 = true;
				fireballLeft2 = false;
				fireballDown2 = false;
				fireballUp2 = false;
				if (countCircle2 < 30)
					fireballFollow2 = false;

			}
			if (directions2 == UP2)
			{
				Y2 -= 5;

			}
			else if (directions2 == DOWN2)
			{
				Y2 += 5;


			}
			else if (directions2 == LEFT2)
			{
				X2 -= 5;


			}
			else if (directions2 == RIGHT2)
			{
				X2 += 5;

			}

		
			al_draw_bitmap_region(player2, sX2, sY2 * al_get_bitmap_height(player2) / 4, 80, 80, X2, Y2, NULL);


			if (fireballUp)
			{
				al_draw_circle(cX, cY, 20, al_map_rgb(200, 200, 50), 5);
				cY -= speed;

			}
			else if (fireballDown)
			{
				al_draw_circle(cX, cY, 20, al_map_rgb(200, 200, 50), 5);
				cY += speed;


			}
			else if (fireballLeft)
			{
				al_draw_circle(cX, cY, 20, al_map_rgb(200, 200, 50), 5);
				cX -= speed;

			}
			else if (fireballRight)
			{
				al_draw_circle(cX, cY, 20, al_map_rgb(200, 200, 50), 5);
				cX += speed;
			}
			if (fireballUp2)
			{
				al_draw_circle(cX2, cY2, 20, al_map_rgb(200, 200, 50), 5);
				cY2 -= speed;

			}
			else if (fireballDown2)
			{
				al_draw_circle(cX2, cY2, 20, al_map_rgb(200, 200, 50), 5);
				cY2 += speed;

			}
			else if (fireballLeft2)
			{
				al_draw_circle(cX2, cY2, 20, al_map_rgb(200, 200, 50), 5);
				cX2 -= speed;

			}
			else if (fireballRight2)
			{
				al_draw_circle(cX2, cY2, 20, al_map_rgb(200, 200, 50), 5);
				cX2 += speed;
			}
			al_flip_display();
			al_clear_to_color(al_map_rgb(0, 0, 0));
		}
	}

	al_destroy_display(myDisplay);
	al_destroy_timer(mytimer);
	al_destroy_event_queue(myqueue);
	al_destroy_bitmap(player);
	al_destroy_bitmap(player2);
	return 0;
}