#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 };
	enum direction3 { DOWN3, LEFT3, RIGHT3, UP3 };

	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 = LEFT3;
	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 FPS = 30;
	float curX = 0, curY = 0;
	int countCircle = 0, countMoves = 0, countCircle2 = 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 *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)
	{
		std::cout << countCircle2 << std::endl;
		countMoves++;
		countCircle++;
		countCircle2++;
		if (X > 640)
			X -= 10;
		else if (X < -20)
			X += 10;
		if (Y > 420)
			Y -= 10;
		else if (Y < -20)
			Y += 10;
		if (X2 > 640)
			X -= 10;
		else if (X2 < -20)
			X2 += 10;
		if (Y2 > 420)
			Y2 -= 10;
		else if (Y2 < -20)
			Y2 += 10;
		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)
		{
			if (fireballDirection == UP)
			{
				fireballUp2 = true;
				fireballFollow2 = true;
				fireballLeft2 = false;
				fireballRight2 = false;
				fireballDown2 = false;

			}
			else if (fireballDirection == DOWN)
			{
				fireballDown2 = true;
				fireballFollow2 = true;
				fireballLeft2 = false;
				fireballRight2 = false;
				fireballUp2 = false;

			}
			else if (fireballDirection == LEFT)
			{
				fireballLeft2 = true;
				fireballFollow2 = true;
				fireballDown = false;
				fireballRight2 = false;
				fireballUp2 = false;

			}
			else if (fireballDirection == RIGHT)
			{
				fireballRight2 = true;
				fireballFollow2 = true;
				fireballLeft2 = false;
				fireballDown2 = false;
				fireballUp2 = false;

			}
			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_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;

			}
			else
			{
				sX = 80;
				sX2 = 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;

			sY = directions;
			sY2 = directions2;
			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 (destroyBG)
		{
			bg = al_load_bitmap("bg2.png");
		}
		if (draw)
		{
			draw = false;
			al_draw_bitmap(bg, 1, 1, NULL);

			al_draw_bitmap_region(player, sX, sY * al_get_bitmap_height(player) / 4, 80, 80, X, Y, NULL);
			if (countMoves > 50)
			{
				directions2 = direction2(rand() % 4);
				countMoves = 0;
			}
			if (countCircle2 > 50)
			{
				fireballDirection = direction3(rand() % 4);
				countCircle2 = 0;
			}
		
			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;
}