実現したいこと
ここに実現したいことを箇条書きで書いてください。
- エリアを分割してオブジェクトの中に入れたい
- 旧エリアと新エリアに分けてオブジェクトの中に入れる
該当のソースコード
javascript
1(function(){ 2'use strict'; 3 4const CHUNK_RANGE = 8; 5const CHUNK_SPLIT_MIN = 4; 6var tiles = []; 7 8 let getRandom = (min, max) => { 9 min = Math.ceil(min); 10 max = Math.floor(max); 11 return Math.floor(Math.random() * (max - min) + min); 12 }; 13 14 let setTile = (x, y, tileId) => { 15 try{ 16 tiles[x + ', ' + y] = tileId; 17 }catch{ 18 //ignored 19 } 20 }; 21 22 let getTile = (x, y) => { 23 try{ 24 return tiles[x + ', ' + y]; 25 }catch{ 26 return false; 27 } 28 }; 29 30 let initTiles = (xChunkLoop, yChunkLoop) => { 31 for(let x = 0; x < CHUNK_RANGE * xChunkLoop; x++) { 32 for(let y = 0; y < CHUNK_RANGE * yChunkLoop; y++) { 33 setTile(x, y, 0); 34 } 35 } 36 }; 37 38 let generatePass = (xPos, yPos, direction, length = 0) => { 39 let i = 0; 40 switch(direction){ 41 case 0: 42 for(; i < length; i++) setTile(xPos, yPos++, 1); 43 break; 44 45 case 1: 46 for(; i < length; i++) setTile(xPos++, yPos, 1); 47 break; 48 49 case 2: 50 for(; i < length; i++) setTile(xPos, yPos--, 1); 51 break; 52 53 case 3: 54 for(; i < length; i++) setTile(xPos--, yPos, 1); 55 break; 56 } 57 }; 58 59 let generateRoom = (xPos, yPos, width, height) => { 60 width = width / 2 | 0; 61 height = height / 2 | 0; 62 63 for(let x = -width + xPos; x <= width + xPos; x++) { 64 for(let y = -height + yPos; y <= height + yPos; y++) { 65 setTile(x, y, 1); 66 } 67 } 68 }; 69 70 var chunks = []; 71 72 let initChunk = (xChunkLoop, yChunkLoop) => { 73 let chunk = { 74 x:0, 75 y:0, 76 w:xChunkLoop, 77 h:yChunkLoop 78 }; 79 chunks = [chunk]; 80 return chunks; 81 }; 82 83 let splitDangeon = (oldChunk, count = 0) => { 84 if(Math.random() > 0.5) {//縦 85 let splitX = CHUNK_SPLIT_MIN; 86 for(let i = 0; Math.random() > i; i += 0.1) splitX++; 87 let width = oldChunk.w - splitX; 88 if(width > CHUNK_SPLIT_MIN){ 89 chunks[count + 1] = {//new chunk 90 x:oldChunk.x, 91 y:oldChunk.y, 92 w:splitX, 93 h:oldChunk.h 94 }; 95 96 chunks[count] = {//old chunk 97 x:splitX, 98 y:oldChunk.y, 99 w:width, 100 h:oldChunk.h 101 }; 102 } 103 } else {//横 104 let splitY = CHUNK_SPLIT_MIN; 105 for(let i = 0; Math.random() > i; i += 0.1) splitY++; 106 107 let height = oldChunk.h - splitY; 108 109 if(height > CHUNK_SPLIT_MIN){ 110 chunks[count] = {//old chunk 111 x:oldChunk.x, 112 y:splitY, 113 w:oldChunk.w, 114 h:height 115 }; 116 117 chunks[count + 1] = {//new chunk 118 x:oldChunk.x, 119 y:oldChunk.y, 120 w:oldChunk.w, 121 h:splitY 122 }; 123 } 124 } 125 count++; 126 if(count < 10) splitDangeon(chunks[count], count); 127 128 }; 129 130 let generateDangeon = () => { 131 132 }; 133 134 let debug = (xChunkLoop, yChunkLoop) => { 135 initTiles(xChunkLoop, yChunkLoop); 136 137 generateRoom(10, 41, 5, 11); 138 139 let map = []; 140 for(let x = 0; x < xChunkLoop * CHUNK_RANGE; x++){ 141 if(x % (xChunkLoop * CHUNK_RANGE) != 0) { 142 map.push('\n'); 143 } 144 for(let y = 0; y < yChunkLoop * CHUNK_RANGE; y++) { 145 switch(tiles[x + ', ' + y]) { 146 case 0: 147 map.push('■'); 148 break; 149 case 1: 150 map.push('□'); 151 break; 152 } 153 } 154 } 155 console.log(map.join('')); 156 console.log('\n'); 157 }; 158 159 var debugChunks = []; 160 161 let debugChunk = (xChunkLoop, yChunkLoop) => { 162 splitDangeon(initChunk(xChunkLoop, yChunkLoop)[0]); 163 for(let chunk in chunks) { 164 for(let xChunk = chunks[chunk].x; xChunk <= chunks[chunk].w; xChunk++) { 165 for(let yChunk = chunks[chunk].y; yChunk <= chunks[chunk].h; yChunk++) { 166 /* 167 let chunkWidth = xChunk + chunks[chunk].x; 168 let chunkHeight = yChunk + chunks[chunk].y; 169 debugChunks[chunkWidth + ', ' + chunkHeight] = chunk;//x, yに入れ直す 170 */ 171 } 172 } 173 } 174 console.log(chunks); 175 console.log('\n'); 176 }; 177 178 debugChunk(16, 16); 179})();

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
退会済みユーザー
2022/09/14 13:07