#include"DxLib.h"

const int KeyboardCode[10]=
{KEY_INPUT_UP
,KEY_INPUT_RIGHT
,KEY_INPUT_DOWN
,KEY_INPUT_LEFT
,KEY_INPUT_Z
,KEY_INPUT_X
,KEY_INPUT_C
,KEY_INPUT_LSHIFT
,KEY_INPUT_ESCAPE
,KEY_INPUT_RETURN
};

bool KeyInput[10];

void GetNewInput()
{
	for(int i=0;i<10;++i) KeyInput[i]=CheckHitKey(KeyboardCode[i]);
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow )
{
	ChangeWindowMode(true);
	if( DxLib_Init() == -1 ) return -1;
	
	int num[10];
	int pass[8];

	LoadDivGraph(L"data\\num.png",10,10,1,6,11,num);
	pass[0]=LoadGraph(L"data\\BG.png");
	pass[1]=LoadGraph(L"data\\Moon.png");
	pass[2]=LoadGraph(L"data\\Dot.png");
	pass[3]=LoadGraph(L"data\\HP.png");
	pass[4]=LoadGraph(L"data\\Score.png");
	pass[5]=LoadGraph(L"data\\Gameover.png");
	pass[6]=LoadGraph(L"data\\Title.png");
	pass[7]=LoadGraph(L"data\\Item.png");

	SetDrawScreen(DX_SCREEN_BACK);

	int c=0;
	int _s=GetNowCount(),e;
	int time[6]={7,7,6,6,6,6};

	int X,Y;
	int DotX[10000];
	int DotY[10000];
	int score=0;
	int state=0;
	int s,t;
	int angle;
	int HP,hp;
	int Next;
	int Speed=1;
	int ItemX,ItemY;
	int itemn=0;

	int moonnum[220]=
	{
206,
195,
187,
181,
175,
167,
163,
161,
156,
153,
150,
147,
144,
142,
139,
136,
134,
132,
130,
127,
125,
123,
121,
119,
117,
115,
114,
112,
110,
108,
107,
105,
104,
102,
100,
99,
97,
96,
94,
93,
92,
90,
89,
88,
86,
85,
84,
82,
81,
80,
79,
78,
77,
75,
74,
73,
72,
71,
70,
69,
68,
67,
66,
65,
64,
63,
62,
61,
60,
59,
58,
57,
56,
55,
54,
54,
53,
52,
51,
50,
49,
48,
48,
47,
46,
45,
45,
44,
43,
42,
42,
41,
40,
39,
39,
38,
37,
37,
36,
35,
35,
34,
34,
33,
32,
32,
32,
30,
30,
29,
29,
28,
28,
27,
26,
26,
25,
25,
24,
24,
23,
23,
22,
22,
21,
21,
20,
20,
20,
19,
19,
18,
18,
17,
17,
16,
16,
16,
15,
15,
15,
14,
14,
13,
13,
13,
12,
12,
12,
11,
11,
11,
10,
10,
10,
9,
9,
9,
9,
8,
8,
8,
7,
7,
7,
7,
6,
6,
6,
6,
5,
5,
5,
5,
4,
4,
4,
4,
4,
4,
3,
3,
3,
3,
3,
3,
2,
2,
2,
2,
2,
2,
2,
2,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
0
	};

	bool bts=false;

	while(ProcessMessage()==0)
	{
		ClearDrawScreen();
		GetNewInput();
	//ここからメイン処理
		DrawGraph(0,0,pass[0],false);
		DrawGraph(100,0,pass[1],true);

		s=score;
		t=10000;
		DrawGraph(5,5,pass[4],true);
		for(int i=0;i<5;++i)
		{
			DrawGraph(44+(i*7),5,num[s/t],true);
			s=s-((s/t)*t);
			t=t/10;
		}

		switch(state)
		{
		case 0:
			DrawGraph(0,0,pass[6],true);
			if(KeyInput[9] && !bts)
			{
				state=1;
				X=220;
				Y=0;
				HP=10000;
				score=0;
				Next=1;
				angle=2;
				ItemX=0;
				ItemY=330;
				itemn=0;
				PlayMusic(L"data\\kusomusic.mid",DX_PLAYTYPE_LOOP);
			}
			if(!KeyInput[9])bts=false;
			break;
		case 1:
			for(int i=0;i<4;++i) if(KeyInput[i]) angle=i;
			--Next;
			if(Next==0)
			{
				++score;
				DotX[10000-HP]=X;
				DotY[10000-HP]=Y;
				switch(angle)
				{
				case 0:
					--Y;
					break;
				case 1:
					++X;
					break;
				case 2:
					++Y;
					break;
				case 3:
					--X;
					break;
				}
				for(int i=0;i<10000-HP;++i)
				{
					if(X==DotX[i] && Y==DotY[i])
					{
						hp=HP;
						HP=0;
						goto esc;
					}
				}
				
				--HP;
				Next=Speed;
				s=Y;
				if(Y<0 || Y>=440) 
				{
					hp=HP;
					HP=0;
					goto esc;
				}
				if(s>219) s=439-s;
				if(moonnum[s]>X || 440-moonnum[s]<X) HP=0;
			}
			switch(itemn)
			{
			case 0:
				++ItemX;
				if(ItemX>440)
				{
					ItemX=110;
					ItemY=0;
					itemn=1;
				}
				break;
			case 1:
				++ItemY;
				if(ItemY==440)
				{
					ItemX=420;
					ItemY=110;
					itemn=2;
				}
				break;
			case 2:
				--ItemX;
				if(ItemX==-20)
				{
					ItemX=330;
					ItemY=420;
					itemn=3;
				}
				break;
			case 3:
				--ItemY;
				if(ItemY==-20)
				{	
					ItemX=0;
					ItemY=330;
					itemn=0;
				}
				break;
			}
			if(ItemX<=X && ItemX+19>=X && ItemY<=Y && ItemY+19>=Y)
			{
				score+=1000;
				switch(itemn)
				{
				case 0:
					ItemX=110;
					ItemY=0;
					itemn=1;
					break;
				case 1:
					ItemX=420;
					ItemY=110;
					itemn=2;
					break;
				case 2:
					ItemX=330;
					ItemY=420;
					itemn=3;
					break;
				case 3:
					ItemX=0;
					ItemY=330;
					itemn=0;
					break;
				}
			}

esc://ここからグラフィック
			DrawGraph(100+X,Y,pass[2],false);
			for(int i=0;i<10000-HP;++i)DrawGraph(DotX[i]+100,DotY[i],pass[2],false);
			s=HP;
			t=1000;
			DrawGraph(580,5,pass[3],true);
			for(int i=0;i<4;++i)
			{
				DrawGraph(600+(7*i),5,num[s/t],true);
				s=s-((s/t)*t);
				t=t/10;
			}
			DrawGraph(ItemX+100,ItemY,pass[7],true);
			if(HP==0) 
			{
					state=2;
					StopMusic();
			}
			break;
		case 2:
			DrawGraph(100+X,Y,pass[2],false);
			for(int i=0;i<10000-hp;++i)DrawGraph(DotX[i]+100,DotY[i],pass[2],false);
			s=HP;
			t=1000;
			DrawGraph(580,5,pass[3],true);
			for(int i=0;i<4;++i)
			{
				DrawGraph(600+(7*i),5,num[s/t],true);
				s=s-((s/t)*t);
				t=t/10;
			}

			DrawGraph(0,0,pass[5],true);
			if(KeyInput[9])
			{
				state=0;
				bts=true;
			}

			break;
		}

	//ここまで

		c=(c+1)%3;
		e=GetNowCount();
		while(e-_s<17-(c/2)) e=GetNowCount();
		_s=e;
		ScreenFlip();
	}

	InitGraph();
	StopMusic();

	DxLib_End();
}