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

#include "Main.h"

#define SCREEN_W 640
#define SCREEN_H 480
#define PLAYER_W 48

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(int offset=0)
	{
		if (visible)
			SDLX_DrawBitmap(shape, x-offset, 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;
	}
};
class Game
{
public:
	GameObject player;
	GameObject prize;
	GameObject enemy;
	int offset;
	SDLX_Bitmap* map;
	int end_wait;
	bool quit;
	bool win;
	bool left;
	bool right;

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

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

		//control variables
		quit = false;
		win = false;
		right = false;
		left = false;
		offset = 0;
		ground_level = 380;
		end_wait = 0;

		//game objects
		player.Init((char*)"Player.bmp", SCREEN_W/2-PLAYER_W/2);	//middle of screen
		enemy.Init((char*)"Enemy.bmp", 0.75*SCREEN_W);			//between player and end of screen
		prize.Init((char*)"Prize.bmp", 1.5*SCREEN_W);				//off screen
	}
	void Update()
	{
		//check if we hit the prize
		if (player.Collision(&prize))
		{
			quit = true;
			win = true;
			prize.visible = false;
			end_wait = 3000;
		}
		//check if we hit the enemy
		if (player.Collision(&enemy))
		{
			player.visible = false;
			quit = true;
			win = false;
			end_wait = 3000;
		}

		//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.type == SDL_KEYUP)
			{
				if (e.keycode == SDLK_RIGHT)
					right = false;
				if (e.keycode == SDLK_LEFT)
					left = false;
			}
		}

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

		//update enemy
		enemy.Move();

		offset = player.x - (SCREEN_W / 2 - PLAYER_W / 2); //movement from original X
	}
	void Draw()
	{
		//note the order (last one will be drawn on top)
		SDLX_DrawBitmap(map, -1*offset, 0);
		prize.Draw(offset);
		enemy.Draw(offset);
		player.Draw(offset);

		//Update the screen
		SDLX_Render();
	}
	void Run()
	{
		while (!quit)
		{
			/////////////////////////////////////////////
			// UPDATE
			/////////////////////////////////////////////
			Update();

			/////////////////////////////////////////////
			// DRAW
			/////////////////////////////////////////////
			Draw();

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

		//done
		SDLX_Delay(end_wait);
	}
};

int main(int argc, char* argv[])
{
	/////////////////////////////////////////////////////////////////
	// INIT part of the game
	// things that are done only once at the start (initialization)
	/////////////////////////////////////////////////////////////////

	Game game;

	/////////////////////////////////////////////////////////////////
	//--------Main game Loop---------- -
	// things that are done repeatedly
	/////////////////////////////////////////////////////////////////

	game.Run();


	return 0;
}