유니티3 블루프린트. 쳅터 2 코드 수정 2.

책 내용에 가장 중요한 부분.
블럭의 렌덤 배열 내용이 없더라.

고민 고민 해서. 셔플로 렌덤배열 로직 만들어서 게임오브젝트의 리스트 배치를 레덤하게 처리 하려고 했으나... 코드만 복잡 하고...

회사 형일씨 도움으로...

아래와 같이 구현.
var numberOfTiles:int  = 16;
var tileObject:GameObject[];
var tileLocations = new Array (
 Vector3(0,4.5,0), Vector3(1.5,4.5,0), Vector3(3,4.5,0), Vector3(4.5,4.5,0),
 Vector3(0,3,0),  Vector3(1.5,3,0), Vector3(3,3,0),   Vector3(4.5,3,0),
 Vector3(0,1.5,0), Vector3(1.5,1.5,0), Vector3(3,1.5,0), Vector3(4.5,1.5,0),
 Vector3(0,0,0),  Vector3(1.5,0,0), Vector3(3,0,0),   Vector3(4.5,0,0)
);

var matchOne : GameObject;
var tileName1: String; //Find clicked block name value
var tName1 : Array;

var matchTwo : GameObject;
var tileName2: String; //Find clicked block name value
var tName2 : Array;

var hit : RaycastHit; //RaycastHit Struct Structure used to get information back from a raycast.

var canClick = true;

//public var speed:int = 0.1;
//var to : Transform;
//var from : Transform;

//setup to 16 tiles object TileLocation use vector3 function
// each tile has difference 1.5 distanse position 


function RamdomSort() //소트 범위 안에서 차순 비교 
{
 return Random.Range( 0f, 1f ) > 0.5f ? 1 : -1;
}

function Start () 
{
 Camera.main.transform.position = Vector3(2.25,2.25,-20);
 
 tileLocations.sort( RamdomSort ); // 타일 로케이션 배열 인덱스를 렌덤소트로 섞는다.

 for(var i:int = 0; i < numberOfTiles; i++)
 {
  
  //Instanse copy to tileObject ans set to location
  Instantiate (tileObject[i], tileLocations[i], Quaternion.identity);

 }
 

}


function RevealCardOne()

 {
  matchOne=hit.transform.gameObject;
  tileName1=matchOne.transform.parent.name;  
  
  if (matchOne == null)
  {
   print("Object Not Find");
  }
   
  else
  {
   tName1=tileName1.Split("_"[0]);
   //bName1=tileName1("");
   print(tName1[0]);
   
   //Ratating clicking Block to Y axis 180 degree.
   matchOne.transform.Rotate(Vector3(0,180,0));
  }
 
 }
 
 
function RevealCardTwo()
 {
  matchTwo=hit.transform.gameObject;
  tileName2=matchTwo.transform.parent.name;  
  
  print("tileName2");
  
  if (matchTwo == null)
  {
   
   print("Object Not Find");
  }
  else
  {
   tName2=tileName2.Split("_"[0]);
   print(tName2[0]);
   
   //Ratating clicking Block to Y axis 180 degree.
   matchTwo.transform.Rotate(Vector3(0,180,0));
  }
  

  
  
  //Check to Your Match Block
  if(tName1[0]==tName2[0])// If Your First Clicked Block name and Second Clicked Block Name is Same!
  {
   if(tileName1 == tileName2)//if you are click to same object double chance.It code can not destroy this block.
   {
    print("****Click to Same Block!*****");
   }
   else
   {
    print("****Click to Other Match Block!*****");
    canClick = true;
    yield new WaitForSeconds (1);
    print("block Match!");
    Destroy (matchOne);
    Destroy (matchTwo);
    numberOfTiles = numberOfTiles -2;
   }
   
   if (numberOfTiles == 0)
   {
    print("GameEnd");
   }
  }
  else
  {
   canClick = false;
   
   print("block Not Match! Try Again");
   yield new WaitForSeconds (1);
   matchOne.transform.Rotate(Vector3(0,-180,0));
   matchTwo.transform.Rotate(Vector3(0,-180,0));
   canClick = true;
  }
  matchOne=null;
  matchTwo=null;
  
//  if(tileName1 != tileName2)
//  {
//   print("*************************");
//  }
  
  
  
 }




function Update () 
{
 if(canClick == true)
 {
  var ray = Camera.main.ScreenPointToRay(Input.mousePosition); //Ray to Camera Screen posiont attach to mouse position.
  if (Input.GetButtonDown("Click1"))
  {
   if(Physics.Raycast(ray,hit,Mathf.Infinity))
   {
    if(!matchOne)
    {
     RevealCardOne();
    }
    else
    {
     RevealCardTwo();
    }
   }
  }
 }


}

Game Developer Leegoon copyright all right reserved since 2010.

Comments