/*
* Copyright 2023, Ali Arya
* Anyone Can Code: Algorithmic Thinking
* Example 6.3c
*/

#include "Main.h"

class GameObject
{
public:
	int x;
	int y;
	SDLX_Bitmap* shape;
	bool visible;

	void Init(char* fname, int xx, int yy)
	{
		shape = SDLX_LoadBitmap(fname);
		x = xx;
		y = yy;
		visible = true;
	}
	void Move()
	{
		x += rand() % 5 - 2;
		y += rand() % 5 - 2;
	}
	void Draw()
	{
		if(visible)
			SDLX_DrawBitmap(shape, x, y);
	}
	bool Collision(GameObject* obj)
	{
		int distanceX = abs(x - obj->x);
		int distanceY = abs(y - obj->y);
		if (distanceX < 10 && distanceY < 10)
			return true;
		else
			return false;
	}
};
int main(int argc, char* argv[])
{
	/////////////////////////////////////////////////////////////////
	// INIT part of the game
	// things that are done only once at the start (initialization)
	/////////////////////////////////////////////////////////////////

	//init SDL library and graphics window
	SDLX_Init("Simple 2D Game", 640, 480, true);

	//game map (background)
	SDLX_Bitmap* map = SDLX_LoadBitmap("map.bmp", false, NULL); //do not use any mask for transparent color

	//game objects
	GameObject player;
	player.Init((char*)"Player.bmp", 0, 0);
	GameObject enemy;
	enemy.Init((char*)"Enemy.bmp", 100, 180);
	GameObject prize;
	prize.Init((char*)"Prize.bmp", 200, 200);

	bool quit = false;
	bool win = false;

	bool right = false;
	bool left = false;
	bool up = false;
	bool down = false;

	/////////////////////////////////////////////////////////////////
	//--------Main game Loop---------- -
	// things that are done repeatedly
	/////////////////////////////////////////////////////////////////
	while (!quit)
	{
		/////////////////////////////////////////////
		// UPDATE
		/////////////////////////////////////////////

		//check if we hit the prize
		if(player.Collision(&prize))
		{
			quit = true;
			win = true;
			prize.visible = false;
		}
		//check if we hit the enemy
		if (player.Collision(&enemy))
		{
			player.visible = false;
			quit = true;
			win = false;
		}

		//check if there is any movement
		//player controlled by keyboard
		SDLX_Event e;
		//keyboard events
		if (SDLX_PollEvent(&e))
		{
			if (e.type == SDL_KEYDOWN)
			{
				if (e.keycode == SDLK_ESCAPE)
					quit = true;
				//update player  
				if (e.keycode == SDLK_RIGHT)
					right = true;
				if (e.keycode == SDLK_LEFT)
					left = true;
				if (e.keycode == SDLK_UP)
					up = true;
				if (e.keycode == SDLK_DOWN)
					down = true;
			}
			if (e.type == SDL_KEYUP)
			{
				if (e.keycode == SDLK_RIGHT)
					right = false;
				if (e.keycode == SDLK_LEFT)
					left = false;
				if (e.keycode == SDLK_UP)
					up = false;
				if (e.keycode == SDLK_DOWN)
					down = false;
			}
		}

		//update player
		if (right)
			player.x += 3;
		if (left)
			player.x -= 3;
		if (up)
			player.y -= 3;
		if (down)
			player.y += 3;

		//update enemy
		enemy.Move();

		/////////////////////////////////////////////
		// DRAW
		/////////////////////////////////////////////
		//note the order (last one will be drawn on top)
		SDLX_DrawBitmap(map, 0, 0);
		prize.Draw();
		enemy.Draw();
		player.Draw();

		//Update the screen
		SDLX_Render();


		//now wait for loop timing
		SDLX_Delay(1);
	}

	SDLX_Delay(3000);

	return 0;
}