1-3. ํํ ์ค ์ ํฌ๊ฐ๋ฅํ ๋ฐฉ ํ์ ( ๊ฐฏ์ ๋ฐ ์์น, ์ต์ 6x3 )
์ ๊ฒ์์ ๊ฒฝ์ฐ 3x3 ๋์ด์ ์๊ตฐ์ด 3x3๋์ด์ ์ ๊ตฐ ํ๊ณ ์ธ์ฐ๋๊ฒ ์ ํฌํ๋ฉด์ด๋ผ
์ ํฌ๊ฐ ๊ฐ๋ฅํ ๋งต์ ๊ฒฝ์ฐ ์ต์ 6x3์ ํ๋๊ฐ ํ์ํฉ๋๋ค .๊ทธ๋์ 6x3์
BuildBlock์ด๋ผ๋๊ฒ while loop์์ ๋๋ฉด์ย
์ข ๋๋คํ๊ฒ ๊ฑธ์ด๊ฐ์๋ ์ข ๊ทธ ์์น์ ๋ฐ๋ฅ์ด ์์ผ๋ฉด ๊น๋ ํํ์
์ด์ ์ ๊น์๋์ ๋ฐ๋ฅํ์ผ์ย GroundBlocksArray์ด๋ ๋์ ์ ์ฅ๋ฉ๋๋ค.
์ด๋ง์ธ์ฆ์จ? ์ด๋์ ๋ค ์ฝ์ผ๋ฉด์ย
x, y ์์น์์ x + 5, ย x+2 ์์น๊น์ง ๋ฐ๋ฅ ํ์ผ์ด ๊น๋ ค์์ผ๋ฉด ์ ํฌ์ฉ๋ฐฉ์ผ๋ก ๋นผ๋ฉด ๋๋ค๋ ๋ง์!
๊ทผ๋ฐ ๋์ ๋ฐฉ์์๋ ๋ฐฉ๋ผ๋ฆฌ ์์น๊ฐ ๊ฒน์น ์๋ ์๊ฒ ์ง? ๊ทธ๋ฐ๊ฒ๋ ๋นผ์ค์ผํจ
์ฌํผ ์ฐ๋ฆฌ๋ ๊ทธ ํจ์์ด๋ฆ์ StoreBattleRoom()์ด๋ผ๊ณ ํ๊ฒ ์ต๋๋ค.
GroundBlockArray ์ฌ๊ธฐ์ ์๋ ๋ฐ๋ฅํ์ผ์ ๋ฃจํ๋๋ฉด์ ์ ํฌ๊ฐ๋ฅํ ๋ฐฉ์ ์ขํ๋จ ๊ธฐ์ค์ ( ํผ๋ด )์ ๋ฝ๋๊ฑฐ์์.
์ ๊ฐ๋ณต์กํ๋ ์์ค๋ฅผ ๋ถ์ฌ๋ฃ์ย
---- ์ ์ทจ์ ---
void Start() {
GroundBlocksArray.Add(gameObject);
Vector3 SpawnPoint = transform.position;
Vector3 StartPoint = new Vector3(SpawnPoint.x, SpawnPoint.y, SpawnPoint.z);
Vector3 EndPoint = BuildBlocks(numberOfGroundTilesToSpawn, SpawnPoint);
print( "์์์ง์ : " + StartPoint + ", " + EndPoint);
transform.name = levelName;
StoreBattleRoom();
}
private void StoreBattleRoom(){
// ํด๋น ์์น์ ์๋์ง ์๋์ง ์ฌ๋ฌ๋ฒ ์กฐํํด์ผ ๋๋ ๋์ ๋๋ฆฌ๋ฅผ ๋ง๋ญ๋๋ค.
// ํด๋น ํค๋ก ์ ๊ทผ์ ๊ฒฐ๊ณผ๊ฐ ์๋ ๊ฒฝ์ฐ ๊ฐ์ด ๋์ผ ์๋ ์์ผ๋ bool?๋ก ๋ง๋ญ๋๋ค.
Dictionary<Vector3, int> dicGroundExist = new Dictionary<Vector3, int>();
ArrayList listBattleRoom = new ArrayList();
Dictionary<Vector2, bool> isInRoom = new Dictionary<Vector2, bool>();
int len = GroundBlocksArray.Count;
GameObject tmpObj;
// ์์น ๋ง์ผ๋ก ํด๋น ์์น์ ๋ฐ๋ฅ์ด ๊น๋ ค์๋์ง ํ์ธํ ๋์ ๋๋ฆฌ๋ ๋ง๋ฌ
for(int i = 0; i < len; i++){
tmpObj = GroundBlocksArray[i] as GameObject;
dicGroundExist.Add(tmpObj.transform.position, i);
}
Vector3 tmpVector;
Vector2 tmpPivotRoom;
int tmpVal;
bool? tmpPivotExist;
Vector2 tmpNewPivotRoom;
List<Vector2> listNewRoomGround;
for(int i = 0; i < len; i++){
tmpObj = GroundBlocksArray[i] as GameObject;
tmpVector = tmpObj.transform.position;
bool isBattleRoom ย = true;
for(int y = 0; (y < 3) && isBattleRoom; ++y){
for(int x = 0; (x < 6) && isBattleRoom; ++x){
if( !dicGroundExist.ContainsKey(tmpVector+new Vector3(x,y,0)) ){
print("false!");
isBattleRoom = false;
}
}
}
print( "isBattleRoom : " + isBattleRoom + ", " + tmpVector);
if(isBattleRoom){ // ํ์ฌ ์์น tmpVector๊ฐ ์ ํฌ์ฉ ๋งต์ ๋ง๋ค๊ธฐ์ ์ ์ ํ ์์์ง์ ์ ( ๋ฐ๋ฅ์ด ๋น์นธ์ด ์๋ )
tmpPivotRoom = new Vector2(tmpVector.x, tmpVector.y);
listNewRoomGround = new List<Vector2>();
if(!isInRoom.ContainsKey(tmpPivotRoom)){
bool roomBoundColision = false;
for(int y = 0; y < 3; ++y){
for(int x = 0; x < 6; ++x){
tmpNewPivotRoom = new Vector2( tmpPivotRoom.x + x , tmpPivotRoom.y + y );
if( !isInRoom.ContainsKey(tmpNewPivotRoom) ){
listNewRoomGround.Add(tmpNewPivotRoom);
} else {
roomBoundColision = true;
}
}
}
if(!roomBoundColision){
listBattleRoom.Add(tmpPivotRoom);
for(int k = 0; k < listNewRoomGround.Count; ++k){
isInRoom.Add( listNewRoomGround[k], true );
}
}
}
}
}
int length = listBattleRoom.Count;
Vector3 tmpVector3 = new Vector3(0,0,0);
for(int i = 0; i < length; ++i){
tmpVector3.x = ((Vector2) listBattleRoom[i]).x;
tmpVector3.y = ((Vector2) listBattleRoom[i]).y;
tmpVector3.z = 0;
GameObject obj = (GameObject) GroundBlocksArray[dicGroundExist[tmpVector3]];
obj.GetComponent<SpriteRenderer>().color = new Color32(255,0,0,255);
}
}
---- ์ ์ทจ์ ( ํจ์ ๋ ) ---
ย C#์์๋ Dictionary์์ ํค๋ฅผ ๋ฃ์ด์ ๊ฐ์ ๋บ๋ ํด๋น ๊ฐ์ด ์์ผ๋ฉด ์๋ฌ๊ฐ ๋๋ฒ๋ฆฐ๋ค!
ย ์๋ฐ hashMap์ฒ๋ผ ์๊ฐํ๋ฉด ๋งํจ์ ๋ฐ๋ค์ ํค์์น๊ฒ ๋๋ ๊ฒ!
ย ContainsKey๋ก ๊ผญ ํ์ธํ๋๋ก ํ๋ค.
์ฌํผ ์์ค๋ฅผ ๊ฐ์์น์ฐ๊ณ ์ฌ์๋ฒํผ์ ๋๋ฅด๋ฉด
์ด๋ ๊ฒ ๋์จ๋ค ๋นจ๊ฐ์์ด ํผ๋ด์ธ๋ฐ 2์นธ์ด ์ฐ์ธก๋ฐฉํฅ ํน์ ์๋จ๋ฐฉํฅ์ผ๋ก ์ฐ๊ฒฐ๋์๋๊ฑด ํจ๋งํ๊ฒ์ ๋๋ค.
๋ฌธ์ ๋ ์ด๊ฒ ์์ค์ ๋ฌธ์ ๊ฐ ์๋๊ณ ์๋ณธ ์์ค ์ ๋ก๋์ ํผ์ดํฌ๊ฐ ๋ถ๋ฅธ ์์์,ย
๋ญ๊ฐ 2์นธ์ฉ ๋์ด์๋๊ฒ์ด ์ฌ์์น ์์ scene๋ทฐ์์ w๋ฅผ ๋๋ฅด๊ณ ๋นจ๊ฐ์นธ์ ์ ํํด๋ณด๋
๋ญ๊ฐ ์ฌ์์น ์๋ค. ํผ๋ด์ด ๊ธฐ๋ณธ ๊ฒ์์ค๋ธ์ ํธ๋ผ๋ฉด ํผ๋ด์ด bottom center๊ฐ ์๋๊ฑด๋ฐ?
๊ทธ๋์ ํฌ๊ธฐ์ง์ ์ธ tํค๋ฅผ ๋๋ฌ๋ดค๋ค.
ํผ์ดํฌ๋ค ใ ใ ใป! ์ฌ์ค ์ด๊ฑด ๋ฐ๋ฅ ํ์ผ ํ๋์๋ค! ๋ฐ๋ฅํ์ผ ํ๋๊ฐ 2x2 ์ ์ ๊ทธ๋ ค์ก๋ค.
์ฌ๊ธฐ์ ์์ฌํด๋ณผ ์ ์๋ ๊ฒ์ offset Distance์
๋๋ค. ๊ธฐ๋ณธ๊ฐ์ด 2์์ฃ .
๊ทธ๋์ 1๋ก ๋ฐ๊ฟ๋๋ค. ๊ทธ๋ผ ์ข๋ ์ด์ดํ ๊ทธ๋ ค์ง๋ ๊ฒ์ธ์ ๋ฐ๋ฅํ์ผ ํ๋๊ฐ 2x2์ ๊ทธ๋ ค์ง๋ ๊ฑด ํด๊ฒฐ์ด ์๋ฉ๋๋ค.
๊ทธ๋ ๋ค๋ฉด, ์ํ์ ์ฐพ์์ผ์ง!
ํ์ด์ด๋ผํค ์ฐฝ ( ๊ณ์ธต์ฐฝ )์ ๋ค์ด๊ฐ๋ฉด Ground(Clone)์ด๋ผ๊ณ ์๋ค์ ์๋ณธ ํ๋ฆฌํน์ ์ด๋ฆ์ด Ground ์ธ ๋ชจ์
Prefabsํด๋์ ๋ค์ด๊ฐ๋ฉด Ground๋ผ๋ ๋์ด ์์ต๋๋ค.
์ด๋์ด ์์ํ์ฃ .
SpriteRender๋ผ๊ณ ์คํ๋ผ์ดํธ๋ฅผ ๊ทธ๋ ค์ฃผ๋ ๋์ ๋๋ค.
์ด ์คํ๋ผ์ดํธ ๋ ๋๋ฌ์๋ ๋ญ๊ฐ ํฌ๊ธฐ๋ฅผ ๋ง์ง๋งํ
์ต์ ์ด ์๋ค์
๊ทผ๋ฐ ground๋ผ๋ ์คํ๋ผ์ดํธ๋ฅผ ์ฐ๋ค์ฌ ์ด๋์ด ์์ฌ์ค๋ฝ์ต๋๋ค.
Spritesํด๋์ ๋ค์ด๊ฐ๋ฉด ์ด ground๋ผ๋ ๋์ด ์์ต๋๋ค.
์ด๋์ ๋๋ธํด๋ฆญํด์ ๊ฟ๋ทฐ๋ก ์ฝ๋๋ค. ( ์ ๋ ๊ทธ๋ฆผํ์ผ ๋ทฐ์ด๊ฐ ๊ธฐ๋ณธ ๊ฟ๋ทฐ๋ผ์์, ๋ค๋ฅธ๋ถ์ ์ค์นํ ๋์ด๋ฃ์ผ์ธ์ฌ )
( ์ก์๋ค ์ด๋! )
์ด๋ฏธ์ง๋ 256 x 256์ธ๋ฐ Pixel Per Unit์ 128์ ํด? ์ด ์ ๋ง์๋!
Pixel Per Unit์ด ์ ๋ํฐ ์ขํ๊ณ์์ ๊ธธ์ด 1๋งํผ์ ๋ชํฝ์ ๊ทธ๋ฆด๊ฑฐ๋์ด๊ฑด๋ฐ ์ด๋ ๊ฒ ํด๋์ผ๋
128x128 ํฝ์ ์ ์ ๋ํฐ ์ขํ๊ณ 1x1 ์ฌ๊ฐํ์ผ๋ก ๊ทธ๋ฆฌ๋ 256 x 256์ 2x2๊ฐ ๋ ์๋ฐ์ ์์ฃ ..
๊ทธ๋ผ 128์ 256์ผ๋ก ๋ฐ๊พธ๊ณ Apply๋ฒํผ์ ๋๋ฌ์.
๊ทธ๋ฆฌ๊ณ ์ฌ์ํ๋ฉด
๊ทผ๋ฐ ์ ์ง ๊ฒฝ๊ณ๊ฐ ์ ์๋ณด์ด๋ ํ์คํ ๊ตฌ๋ถ์ด ์๊ฐ๋๋ค.
->||<------ ์ด ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ฐ์์ ์ ์๋ณด์ด๊ฒ ์ง๋ง ์์.
์ด๊ฑธ ๊ฒ์ฐฝ์ผ์ ๋์ผ๋ฉด ์ฐ์น์ธ๋ฐ....
์ฌ์ง ์ ๋ก๋๊ฐ ์๋์ ๋ค์ํธ์
๋๋ ์ธ์ ๊ฐ ์ด๋ฐ๊ฑฐ ์์๋ฃ๊ณ ์ธ ๋ ์ด ์ค๊ฒ ์ง??
์์ ์ด๋์ ๋งํ