#include "DxLib.h" #include #include int Key[256]; int GetHitKeyStateAll_2(int KeyStateBuf[]){ char GetHitKeyStateAll_Key[256]; GetHitKeyStateAll( GetHitKeyStateAll_Key ); for(int i=0;i<256;i++){ if(GetHitKeyStateAll_Key[i]==1) KeyStateBuf[i]++; else KeyStateBuf[i]=0; } return 0; } enum MapID { MAP_NONE = 1, MAP_WALL, MAP_FLOOR, MAP_SIDE, MAP_SWITCH_STEP, MAP_2ND_SIDE = 1<<4, MAP_2ND_BORDER_U = 2<<4, MAP_2ND_BORDER_R = 3<<4, MAP_2ND_BORDER_L = 4<<4, MAP_2ND_BORDER_D = 5<<4, MAP_2ND_CONNECT_RU = 6<<4, MAP_2ND_CONNECT_LU = 7<<4, MAP_2ND_CONNECT_RD = 8<<4, MAP_2ND_CONNECT_LD = 9<<4, MAP_2ND_CONNECT_CS = 10<<4, MAP_2ND_RU = 11<<4, MAP_2ND_FLOOR = 12<<4, MAP_2ND_LU = 13<<4, MAP_2ND_WAY = 14<<4, MAP_2ND_WAY_L = 15<<4, MAP_2ND_WAY_R = 16<<4, MAP_2ND_STEP = 17<<4, MAP_2ND_SWITCH_STEP = 18<<4, MAP_2ND_WALL = 19<<4 }; enum MoveID { MOVE_RIGHT, MOVE_LEFT, MOVE_UP, MOVE_DOWN, MOVE_NONE }; static int g_PosX = 2<<4; static int g_PosY = 2<<4; static int g_Layer = 0; static int g_ChangeLayer = 0; static MoveID g_MoveID = MOVE_NONE; int g_Green = 0; int g_Cyan = 0; int g_White = 0; int g_Yellow = 0; int g_Pink = 0; int CheckMove(std::vector>& t_MapVec, int t_X, int t_Y) { int result = -1; int t_Product = 0; if(g_Layer == 0){ t_Product = 7; }else if(g_Layer == 1){ t_Product = (31<<4); } switch(t_MapVec[t_Y][t_X]&t_Product){ case MAP_2ND_SWITCH_STEP: case MAP_SWITCH_STEP: result = 0; g_Layer ^= 1; g_ChangeLayer = 1; break; case MAP_2ND_WAY_R: case MAP_2ND_WAY_L: case MAP_2ND_WAY: case MAP_FLOOR: case MAP_2ND_FLOOR: case MAP_2ND_STEP: result = 0; g_ChangeLayer = 0; break; default:break; } if((g_Layer == 1 && (t_MapVec[t_Y][t_X]&7) == MAP_FLOOR) || (g_Layer == 0 && (t_MapVec[t_Y][t_X]&(31<<4)) == MAP_2ND_STEP)){ if(g_ChangeLayer == 1){ result = 0; g_Layer ^= 1; } } return(result); } void Move(std::vector>& t_MapVec){ if(g_MoveID == MOVE_NONE){ if(Key[KEY_INPUT_RIGHT] >= 1){ if( CheckMove( t_MapVec, (g_PosX>>4) + 1, g_PosY>>4) == 0){ g_MoveID = MOVE_RIGHT; } }else if(Key[KEY_INPUT_LEFT] >= 1){ if( CheckMove( t_MapVec, (g_PosX>>4) - 1, g_PosY>>4) == 0){ g_MoveID = MOVE_LEFT; } }else if(Key[KEY_INPUT_UP] >= 1){ if( CheckMove( t_MapVec, (g_PosX>>4), (g_PosY>>4) - 1) == 0){ g_MoveID = MOVE_UP; } }else if(Key[KEY_INPUT_DOWN] >= 1){ if( CheckMove( t_MapVec, (g_PosX>>4), (g_PosY>>4) + 1) == 0){ g_MoveID = MOVE_DOWN; } } } switch(g_MoveID){ case MOVE_RIGHT: g_PosX += 4; break; case MOVE_LEFT: g_PosX -= 4; break; case MOVE_UP: g_PosY -= 4; break; case MOVE_DOWN: g_PosY += 4; break; default:break; } if(((g_PosY & 15) == 0) && ((g_PosX & 15) == 0) ){ g_MoveID = MOVE_NONE; } } void DrawMap(std::vector>& t_MapVec){ for(int i = 0, count = (int)t_MapVec.size(); i < count; i++) { for(int j = 0, count2 = (int)t_MapVec[i].size(); j < count2; j++) { switch(t_MapVec[i][j]&7){ case MAP_NONE: DrawString(j<<4, i<<4, " ", g_Green);break; case MAP_WALL: DrawString(j<<4, i<<4, "■", g_Green);break; case MAP_FLOOR: DrawString(j<<4, i<<4, "×", g_Green);break; case MAP_SIDE: DrawString(j<<4, i<<4, "┃", g_White);break; case MAP_SWITCH_STEP: DrawString(j<<4, i<<4, "H", g_Yellow);break; default: break; } } } } void DrawMap2(std::vector>& t_MapVec){ for(int i = 0, count = (int)t_MapVec.size(); i < count; i++) { for(int j = 0, count2 = (int)t_MapVec[i].size(); j < count2; j++) { switch(t_MapVec[i][j]&(31<<4)){ case MAP_2ND_LU: DrawString(j<<4, i<<4, "┏", g_White);break; case MAP_2ND_RU: DrawString(j<<4, i<<4, "┓", g_White);break; case MAP_2ND_SIDE: DrawString(j<<4, i<<4, "┃", g_White);break; case MAP_2ND_BORDER_U: DrawString(j<<4, i<<4, "━", g_White);break; case MAP_2ND_BORDER_L: DrawString(j<<4, i<<4, "┣", g_White);break; case MAP_2ND_BORDER_R: DrawString(j<<4, i<<4, "┫", g_White);break; case MAP_2ND_BORDER_D: DrawString(j<<4, i<<4, "━", g_White);break; case MAP_2ND_WAY_L: DrawString(j<<4, i<<4, "┣", g_Cyan);break; case MAP_2ND_WAY_R: DrawString(j<<4, i<<4, "┫", g_Cyan);break; case MAP_2ND_WAY: DrawString(j<<4, i<<4, "━", g_Cyan);break; case MAP_2ND_FLOOR: DrawString(j<<4, i<<4, "×", g_White);break; case MAP_2ND_STEP: DrawString(j<<4, i<<4, "H", g_White);break; case MAP_2ND_CONNECT_RU:DrawString(j<<4, i<<4, "┗", g_White);break; case MAP_2ND_CONNECT_LU:DrawString(j<<4, i<<4, "┛", g_White);break; case MAP_2ND_CONNECT_RD:DrawString(j<<4, i<<4, "┏", g_White);break; case MAP_2ND_CONNECT_LD:DrawString(j<<4, i<<4, "┓", g_White);break; case MAP_2ND_CONNECT_CS:DrawString(j<<4, i<<4, "╋", g_White);break; case MAP_2ND_WALL:DrawString(j<<4, i<<4, "×", g_Cyan);break; default: break; } } } } void CreateBridge(std::vector>& t_MapVec, int t_MinLength) { for(int i = 0, count = t_MapVec.size(); i < count; i += ((t_MinLength - 1)<<1)) { int t_SideCount = 0; for(int j = 0, count2 = t_MapVec[i].size(); j < count2; j += (t_MinLength - 1)) { if(i + 1 < count){ int t_RandY = rand() % (t_MinLength - 3) + 1; if( j + t_MinLength < count2 && (t_MapVec[i + 1 + t_RandY][j]&(31<<4)) == MAP_2ND_SIDE){ t_SideCount += 1; if((t_MapVec[i + t_RandY][j + 1]&(31<<4)) != 0){ t_RandY = 3; } if((t_MapVec[i + 1 + t_RandY][j + 1]&(31<<4)) == 0 && t_SideCount > 0 && (t_SideCount % 2 == 0)){ int t_CreateBridgeFlag = 0; for( int k = j + (t_MinLength - 1); k < count2; k += (t_MinLength - 1)){ if((t_MapVec[i][k]&(31<<4)) != 0){ t_CreateBridgeFlag = 1; } } if(t_CreateBridgeFlag == 1){ t_MapVec[i + 1 + t_RandY][j] = (t_MapVec[i + 1 + t_RandY][j]&7)|MAP_2ND_WAY_L; for( int k = j + 1; k < count2; k++){ if((t_MapVec[i + 1 + t_RandY][k]&(31<<4)) == MAP_2ND_SIDE){ t_SideCount += 1; j += (t_MinLength - 1); t_MapVec[i + 1 + t_RandY][k] = (t_MapVec[i + 1 + t_RandY][k]&7)|MAP_2ND_WAY_R; break; }else{ t_MapVec[i + 1 + t_RandY][k] = (t_MapVec[i + 1 + t_RandY][k]&7)|MAP_2ND_WAY; } } } } } } } } } void CreateStep(std::vector>& t_MapVec, int t_MinLength) { //階段を作る for(int i = 0, count = t_MapVec.size(); i < count; i+= (t_MinLength - 1)) { for(int j = 0, count2 = t_MapVec[i].size(); j < count2; j += (t_MinLength - 1)) { if(i + 1 < count && j + 1 < count2 && (t_MapVec[i][j + 1]&(31<<4)) != 0 && (t_MapVec[i + 1][j + 1]&(31<<4)) == 0){ int t_RandNum = 1; int t_StepPosX = j + 1; for(int k = j + 1; k < count2 && (t_MapVec[i][k]&(31<<4)) != 0 && (t_MapVec[i + 1][k]&(31<<4)) == 0; k++){ if((t_MapVec[i][k]&(31<<4)) == MAP_2ND_BORDER_R){ t_MapVec[i + 1][k] = MAP_SIDE; t_MapVec[i + 2][k] = MAP_SIDE; }else{ t_MapVec[i + 1][k] = MAP_2ND_WALL; t_MapVec[i + 2][k] = MAP_2ND_WALL; } t_RandNum += 1; } t_StepPosX = j + (rand() % (t_RandNum - 2)) + 1; t_MapVec[i][t_StepPosX] = (t_MapVec[i][t_StepPosX]&7)|MAP_2ND_STEP; t_MapVec[i + 1][t_StepPosX] = (t_MapVec[i + 1][t_StepPosX]&7)|MAP_2ND_STEP; t_MapVec[i + 2][t_StepPosX] = MAP_SWITCH_STEP|MAP_2ND_SWITCH_STEP; } if(i + 1 < count && (t_MapVec[i][j]&(31<<4)) != 0 && (t_MapVec[i + 1][j]&(31<<4)) == 0){ for(int k = j; k < count2 && (t_MapVec[i][k]&(31<<4)) != 0 && (t_MapVec[i + 1][k]&(31<<4)) == 0; k++){ if(k == j){ t_MapVec[i + 1][k] = MAP_SIDE; t_MapVec[i + 2][k] = MAP_SIDE; }else{ t_MapVec[i + 1][k] = MAP_2ND_WALL; t_MapVec[i + 2][k] = MAP_2ND_WALL; } } } } } for(int i = 0, count = t_MapVec.size(); i < count; i+= (t_MinLength - 1)) { for(int j = 0, count2 = t_MapVec[i].size(); j < count2; j += (t_MinLength - 1)) { if(i == 0){ for(int k = j; k < count2 && (t_MapVec[i][k]&(31<<4)) != 0; k++){ if( k > 0 && k + 1 < count2){ t_MapVec[i + 1][k] |= MAP_FLOOR; t_MapVec[i + 2][k] |= MAP_FLOOR; } } } if(i > 1){ for(int k = j; k < count2; k++){ if((t_MapVec[i][k]&(31<<4)) != 0 && k > 0 && k + 1 < count2 && (t_MapVec[i - 1][k]&(31<<4)) == 0 && i + 2 < count){ t_MapVec[i][k] |= MAP_FLOOR; t_MapVec[i + 1][k] |= MAP_FLOOR; t_MapVec[i + 2][k] |= MAP_FLOOR; } } } } } } void ModifyHill(std::vector>& t_MapVec, int t_MinLength) { for(int i = 0, count = t_MapVec.size(); i < count; i+= (t_MinLength - 1)) { for(int j = 0, count2 = t_MapVec[i].size(); j < count2; j += (t_MinLength - 1)) { if(j > 0 && j + 1 < count2){ for(int k = i + 1; k < count && (t_MapVec[k][j]&(31<<4)) == MAP_2ND_SIDE; k++){ if((t_MapVec[k][j + 1]&(31<<4)) != MAP_2ND_FLOOR || (t_MapVec[k][j - 1]&(31<<4)) != MAP_2ND_FLOOR){ break; } t_MapVec[k][j] = (t_MapVec[k][j]&7)|MAP_2ND_FLOOR; } } if(i > 0 && i + 1 < count){ for(int k = j + 1; k < count2 && (t_MapVec[i][k]&(31<<4)) == MAP_2ND_BORDER_D; k++){ if((t_MapVec[i + 1][k]&(31<<4)) != MAP_2ND_FLOOR || (t_MapVec[i - 1][k]&(31<<4)) != MAP_2ND_FLOOR){ break; } t_MapVec[i][k] = (t_MapVec[i][k]&7)|MAP_2ND_FLOOR; } } } } for(int i = 0, count = t_MapVec.size(); i < count; i+= (t_MinLength - 1)) { for(int j = 0, count2 = t_MapVec[i].size(); j < count2; j += (t_MinLength - 1)) { int t_Flag = 0; if((t_MapVec[i][j]&(31<<4)) == MAP_2ND_RU){ if(j + 1 < count2 && ((t_MapVec[i][j + 1]&(31<<4)) == MAP_2ND_BORDER_U)){ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_BORDER_U; } }else if((t_MapVec[i][j]&(31<<4)) == MAP_2ND_BORDER_L){ if( j - 1 > 0 && (t_MapVec[i][j - 1]&(31<<4)) == MAP_2ND_BORDER_U){ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_CONNECT_LU; }else if( i + 1 < count && (t_MapVec[i + 1][j]&(31<<4)) == MAP_2ND_SIDE){ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_SIDE; } }else if((t_MapVec[i][j]&(31<<4)) == MAP_2ND_BORDER_R){ if( j + 1 < count2 && (t_MapVec[i][j + 1]&(31<<4)) == MAP_2ND_BORDER_U){ if(i + 1 < count && (t_MapVec[i + 1][j]&(31<<4)) == MAP_2ND_SIDE ){ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_CONNECT_CS; }else{ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_CONNECT_RU; } }else if( j + 1 < count2 && (t_MapVec[i][j + 1]&(31<<4)) == MAP_2ND_BORDER_D){ if(i + 1 < count && (t_MapVec[i + 1][j]&(31<<4)) == MAP_2ND_SIDE){ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_CONNECT_LD; }else{ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_BORDER_D; } }else if( i + 1 < count && (t_MapVec[i + 1][j]&(31<<4)) == MAP_2ND_SIDE){ if(j - 1 > 0 && (t_MapVec[i][j - 1]&(31<<4)) == MAP_2ND_BORDER_D){ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_CONNECT_LD; }else{ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_SIDE; } }else if(i + 1 < count && (t_MapVec[i + 1][j]&(31<<4)) == MAP_2ND_FLOOR){ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_FLOOR; } }else if((t_MapVec[i][j]&(31<<4)) == MAP_2ND_SIDE){ if( j + 1 < count2 && (t_MapVec[i][j + 1]&(31<<4)) == MAP_2ND_BORDER_D){ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_CONNECT_RD; }else if( j - 1 > 0 && (t_MapVec[i][j - 1]&(31<<4)) == MAP_2ND_SIDE){ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_CONNECT_LD; } }else if((t_MapVec[i][j]&(31<<4)) == MAP_2ND_BORDER_D){ if( i + 1 < count && (t_MapVec[i + 1][j]&(31<<4)) == MAP_2ND_SIDE){ if( j + 1 < count2 && (t_MapVec[i + 1][j + 1]&(31<<4)) == MAP_2ND_FLOOR){ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_CONNECT_RU; }else{ t_MapVec[i][j] = (t_MapVec[i][j]&7)|MAP_2ND_CONNECT_RD; } } } } } } void CreateHill(std::vector>& t_MapVec, int t_PosX, int t_PosY, int t_Width, int t_Height) { for(int i = t_PosY, count = t_PosY + t_Height; i < count; i++) { for(int j = t_PosX, count2 = t_PosX + t_Width; j < count2; j++) { if((t_MapVec[i][j] & (31<<4)) == 0){ if(i == t_PosY){ if(j == t_PosX){ t_MapVec[i][j] = MAP_2ND_LU; }else if(j == count2 - 1){ t_MapVec[i][j] = MAP_2ND_RU; }else{ t_MapVec[i][j] = MAP_2ND_BORDER_U; } }else if(i == count - 1){ if(j == t_PosX){ t_MapVec[i][j] = MAP_2ND_BORDER_L; }else if(j == count2 - 1){ t_MapVec[i][j] = MAP_2ND_BORDER_R; }else{ t_MapVec[i][j] = MAP_2ND_BORDER_D; } }else{ if((j - t_PosX) % (count2 - t_PosX - 1) == 0 ){ t_MapVec[i][j] = MAP_2ND_SIDE; }else{ t_MapVec[i][j] = MAP_2ND_FLOOR; } } } } } } void DecideHillLength(std::vector>& t_MapVec, int t_SplitX, int t_SplitY, int t_MinLength) { for(int i = 0; i < t_SplitY; i+=2) { for(int j = 0; j < t_SplitX; j+=2) { if(j + 1 < t_SplitX && i + 1 < t_SplitY){ int t_Width = ((rand() % 2) + 1); int t_Height = ((rand() % 2) + 1); if(i == 0 && j + 2 >= t_SplitX){ t_Width = 1; t_Height = 1; }else if(i + 2 >= t_SplitY && j == 0){ t_Width = 1; t_Height = 1; } CreateHill( t_MapVec, j*(t_MinLength - 1), i*(t_MinLength - 1), t_Width*(t_MinLength - 1) + 1, t_Height*(t_MinLength - 1) + 1); } } } ModifyHill( t_MapVec, t_MinLength ); CreateStep( t_MapVec, t_MinLength ); CreateBridge( t_MapVec, t_MinLength ); } void Create2(std::vector>& t_MapVec, int t_MinLength) { if((int)t_MapVec.size() > 0 && t_MinLength >= 6){ int t_Width = t_MapVec[0].size(); int t_Height = t_MapVec.size(); int t_SplitX = t_Width / (t_MinLength - 1); int t_SplitY = t_Height / (t_MinLength - 1); for(int i = 0; i < t_Height; i++) { for(int j = 0; j < t_Width; j++) { if(i % (t_Height - 1) == 0 || j % (t_Width - 1) == 0){ t_MapVec[i][j] = MAP_WALL; }else{ t_MapVec[i][j] = MAP_FLOOR; } } } DecideHillLength(t_MapVec, t_SplitX, t_SplitY, t_MinLength); } } void Create(std::vector>& t_MapVec, int t_Width, int t_Height, int t_MinLength) { if(t_Width >= 6 && t_Height >= 6 && t_MinLength >= 6){ int t_SplitX = t_Width / (t_MinLength - 1); int t_SplitY = t_Height / (t_MinLength - 1); t_Width = t_SplitX * (t_MinLength - 1) + 1; t_Height = t_SplitY * (t_MinLength - 1) + 1; t_MapVec.resize(t_Height); for(int i = 0; i < t_Height; i++) { t_MapVec[i].resize(t_Width); for(int j = 0; j < t_Width; j++) { if(i % (t_Height - 1) == 0 || j % (t_Width - 1) == 0){ t_MapVec[i][j] = MAP_WALL; }else{ t_MapVec[i][j] = MAP_FLOOR; } } } DecideHillLength(t_MapVec, t_SplitX, t_SplitY, t_MinLength); } } const int MAP_WIDTH = 60; const int MAP_HEIGHT = 40; const int MIN_LENGTH = 6; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){ ChangeWindowMode( TRUE ); if(DxLib_Init() == -1 ) return -1; SetDrawScreen( DX_SCREEN_BACK ); SetGraphMode(1024, 768, 32); SetWindowPosition( 100, 32 ); srand((unsigned int)time(NULL)); std::vector> t_MapVec; Create(t_MapVec, MAP_WIDTH, MAP_HEIGHT, MIN_LENGTH); g_Green = GetColor(0, 180, 0); g_Cyan = GetColor(0, 255, 255); g_White = GetColor(255, 255, 255); g_Yellow = GetColor(255, 255, 0); g_Pink = GetColor(255, 100, 255); while(ProcessMessage()==0 && ClearDrawScreen()==0 && GetHitKeyStateAll_2(Key)==0 ){ Move(t_MapVec); //レイヤー1描画 DrawMap(t_MapVec); //1階に居るなら、レイヤー1の後に主人公描画 if(g_Layer == 0){ DrawString( g_PosX, g_PosY, "■", g_Pink); } //レイヤー2描画 DrawMap2(t_MapVec); //2階に居るなら、レイヤー2の後に主人公描画 if(g_Layer == 1){ DrawString( g_PosX, g_PosY, "■", g_Pink); } if( Key[KEY_INPUT_Z] == 1){ //マップを書き換える関数。 Create2(t_MapVec, MIN_LENGTH); } ScreenFlip(); } DxLib_End(); return 0; }