#include <DxLib.h>


typedef struct
{
	int model_normal;
	float x;
	float y;
	float speed;
	float width;
	float height;
	int model_attack;
}redball;
typedef struct
{
	int model_normal;
	float x;
	float y;
	float speed;
	float width;
	float height;
	int model_attack;
}bluecube;
typedef struct
{
	int model_normal;
	float x;
	float y;
	float speed;
	float width;
	float height;
	int model_attack;
}yellowpyramid;
typedef struct
{
	int model_normal;
	float x;
	float y;
	float speed;
	float width;
	float height;
	int model_attack;
}p1;

typedef struct
{
	int model;
	float x;
	float y;
	float z;
	int back;
}stage_1;

typedef struct
{
	int attack;
	int special;
	int shield;
	int grab;
	int appeal;
	int up;
	int down;
	int right;
	int left;
	int jump;
	int pause;
}key;

typedef struct
{
	float x;
	float y;
	float z;
}camera;

typedef struct
{
	float x;
    float y;
	float width;
	float height;
	float center_x;
	float center_y;
}screen;

typedef struct
{
	int frame_1_x;
	int frame_1_y;
	int frame_1_graph;
	int frame_2_x;
	int frame_2_y;
	int frame_2_graph;
	int back;
	int keyflag;
	int selectsound;
	int chosesound;
}menu_t;

