Unity3D消消乐实现原理
生活随笔
收集整理的這篇文章主要介紹了
Unity3D消消乐实现原理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基本原理:
??? 遍歷所有寶石集合,判斷行和列三連的情況,刪除三連,更新上方寶石位置.
1.定義一個寶石腳本,功能:隨機指定寶石類型,指定寶石位置,iTween動畫,銷毀,顏色
using UnityEngine; using System.Collections;public class Gemstone : MonoBehaviour {public float xOffset = -4.0f; //x方向的偏移public float yOffset = -1.5f; //y方向的偏移public int rowIndex = 0; //行號public int columnIndex = 0; //列號public GameObject[] gemstoneBgs; //寶石(gemstone)的數組public int gemstoneType; //寶石(gemstone)的類型public GameController gameController;public SpriteRenderer spriteRenderer;public bool isSelected{set{if(value){spriteRenderer.color = Color.red;}else{spriteRenderer.color = Color.white;}}}private GameObject gemstoneBg;// Use this for initializationvoid Awake(){}void Start () {gameController = GameObject.Find ("GameController").GetComponent<GameController> ();spriteRenderer = gemstoneBg.GetComponent<SpriteRenderer> ();}// Update is called once per framevoid Update () {}public void UpdatePosition(int _rowIndex,int _columnIndex){ //調整gemstone(寶石)的位置rowIndex = _rowIndex;columnIndex = _columnIndex;this.transform.position = new Vector3 (columnIndex*1.2f + xOffset, rowIndex*1.2f + yOffset, 0);}public void TweenToPostion(int _rowIndex,int _columnIndex){rowIndex = _rowIndex;columnIndex = _columnIndex;iTween.MoveTo (this.gameObject, iTween.Hash ("x",columnIndex * 1.2f + xOffset,"y",rowIndex*1.2f + yOffset,"time",0.3f));}public void RandomCreateGemstoneBg(){ //生成隨機的寶石類型if (gemstoneBg != null)return;gemstoneType = Random.Range (0, gemstoneBgs.Length);gemstoneBg = Instantiate(gemstoneBgs[gemstoneType]) as GameObject;gemstoneBg.transform.parent = this.transform;}//鼠標點擊的回調方法public void OnMouseDown(){gameController.Select (this);}public void Dispose(){Destroy (this.gameObject);Destroy (gemstoneBg.gameObject);gameController = null;} } 腳本掛在到一個空的預制體上,所有類型的寶石��這個空物體生成,將所有的寶石預制體拖到集合目錄下
2.定義管理腳本
聲明一下字段:
Start里面初始化;
定義如下方法:
/// <summary>/// 生成寶石的方法/// </summary>/// <returns>返回一個定義好的寶石.</returns>/// <param name="rowIndex">Row 行坐標.</param>/// <param name="columnIndex">Column 列坐標.</param>public Gemstone AddGemstone(int rowIndex,int columnIndex){ //生成寶石//克隆一個寶石Gemstone c = Instantiate (gemstone) as Gemstone;c.transform.parent = this.transform;//指定父物體c.GetComponent<Gemstone>().RandomCreateGemstoneBg();//調用寶石本身上寶石腳本的隨機指定寶石類型的方法c.GetComponent<Gemstone>().UpdatePosition (rowIndex,columnIndex);//調用借用行號和列號自動排列(指定位置)的方法return c;}/// <summary>/// 查找行號和列好對應寶石的方法/// </summary>/// <returns>The gemstone.</returns>/// <param name="rowIndex">Row index.</param>/// <param name="columnIndex">Column index.</param>public Gemstone GetGemstone(int rowIndex,int columnIndex){ //通過行號和列號,取得所對應位置的寶石ArrayList temp = gemstoneList [rowIndex] as ArrayList;Gemstone c = temp [columnIndex] as Gemstone;return c;}public void SetGemstone(int rowIndex,int columnIndex, Gemstone c){ //設置所對應列號和列號位置的寶石ArrayList temp = gemstoneList [rowIndex] as ArrayList;temp [columnIndex] = c;}//點選當前寶石的時候,指定當前寶石的color,如果當前寶石為空時,表示是第一次點選,那么顏色指定為紅色,如果不為空的話,表示是第二點選(即交換的對象)public void Select(Gemstone c){//Destroy (c.gameObject); //測試,讓所點擊的物體消失if (currentGemstone == null) {currentGemstone = c;currentGemstone.isSelected = true;return;}else{if( Mathf.Abs(currentGemstone.rowIndex - c.rowIndex) + Mathf.Abs(currentGemstone.columnIndex - c.columnIndex) == 1 ){//判斷是否挨著的StartCoroutine (ExangeAndMatches(currentGemstone,c));//開啟協程,實現寶石交換并且檢測是否匹配}else{GetComponent<AudioSource>().PlayOneShot(errorClip);}currentGemstone.isSelected = false;currentGemstone = null;}}IEnumerator ExangeAndMatches(Gemstone c1,Gemstone c2){ //實現寶石交換并且檢測是否匹配Exchange(c1,c2);//調用交換寶石的方法yield return new WaitForSeconds (0.5f);//等待0.5sif(CheckHorizontalMatches() || CheckVerticalMatches() ){//經過水平檢測和垂直檢測后,如若有成功匹配的RemoveMatches();//執行刪除寶石的方法}else{//如果沒有匹配的寶石,直接在交換回來 // Debug.Log ("沒有檢測到相同的寶石,交換回來!!");Exchange(c1,c2);}}/// <summary>/// 水平檢測的方法,返回一個布爾值/// </summary>/// <returns><c>true</c>, if horizontal matches was checked, <c>false</c> otherwise.</returns>bool CheckHorizontalMatches(){ //實現檢測水平方向的匹配bool isMatches = false;//遍歷列表中的所有寶石for (int rowIndex = 0; rowIndex < rowNum; rowIndex++) {for(int columnIndex =0; columnIndex < columnNum - 2; columnIndex++){//判斷相同行挨著的寶石的類型if((GetGemstone (rowIndex,columnIndex).gemstoneType == GetGemstone (rowIndex,columnIndex + 1).gemstoneType ) && (GetGemstone (rowIndex,columnIndex).gemstoneType == GetGemstone (rowIndex,columnIndex + 2).gemstoneType )){ // Debug.Log ("發現行相同的寶石");//將三聯的寶石放到三聯數組里面AddMatches (GetGemstone (rowIndex,columnIndex));AddMatches (GetGemstone (rowIndex,columnIndex + 1));AddMatches (GetGemstone (rowIndex,columnIndex + 2));isMatches = true;}} }return isMatches;}/// <summary>/// 垂直檢測的方法,同事也會返回一個布爾值/// </summary>/// <returns><c>true</c>, if vertical matches was checked, <c>false</c> otherwise.</returns>bool CheckVerticalMatches(){ //實現檢測垂直方向的匹配bool isMatches = false;//遍歷所有寶石for(int columnIndex = 0; columnIndex < columnNum; columnIndex++){for(int rowIndex = 0; rowIndex < rowNum -2 ;rowIndex++){//判斷相同列挨著的寶石的類型if((GetGemstone(rowIndex,columnIndex).gemstoneType == GetGemstone (rowIndex + 1,columnIndex).gemstoneType ) & (GetGemstone(rowIndex,columnIndex).gemstoneType == GetGemstone (rowIndex + 2,columnIndex).gemstoneType ) ){ // Debug.Log ("發現列相同的寶石");//將三聯的寶石放到三聯列表里AddMatches (GetGemstone (rowIndex,columnIndex));AddMatches (GetGemstone (rowIndex +1,columnIndex));AddMatches (GetGemstone (rowIndex +2,columnIndex));isMatches = true;}}}return isMatches;}void AddMatches(Gemstone c){if(matchesGemstone == null)matchesGemstone = new ArrayList();int index = matchesGemstone.IndexOf (c); //檢測該寶石是否已在數組當中if(index == -1){//如果沒有在三聯列表里,添加進去matchesGemstone.Add (c);}}/// <summary>/// 刪除三聯的寶石/// </summary>void RemoveMatches(){ //刪除匹配的寶石for(int i=0; i< matchesGemstone.Count; i++){//遍歷三聯的列表Gemstone c = matchesGemstone[i] as Gemstone;RemoveGemstone(c);//調用刪除寶石的方法}matchesGemstone = new ArrayList ();//重新更新三聯列表StartCoroutine (WaitForCheckMatchesAgain ());//開啟攜程,檢測是否還有因為銷毀三聯寶石以后還有自動匹配的三聯寶石,如果有直接銷毀}IEnumerator WaitForCheckMatchesAgain(){yield return new WaitForSeconds (0.5f);if (CheckHorizontalMatches () || CheckVerticalMatches ()) {RemoveMatches (); }}void RemoveGemstone(Gemstone c){ //刪除寶石//Debug.Log ("刪除寶石!");c.Dispose ();GetComponent<AudioSource>().PlayOneShot (match3Clip);for (int i=c.rowIndex +1; i<rowNum; i++) {//更新銷毀寶石對應列上方寶石的位置,下滑的動畫Gemstone temGemstone = GetGemstone (i,c.columnIndex);temGemstone.rowIndex --;SetGemstone(temGemstone.rowIndex,temGemstone.columnIndex,temGemstone);//temGemstone.UpdatePosition (temGemstone.rowIndex,temGemstone.columnIndex);temGemstone.TweenToPostion (temGemstone.rowIndex,temGemstone.columnIndex);}Gemstone newGemstone = AddGemstone (rowNum, c.columnIndex);//在空缺的位置添加寶石newGemstone.rowIndex--;SetGemstone (newGemstone.rowIndex, newGemstone.columnIndex, newGemstone);//newGemstone.UpdatePosition (newGemstone.rowIndex, newGemstone.columnIndex);newGemstone.TweenToPostion (newGemstone.rowIndex, newGemstone.columnIndex);}/// <summary>/// 交換寶石的方法/// </summary>/// <param name="c1">C1.</param>/// <param name="c2">C2.</param>public void Exchange(Gemstone c1,Gemstone c2){ //實現寶石之間交換位置GetComponent<AudioSource>().PlayOneShot (swapClip); //播放交換的聲音SetGemstone (c1.rowIndex, c1.columnIndex, c2);SetGemstone (c2.rowIndex, c2.columnIndex, c1);//交換c1,c2的行號int tempRowIndex;tempRowIndex = c1.rowIndex;c1.rowIndex = c2.rowIndex;c2.rowIndex = tempRowIndex;//交換c1,c2的列號int tempColumnIndex;tempColumnIndex = c1.columnIndex;c1.columnIndex = c2.columnIndex;c2.columnIndex = tempColumnIndex;//c1.UpdatePosition (c1.rowIndex, c1.columnIndex);//c2.UpdatePosition (c2.rowIndex, c2.columnIndex);//ITween動畫將兩個寶石移動到指定的行號和列號的位置上c1.TweenToPostion (c1.rowIndex, c1.columnIndex);c2.TweenToPostion (c2.rowIndex, c2.columnIndex);}} 將Controller腳本掛在到一個空物體上,添加Audio Source組件工程下載鏈接:
http://download.csdn.net/detail/gaoheshun/9900322
完整Unity3D工程消消樂升級版:
http://download.csdn.net/detail/gaoheshun/9900326
總結
以上是生活随笔為你收集整理的Unity3D消消乐实现原理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 开发之 根据坐标计算长度、角度
- 下一篇: torque配置