int attack(float attack_x,float attack_y,int attack_model)
{
	MV1SetPosition(attack_model,VGet(attack_x,attack_y,0.0f));
	MV1DrawModel(attack_model);
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	int DesktopWidth;
	int DesktopHeight;
	int DesktopColorBitDepth;
	redball rb;
	bluecube bc;
	yellowpyramid yp;
	stage_1 stage_1;
	p1 p1;
	key key;
	camera camera;
	screen screen;
	menu_t menu;

	//GetDefaultState(&DesktopWidth,&DesktopHeight,&DesktopColorBitDepth);
	SetGraphMode(1024,768,16);
	SetWindowIconID(100);
	SetMainWindowText("SolidFighters");

	ChangeWindowMode(TRUE);
	if(DxLib_Init()==-1) return -1;

	SetDrawScreen(DX_SCREEN_BACK);


	//REDBALL構造体
	rb.model_normal=MV1LoadModel("data\\characters\\redball\\redball.mqo");
	rb.x=-500.0f;
	rb.y=-5400.0f;
	rb.speed=25.0f;
	rb.width=200.0f;
	rb.height=200.0f;
	rb.model_attack=MV1LoadModel("data\\characters\\redball\\redball_attack.mqo");
	//BLUECUBE構造体
	bc.model_normal=MV1LoadModel("data\\characters\\bluecube\\bluecube.mqo");
	bc.x=-500.0f;
	bc.y=-5400.0f;
	bc.speed=10.0f;
	bc.width=200.0f;
	bc.height=200.0f;
	bc.model_attack=MV1LoadModel("data\\characters\\bluecube\\bluecube_attack.mqo");
	//YELLOWPYRAMID構造体
	yp.model_normal=MV1LoadModel("data\\characters\\yellowpyramid\\yellowpyramid.mqo");
	yp.x=-500.0f;
	yp.y=-5400.0f;
	yp.speed=50.0f;
	yp.width=200.0f;
	yp.height=200.0f;
	yp.model_attack=MV1LoadModel("data\\characters\\yellowpyramid\\yellowpyramid_attack.mqo");
	//STAGE_1構造体
	stage_1.model=MV1LoadModel("data\\stages\\stage_1\\stage_1.mqo");
	stage_1.x=0.0f;
	stage_1.y=-5500.0f;
	stage_1.back=LoadGraph("data\\stages\\stage_1\\stage_1_back.png",TRUE);
	//KEY構造体
	key.attack=(KEY_INPUT_Z);
	key.special=(KEY_INPUT_X);
	key.shield=(KEY_INPUT_C);
	key.grab=(KEY_INPUT_D);
	key.appeal=(KEY_INPUT_S);
	key.up=(KEY_INPUT_UP);
	key.down=(KEY_INPUT_DOWN);
	key.right=(KEY_INPUT_RIGHT);
	key.left=(KEY_INPUT_LEFT);
	key.jump=(KEY_INPUT_UP);
	key.pause=(KEY_INPUT_RETURN);
	//CAMERA構造体
	camera.x=0.0f;
	camera.y=-5000.0f;
	camera.z=-2000.0f;
	//SCREEN構造体
	screen.x=0.0f;
	screen.y=0.0f;
	screen.width=1024.0f;
	screen.height=768.0f;
	screen.center_x=(screen.x+screen.width)/2.0f;
	screen.center_y=(screen.y+screen.height)/2.0f-250.0f;
	//MENU構造体
	menu.frame_1_x=32;
	menu.frame_1_y=32;
	menu.frame_1_graph=LoadGraph("data\\menu\\frame_1.bmp");
	menu.frame_2_graph=LoadGraph("data\\menu\\frame_2.bmp");
	menu.back=LoadGraph("data\\menu\\back.png");
	menu.keyflag=0;
	menu.selectsound=LoadSoundMem("data\\menu\\select.wav");
	menu.chosesound=LoadSoundMem("data\\menu\\chose.wav");

	int bright;
	float jumppower;
	jumppower=0.0f;

	while(!ProcessMessage()&&!ClearDrawScreen()&&!CheckHitKey(KEY_INPUT_ESCAPE))
	{
		DrawGraph(0,0,menu.back,FALSE);
		DrawGraph(menu.frame_1_x,menu.frame_1_y,menu.frame_1_graph,TRUE);
		if(menu.keyflag==0)
		{
			if(menu.frame_1_x==32&&CheckHitKey(key.right))
			{
				menu.frame_1_x=352;
				PlaySoundMem(menu.selectsound,DX_PLAYTYPE_BACK);
			}
			else if(menu.frame_1_x==352&&CheckHitKey(key.right))
			{
				menu.frame_1_x=672;
				PlaySoundMem(menu.selectsound,DX_PLAYTYPE_BACK);
			}
            else if(menu.frame_1_x==352&&CheckHitKey(key.left))
			{
				menu.frame_1_x=32;
				PlaySoundMem(menu.selectsound,DX_PLAYTYPE_BACK);
			}
            else if(menu.frame_1_x==672&&CheckHitKey(key.left))
			{
				menu.frame_1_x=352;
				PlaySoundMem(menu.selectsound,DX_PLAYTYPE_BACK);
			}
			if(menu.frame_1_x==32&&CheckHitKey(key.pause))
			{
				menu.frame_1_graph=menu.frame_2_graph;
				DrawGraph(menu.frame_1_x,menu.frame_1_y,menu.frame_2_graph,TRUE);
				PlaySoundMem(menu.chosesound,DX_PLAYTYPE_NORMAL);
				p1.model_normal=rb.model_normal;
				p1.x=rb.x;
				p1.y=rb.y;
				p1.speed=rb.speed;
				p1.height=rb.height;
				p1.model_attack=rb.model_attack;
				break;
			}
			if(menu.frame_1_x==352&&CheckHitKey(key.pause))
			{
				menu.frame_1_graph=menu.frame_2_graph;
				DrawGraph(menu.frame_1_x,menu.frame_1_y,menu.frame_2_graph,TRUE);
				PlaySoundMem(menu.chosesound,DX_PLAYTYPE_NORMAL);
				p1.model_normal=bc.model_normal;
				p1.x=bc.x;
				p1.y=bc.y;
				p1.speed=bc.speed;
				p1.height=bc.height;
				p1.model_attack=bc.model_attack;
				break;
			}
			if(menu.frame_1_x==672&&CheckHitKey(key.pause))
			{
				menu.frame_1_graph=menu.frame_2_graph;
				DrawGraph(menu.frame_1_x,menu.frame_1_y,menu.frame_2_graph,TRUE);
				PlaySoundMem(menu.chosesound,DX_PLAYTYPE_NORMAL);
				p1.model_normal=yp.model_normal;
				p1.x=yp.x;
				p1.y=yp.y;
				p1.speed=yp.speed;
				p1.height=yp.height;
				p1.model_attack=yp.model_attack;
				break;
			}
		}
		if(CheckHitKeyAll()) menu.keyflag=1;
		if(!CheckHitKeyAll()) menu.keyflag=0;

		ScreenFlip();
	}

	DrawGraph(menu.frame_1_x,menu.frame_1_y,menu.frame_2_graph,TRUE);
	for(bright=255;bright>0;bright-=5)
	{
		ClearDrawScreen();
		if(CheckHitKey(KEY_INPUT_ESCAPE)) break;
		DrawGraph(0,0,menu.back,FALSE);
		DrawGraph(menu.frame_1_x,menu.frame_1_y,menu.frame_2_graph,TRUE);
		SetDrawBright(bright,bright,bright);
		ScreenFlip();
	}

	SetDrawBright(255,255,255);

	while((!ProcessMessage()&&!ClearDrawScreen()&&!CheckHitKey(KEY_INPUT_ESCAPE)))
	{
		attack(100.0f,-5000.0f,p1.model_attack);
		if(CheckHitKey(key.right))
		{
			p1.x+=p1.speed;
		}
		if(CheckHitKey(key.left))
		{
			p1.x-=p1.speed;
		}
		p1.y-=jumppower;
		jumppower-=1.0f;
		if(p1.y>-5400.0f)
		{
			p1.y=-5400.0f;
			jumppower=0.0f;
		}
		if(CheckHitKey(key.jump)&&p1.y==-5400.0f)
		{
			jumppower=25.0f;
		}
		if(CheckHitKey(key.special))
		{
			attack(p1.x,p1.y,p1.model_attack);
		}
		DrawGraph(0,0,stage_1.back,FALSE);
		SetCameraPositionAndTargetAndUpVec(VGet(camera.x,camera.y,camera.z),VGet(stage_1.x,stage_1.y,0.0f),VGet(0.0f,1.0f,0.0f));
	    //SetCameraScreenCenter(screen.center_x,screen.center_y); 
		SetLightDirection(VGet(-1.0f,-10.0f,1.0f));
		MV1SetPosition(p1.model_normal,VGet(p1.x,p1.y,0.0f));
		MV1SetPosition(stage_1.model,VGet(stage_1.x,stage_1.y,0.0f));
		MV1DrawModel(stage_1.model);
		MV1DrawModel(p1.model_normal);

		ScreenFlip();
	}

	DxLib_End();
	return 0;
}