Unity3D_(游戲)甜品消消樂01_游戲基礎界面


 

甜品消消樂游戲

 

   (腳本源碼在游戲UI設計最下方)

  

  三消游戲,存在L型消除有一點小Bug,當甜品在餅干附近消除時會清除餅干

  餅干作為游戲障礙物時不可移動的,多塊餅干並排時會擋住甜品掉落

  發現消除類游戲的算法還是比較復雜的

  游戲小道具因算法繞的我頭有點暈就沒有實現

  甜品掉落速度可以在GameManager游戲管理類上設置fill Time值(我這里是0.25)

  emm,游戲開始界面有點low,未添加渲染動畫

 

游戲項目已托管到Github上  傳送門

 

甜品消消樂01_游戲基礎界面  傳送門

甜品消消樂02_游戲核心算法  傳送門

甜品消消樂03_游戲UI設計     傳送門

 

 

 (文章最下邊有游戲腳本) 

 

 

實現過程

 

 

素材導入,基本預制體的制作

 

  通過Gizmos影藏Scene場景中小攝像機

  (點擊攝像機進行關閉)

 

  新建一個GameObject->New Sprite,甜甜圈拖動上Sprite上,將游戲分層開發(分為模型層和渲染層)

 

  當不知道甜甜圈尺寸時,可以新建一個3D Object->Cube,Cube默認長寬比例為1m

  將甜甜圈長寬縮放比例0.45

  將甜甜圈做成預設體

 

  制作格子背景

  設置格子縮放比例為0.65

 

  為設置格子背景在甜甜圈背面,將Gird背景的Order in Layout設置為-1

  將Gird設置成預設體

  (不需要用到Cube我就把它刪除了)

 

 

游戲管理的創建,巧克力的生成

 

  新建一個GameObject->New Sprite,制作游戲背景

   (背景比攝像機范圍稍微大一些,放曝光)

  

  添加GameManager游戲管理類腳本,掛在到GameObject(GameManager)上

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    //單例實例化
    private static GameManager _instance;
    public static GameManager Instance
    {
        get
        {
            return _instance;
        }

        set
        {
            _instance = value;
        }
    }

    //大網格的行列數
    public int xColumn;
    public int yRow;

    public GameObject gridPrefab;

    private void Awake()
    {
        _instance = this;
    }

    // Use this for initialization
    void Start () {
       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,new Vector3(x,y,0),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}
GameManager.cs

 

  巧克力塊游戲背景

  大網格的行列數

    public int xColumn;
    public int yRow;

 

    public GameObject gridPrefab;

    void Start () {
       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,new Vector3(x,y,0),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }
    }

 

 

  (為了使背景在巧克力塊后面,設置background的Order in Layout 為 -2)

 

  實際需要實例化巧克力的X位置 = GameManager位置的X坐標-大網格長度的一半+行列對應的X坐標

  實際需要實例化巧克力的Y位置 = GameManager位置的Y坐標-大網格長度的一半+行列對應的Y坐標

  

  調整巧克力塊背景

    public Vector3 CorrectPosition(int x ,int y)
    {
        //實際需要實例化巧克力的X位置 = GameManager位置的X坐標-大網格長度的一半+行列對應的X坐標
        // 實際需要實例化巧克力的Y位置 = GameManager位置的Y坐標-大網格長度的一半+行列對應的Y坐標
        return new Vector3(transform.position.x-xColumn/2f+x,transform.position.y+yRow/2f-y);
    }

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    //單例實例化
    private static GameManager _instance;
    public static GameManager Instance
    {
        get
        {
            return _instance;
        }

        set
        {
            _instance = value;
        }
    }

    //大網格的行列數
    public int xColumn;
    public int yRow;

    public GameObject gridPrefab;

    private void Awake()
    {
        _instance = this;
    }

    // Use this for initialization
    void Start () {
       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    public Vector3 CorrectPosition(int x ,int y)
    {
        //實際需要實例化巧克力的X位置 = GameManager位置的X坐標-大網格長度的一半+行列對應的X坐標
        // 實際需要實例化巧克力的Y位置 = GameManager位置的Y坐標-大網格長度的一半+行列對應的Y坐標
        return new Vector3(transform.position.x-xColumn/2f+x,transform.position.y+yRow/2f-y);
    }

}
GameManager.cs

 

 

甜品的類型枚舉,結構體,字典的創建

 

  甜品的種類

    public enum SweetsType
    {
        EMPTY,
        NORMAL,
        BARRIER,
        ROE_CLEAR,
        COLUMN_CLEAR,
        RAINBOWCANDY,
        COUNT   //標記類型
    }

 

  甜品預制體的字典,我們可以通過甜品的種類來得到對應的甜品游戲物體

    public Dictionary<SweetsType, GameObject> sweetPrefabDict;

    [System.Serializable]
    public struct SweetPrefab
    {
        public SweetsType type;
        public GameObject prefab;
    }

    public SweetPrefab[] sweetPrefabs;

 

 

  字典的實例化

        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();
        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

 

 

甜品的生成

 

  甜品數組

 private GameObject[,] sweets;

 

void Start () {

        //字典的實例化
        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();

        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (!sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }

        sweets = new GameObject[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                sweets[x,y] = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x, y), Quaternion.identity);
                sweets[x,y].transform.SetParent(transform);
            }
        }

    }

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameSweet : MonoBehaviour {

    private int x;

    public int X
    {
        get
        {
            return x;
        }

        set
        {
            x = value;
        }
    }

    private int y;

    public int Y
    {
        get
        {
            return y;
        }

        set
        {
            y = value;
        }
    }

    private GameManager.SweetsType type;

    public GameManager.SweetsType Type
    {
        get
        {
            return type;
        }
    }

    public GameManager gameManager;

    public void Init(int _x,int _y,GameManager _gameManager,GameManager.SweetsType _type)
    {
        x = _x;
        y = _y;
        gameManager = _gameManager;
        type = _type;
    }


}
GameSweet.cs 管理甜甜圈腳本

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    //甜品的種類
    public enum SweetsType
    {
        EMPTY,
        NORMAL,
        BARRIER,
        ROE_CLEAR,
        COLUMN_CLEAR,
        RAINBOWCANDY,
        COUNT   //標記類型
    }

    //甜品預制體的字典,我們可以通過甜品的種類來得到對應的甜品游戲物體
    public Dictionary<SweetsType, GameObject> sweetPrefabDict;

    [System.Serializable]
    public struct SweetPrefab
    {
        public SweetsType type;
        public GameObject prefab;
    }

    public SweetPrefab[] sweetPrefabs;

    //單例實例化
    private static GameManager _instance;
    public static GameManager Instance
    {
        get
        {
            return _instance;
        }

        set
        {
            _instance = value;
        }
    }

    //大網格的行列數
    public int xColumn;
    public int yRow;

    public GameObject gridPrefab;

    //甜品數組
    private GameSweet[,] sweets;

    private void Awake()
    {
        _instance = this;
    }

    // Use this for initialization
    void Start () {

        //字典的實例化
        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();

        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (!sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }

        sweets = new GameSweet[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
               GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x, y), Quaternion.identity);
               newSweet.transform.SetParent(transform);

                sweets[x,y] = newSweet.GetComponent<GameSweet>();
                sweets[x, y].Init(x, y, this, SweetsType.NORMAL);
            }
        }

    }
    
    // Update is called once per frame
    void Update () {
        
    }

    public Vector3 CorrectPosition(int x ,int y)
    {
        //實際需要實例化巧克力的X位置 = GameManager位置的X坐標-大網格長度的一半+行列對應的X坐標
        // 實際需要實例化巧克力的Y位置 = GameManager位置的Y坐標-大網格長度的一半+行列對應的Y坐標
        return new Vector3(transform.position.x-xColumn/2f+x,transform.position.y+yRow/2f-y);
    }

}
GameManager.cs

 

 

甜品移動

 

  添加甜品移動腳本

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovedSweet : MonoBehaviour {

    private GameSweet sweet;

    private void Awake()
    {
        sweet = GetComponent<GameSweet>();
    }

    public void Move(int newX,int newY)
    {
        sweet.X = newX;
        sweet.Y = newY;
        sweet.transform.localPosition = sweet.gameManager.CorrectPosition(newX, newY);
    }
}
MovedSweet.cs

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameSweet : MonoBehaviour {

    private int x;
    private int y;
    public int X
    {
        get
        {
            return x;
        }

        set
        {
            if (CanMove())
            {
                x = value;
            }
        }
    }
    public int Y
    {
        get
        {
            return y;
        }

        set
        {
            if (CanMove())
            {
                y = value;
            }
        }
    }

    private GameManager.SweetsType type;
    public GameManager.SweetsType Type
    {
        get
        {
            return type;
        }
    }


    [HideInInspector]
    public GameManager gameManager;

    public MovedSweet MovedComponet
    {
        get
        {
            return movedComponet;
        }
    }
    private MovedSweet movedComponet;
    
    //判斷甜品是否可以移動
    public bool CanMove()
    {
        return movedComponet != null;
    }

    public void Init(int _x,int _y,GameManager _gameManager,GameManager.SweetsType _type)
    {
        x = _x;
        y = _y;
        gameManager = _gameManager;
        type = _type;
    }


}
GameSweet.cs

 

  判斷甜品是否可以移動

    public bool CanMove()
    {
        return movedComponet != null;
    }

 

    private int x;
    private int y;
    public int X
    {
        get
        {
            return x;
        }

        set
        {
            if (CanMove())
            {
                x = value;
            }
        }
    }
    public int Y
    {
        get
        {
            return y;
        }

        set
        {
            if (CanMove())
            {
                y = value;
            }
        }
    }

 

 

移動的測試

  

  測試甜品移動到Vector.zero位置處

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovedSweet : MonoBehaviour {

    private GameSweet sweet;

    private void Awake()
    {
        sweet = GetComponent<GameSweet>();
    }

    public void Move(int newX,int newY)
    {
        sweet.X = newX;
        sweet.Y = newY;
        sweet.transform.localPosition = sweet.gameManager.CorrectPosition(newX, newY);
    }
}
MovedSweet.cs

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameSweet : MonoBehaviour {

    private int x;
    private int y;
    public int X
    {
        get
        {
            return x;
        }

        set
        {
            if (CanMove())
            {
                x = value;
            }
        }
    }
    public int Y
    {
        get
        {
            return y;
        }

        set
        {
            if (CanMove())
            {
                y = value;
            }
        }
    }

    private GameManager.SweetsType type;
    public GameManager.SweetsType Type
    {
        get
        {
            return type;
        }
    }


    [HideInInspector]
    public GameManager gameManager;

    public MovedSweet MovedComponet
    {
        get
        {
            return movedComponet;
        }
    }
    private MovedSweet movedComponet;
    
    //判斷甜品是否可以移動
    public bool CanMove()
    {
        return movedComponet != null;
    }

    private void Awake()
    {
        movedComponet = GetComponent<MovedSweet>();
    }

    public void Init(int _x,int _y,GameManager _gameManager,GameManager.SweetsType _type)
    {
        x = _x;
        y = _y;
        gameManager = _gameManager;
        type = _type;
    }


}
GameSweet.cs

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    //甜品的種類
    public enum SweetsType
    {
        EMPTY,
        NORMAL,
        BARRIER,
        ROE_CLEAR,
        COLUMN_CLEAR,
        RAINBOWCANDY,
        COUNT   //標記類型
    }

    //甜品預制體的字典,我們可以通過甜品的種類來得到對應的甜品游戲物體
    public Dictionary<SweetsType, GameObject> sweetPrefabDict;

    [System.Serializable]
    public struct SweetPrefab
    {
        public SweetsType type;
        public GameObject prefab;
    }

    public SweetPrefab[] sweetPrefabs;

    //單例實例化
    private static GameManager _instance;
    public static GameManager Instance
    {
        get
        {
            return _instance;
        }

        set
        {
            _instance = value;
        }
    }

    //大網格的行列數
    public int xColumn;
    public int yRow;

    public GameObject gridPrefab;

    //甜品數組
    private GameSweet[,] sweets;

    private void Awake()
    {
        _instance = this;
    }

    // Use this for initialization
    void Start () {

        //字典的實例化
        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();

        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (!sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }

        sweets = new GameSweet[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                                                                                // CorrectPosition(x, y)
               GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL],Vector3.zero,Quaternion.identity);
               newSweet.transform.SetParent(transform);

                sweets[x,y] = newSweet.GetComponent<GameSweet>();
                sweets[x, y].Init(x, y, this, SweetsType.NORMAL);

               // if (sweets[x, y].CanMove())
                //{
                 //   sweets[x, y].MovedComponet.Move();
               // }
            }
        }

    }
    
    // Update is called once per frame
    void Update () {
        
    }

    public Vector3 CorrectPosition(int x ,int y)
    {
        //實際需要實例化巧克力的X位置 = GameManager位置的X坐標-大網格長度的一半+行列對應的X坐標
        // 實際需要實例化巧克力的Y位置 = GameManager位置的Y坐標-大網格長度的一半+行列對應的Y坐標
        return new Vector3(transform.position.x-xColumn/2f+x,transform.position.y+yRow/2f-y);
    }

}
GameManager.cs

 

 

 

甜品顏色

 

  添加腳本顏色腳本ColorSweet.cs

  通過更改Sweet下的Sprite來更改甜品的種類

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColorSweet : MonoBehaviour {

    public enum ColorType
    {
        YELLOW,
        PUPLE,
        RED,
        BLUE,
        GREEN,
        PNGK,
        ANY,
        COUNT
    }

    [System.Serializable]
    public struct ColorSprite
    {
        public ColorType color;
        public Sprite sprite;
    }

    public ColorSprite[] colorSprites;

    private Dictionary<ColorType, Sprite> colorSpriteDict;
}
ColorSweet.cs

 

public enum ColorType
    {
        YELLOW,
        PUPLE,
        RED,
        BLUE,
        GREEN,
        PNGK,
        ANY,
        COUNT
    }

 

  每一種顏色對應一種甜品類型

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    //甜品的種類
    public enum SweetsType
    {
        EMPTY,
        NORMAL,
        BARRIER,
        ROE_CLEAR,
        COLUMN_CLEAR,
        RAINBOWCANDY,
        COUNT   //標記類型
    }

    //甜品預制體的字典,我們可以通過甜品的種類來得到對應的甜品游戲物體
    public Dictionary<SweetsType, GameObject> sweetPrefabDict;

    [System.Serializable]
    public struct SweetPrefab
    {
        public SweetsType type;
        public GameObject prefab;
    }

    public SweetPrefab[] sweetPrefabs;

    //單例實例化
    private static GameManager _instance;
    public static GameManager Instance
    {
        get
        {
            return _instance;
        }

        set
        {
            _instance = value;
        }
    }

    //大網格的行列數
    public int xColumn;
    public int yRow;

    public GameObject gridPrefab;

    //甜品數組
    private GameSweet[,] sweets;

    private void Awake()
    {
        _instance = this;
    }

    // Use this for initialization
    void Start () {

        //字典的實例化
        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();

        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (!sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }

        sweets = new GameSweet[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                                                                                // CorrectPosition(x, y)
               GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL],Vector3.zero,Quaternion.identity);
               newSweet.transform.SetParent(transform);

                sweets[x,y] = newSweet.GetComponent<GameSweet>();
                sweets[x, y].Init(x, y, this, SweetsType.NORMAL);

                if (sweets[x, y].CanMove())
                {
                    sweets[x, y].MovedComponet.Move(x,y);
                }

                if (sweets[x, y].CanColor())
                {
                    sweets[x, y].ColorComponet.SetColor((ColorSweet.ColorType)(Random.Range(0, sweets[x, y].ColorComponet.NumColors)));
                }

            }
        }

    }
    
    // Update is called once per frame
    void Update () {
        
    }

    public Vector3 CorrectPosition(int x ,int y)
    {
        //實際需要實例化巧克力的X位置 = GameManager位置的X坐標-大網格長度的一半+行列對應的X坐標
        // 實際需要實例化巧克力的Y位置 = GameManager位置的Y坐標-大網格長度的一半+行列對應的Y坐標
        return new Vector3(transform.position.x-xColumn/2f+x,transform.position.y+yRow/2f-y);
    }

}
GameManager.cs

 

  當甜品可以移動時,甜品可以進行移動

                if (sweets[x, y].CanMove())
                {
                    sweets[x, y].MovedComponet.Move(x,y);
                }

 

  當甜品存在顏色使填充隨機色

                if (sweets[x, y].CanColor())
                {
                    sweets[x, y].ColorComponet.SetColor((ColorSweet.ColorType)(Random.Range(0, sweets[x, y].ColorComponet.NumColors)));
                }

 

sweets = new GameSweet[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                                                                                // CorrectPosition(x, y)
               GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL],Vector3.zero,Quaternion.identity);
               newSweet.transform.SetParent(transform);

                sweets[x,y] = newSweet.GetComponent<GameSweet>();
                sweets[x, y].Init(x, y, this, SweetsType.NORMAL);

                //當甜品可以移動時,甜品可以進行移動
                if (sweets[x, y].CanMove())
                {
                    sweets[x, y].MovedComponet.Move(x,y);
                }

                //當甜品存在顏色使填充隨機色
                if (sweets[x, y].CanColor())
                {
                    sweets[x, y].ColorComponet.SetColor((ColorSweet.ColorType)(Random.Range(0, sweets[x, y].ColorComponet.NumColors)));
                }

            }
        }

 

 

 

 

空甜品的生成

 

  創建空游戲物體預制體

 

  產生甜品的方法

    public GameSweet CreateNewSweet(int x,int y,SweetsType type)
    {
        GameObject newSweet =  Instantiate(sweetPrefabDict[type], CorrectPosition(x, y), Quaternion.identity);
        newSweet.transform.parent = transform;

        sweets[x, y] = newSweet.GetComponent<GameSweet>();
        sweets[x, y].Init(x,y,this,type);

        return sweets[x, y];
    }

 

void Start () {

        //字典的實例化
        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();

        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (!sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }

        sweets = new GameSweet[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                CreateNewSweet(x, y, SweetsType.EMPTY);
            }
        }

    }

 

 

 

填充核心算法

 

  填充全部甜品的方法

 public void AllFill()
    {
        while (Fill())
        {

        }
    }

 

  當甜品填充滿時返回true

    public bool Fill()
    {
        bool FilledNotFinshed = false;  //用來判斷本次是否完成

        //行遍歷
        for(int y=yRow-2;y>=0;y--)
        {
            for(int x=0;x<xColumn;x++)
            {
                GameSweet sweet = sweets[x, y]; //得到當前元素位置

                //如果無法移動,則無法往下填充
                if (sweet.CanMove())
                {
                    GameSweet sweetBelow = sweets[x, y + 1]; 

                    if(sweetBelow.Type == SweetsType.EMPTY)
                    {
                        sweet.MovedComponet.Move(x,y+1);
                        sweets[x, y + 1] = sweet;
                        CreateNewSweet(x, y, SweetsType.EMPTY);
                        FilledNotFinshed = true;
                    }
                }
            }

        }
        //最上排的特殊情況
        for (int x = 0; x < xColumn; x++)
           {
               GameSweet sweet = sweets[x, 0];

              if(sweet.Type == SweetsType.EMPTY)
              {
                    GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x,-1), Quaternion.identity);
                    newSweet.transform.parent = transform;

                    sweets[x, 0] = newSweet.GetComponent<GameSweet>();
                    sweets[x, 0].Init(x, -1,this,SweetsType.NORMAL);
                    sweets[x, 0].MovedComponet.Move(x, 0);
                    sweets[x, 0].ColorComponet.SetColor((ColorSweet.ColorType)Random.Range(0,sweets[x,0].ColorComponet.NumColors));
                FilledNotFinshed = true;      
              }
        }
        return FilledNotFinshed;
    }

 

  在Start()中調用 AllFill()方法

    void Start() {

        //字典的實例化
        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();

        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (!sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }

        sweets = new GameSweet[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                CreateNewSweet(x, y, SweetsType.EMPTY);
            }
        }

        AllFill();
    }

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    //甜品的種類
    public enum SweetsType
    {
        EMPTY,
        NORMAL,
        BARRIER,
        ROE_CLEAR,
        COLUMN_CLEAR,
        RAINBOWCANDY,
        COUNT   //標記類型
    }

    //甜品預制體的字典,我們可以通過甜品的種類來得到對應的甜品游戲物體
    public Dictionary<SweetsType, GameObject> sweetPrefabDict;

    [System.Serializable]
    public struct SweetPrefab
    {
        public SweetsType type;
        public GameObject prefab;
    }

    public SweetPrefab[] sweetPrefabs;

    //單例實例化
    private static GameManager _instance;
    public static GameManager Instance
    {
        get
        {
            return _instance;
        }

        set
        {
            _instance = value;
        }
    }

    //大網格的行列數
    public int xColumn;
    public int yRow;

    public GameObject gridPrefab;

    //甜品數組
    private GameSweet[,] sweets;

    private void Awake()
    {
        _instance = this;
    }

    // Use this for initialization
    void Start() {

        //字典的實例化
        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();

        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (!sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }

        sweets = new GameSweet[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                CreateNewSweet(x, y, SweetsType.EMPTY);
            }
        }

        AllFill();
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    public Vector3 CorrectPosition(int x ,int y)
    {
        //實際需要實例化巧克力的X位置 = GameManager位置的X坐標-大網格長度的一半+行列對應的X坐標
        // 實際需要實例化巧克力的Y位置 = GameManager位置的Y坐標-大網格長度的一半+行列對應的Y坐標
        return new Vector3(transform.position.x-xColumn/2f+x,transform.position.y+yRow/2f-y);
    }

    //產生甜品的方法
    public GameSweet CreateNewSweet(int x,int y,SweetsType type)
    {
        GameObject newSweet =  Instantiate(sweetPrefabDict[type], CorrectPosition(x, y), Quaternion.identity);
        newSweet.transform.parent = transform;

        sweets[x, y] = newSweet.GetComponent<GameSweet>();
        sweets[x, y].Init(x,y,this,type);

        return sweets[x, y];
    }

    //填充甜品的方法
    public void AllFill()
    {
        while (Fill())
        {

        }
    }

    public bool Fill()
    {
        bool FilledNotFinshed = false;  //用來判斷本次是否完成

        //行遍歷
        for(int y=yRow-2;y>=0;y--)
        {
            for(int x=0;x<xColumn;x++)
            {
                GameSweet sweet = sweets[x, y]; //得到當前元素位置

                //如果無法移動,則無法往下填充
                if (sweet.CanMove())
                {
                    GameSweet sweetBelow = sweets[x, y + 1]; 

                    if(sweetBelow.Type == SweetsType.EMPTY)
                    {
                        sweet.MovedComponet.Move(x,y+1);
                        sweets[x, y + 1] = sweet;
                        CreateNewSweet(x, y, SweetsType.EMPTY);
                        FilledNotFinshed = true;
                    }
                }
            }

        }
        //最上排的特殊情況
        for (int x = 0; x < xColumn; x++)
           {
               GameSweet sweet = sweets[x, 0];

              if(sweet.Type == SweetsType.EMPTY)
              {
                    GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x,-1), Quaternion.identity);
                    newSweet.transform.parent = transform;

                    sweets[x, 0] = newSweet.GetComponent<GameSweet>();
                    sweets[x, 0].Init(x, -1,this,SweetsType.NORMAL);
                    sweets[x, 0].MovedComponet.Move(x, 0);
                    sweets[x, 0].ColorComponet.SetColor((ColorSweet.ColorType)Random.Range(0,sweets[x,0].ColorComponet.NumColors));
                FilledNotFinshed = true;      
              }
        }
        return FilledNotFinshed;
    }
}
GameManager.cs

 

  為了使游戲填充時間肉眼速度看得見,添加移動填充協成

 

 

移動填充的協成

 

  填充時間

 public float fillTime;

 

  填充甜品的方法

    public IEnumerator AllFill()
    {
        while (Fill())
        {
            yield return new WaitForSeconds(fillTime);
        }
    }

 

 

 void Start() {

        //字典的實例化
        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();

        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (!sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }

        sweets = new GameSweet[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                CreateNewSweet(x, y, SweetsType.EMPTY);
            }
        }

        StartCoroutine(AllFill());
    }

 

 

 

  此時出場動畫會出現卡頓現象,通過修改MovedSweet.cs腳本添加協成

  使每一幀都會進行出場動畫

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovedSweet : MonoBehaviour {

    private GameSweet sweet;

    private IEnumerator moveCoroutine;

    private void Awake()
    {
        sweet = GetComponent<GameSweet>();
    }

    //開啟或者結束一個協成
    public void Move(int newX,int newY,float time)
    {
        if(moveCoroutine!=null)
        {
            StopCoroutine(moveCoroutine);
        }

        moveCoroutine = MoveCoroutine(newX,newY,time);
        StartCoroutine(moveCoroutine);
    }

    //負責移動的協成
    private IEnumerator MoveCoroutine(int newX,int newY,float time)
    {
        sweet.X = newX;
        sweet.Y = newY;

        //每一幀移動一點點
        Vector3 startPos = transform.position;
        Vector3 endPos = sweet.gameManager.CorrectPosition(newX,newY);

        for(float t=0;t<time;t+=Time.deltaTime)
        {
            sweet.transform.position = Vector3.Lerp(startPos,endPos,t/time);
            yield return 0;
        }

        sweet.transform.position = endPos;

    }
}
MovedSweet.cs

 

  開啟或者結束一個協成

public void Move(int newX,int newY,float time)
    {
        if(moveCoroutine!=null)
        {
            StopCoroutine(moveCoroutine);
        }

        moveCoroutine = MoveCoroutine(newX,newY,time);
        StartCoroutine(moveCoroutine);
    }

 

  負責移動的協成

    private IEnumerator MoveCoroutine(int newX,int newY,float time)
    {
        sweet.X = newX;
        sweet.Y = newY;

        //每一幀移動一點點
        Vector3 startPos = transform.position;
        Vector3 endPos = sweet.gameManager.CorrectPosition(newX,newY);

        for(float t=0;t<time;t+=Time.deltaTime)
        {
            sweet.transform.position = Vector3.Lerp(startPos,endPos,t/time);
            yield return 0;
        }

        sweet.transform.position = endPos;
    }

 

 public bool Fill()
    {
        bool FilledNotFinshed = false;  //用來判斷本次是否完成

        //行遍歷
        for(int y=yRow-2;y>=0;y--)
        {
            for(int x=0;x<xColumn;x++)
            {
                GameSweet sweet = sweets[x, y]; //得到當前元素位置

                //如果無法移動,則無法往下填充
                if (sweet.CanMove())
                {
                    GameSweet sweetBelow = sweets[x, y + 1]; 

                    if(sweetBelow.Type == SweetsType.EMPTY)
                    {
                        sweet.MovedComponet.Move(x,y+1,fillTime);
                        sweets[x, y + 1] = sweet;
                        CreateNewSweet(x, y, SweetsType.EMPTY);
                        FilledNotFinshed = true;
                    }
                }
            }

        }
        //最上排的特殊情況
        for (int x = 0; x < xColumn; x++)
           {
               GameSweet sweet = sweets[x, 0];

              if(sweet.Type == SweetsType.EMPTY)
              {
                    GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x,-1), Quaternion.identity);
                    newSweet.transform.parent = transform;

                    sweets[x, 0] = newSweet.GetComponent<GameSweet>();
                    sweets[x, 0].Init(x, -1,this,SweetsType.NORMAL);
                    sweets[x, 0].MovedComponet.Move(x, 0,fillTime);
                    sweets[x, 0].ColorComponet.SetColor((ColorSweet.ColorType)Random.Range(0,sweets[x,0].ColorComponet.NumColors));
                FilledNotFinshed = true;      
              }
        }
        return FilledNotFinshed;
    }

 

 

 

障礙物_餅干

 

  制作餅干預制體

 

   void Start() {

        //字典的實例化
        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();

        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (!sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }

        sweets = new GameSweet[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                CreateNewSweet(x, y, SweetsType.EMPTY);
            }
        }

        //在(4,4)這個坐標點生成障礙物
        Destroy(sweets[4, 4].gameObject);
        CreateNewSweet(4,4,SweetsType.BARRIER);

        StartCoroutine(AllFill());
    }

 

 

 

斜向填充算法

 

  如果無法移動,其它甜品則無法往下填充

    public bool Fill()
    {
        bool FilledNotFinshed = false;  //用來判斷本次是否完成

        //行遍歷
        for(int y=yRow-2;y>=0;y--)
        {
            for(int x=0;x<xColumn;x++)
            {
                GameSweet sweet = sweets[x, y]; //得到當前元素位置

                //如果無法移動,則無法往下填充
                if (sweet.CanMove())
                {
                    GameSweet sweetBelow = sweets[x, y + 1]; 

                    if(sweetBelow.Type == SweetsType.EMPTY)//垂直填充
                    {
                        Destroy(sweetBelow.gameObject);
                        sweet.MovedComponet.Move(x,y+1,fillTime);
                        sweets[x, y + 1] = sweet;
                        CreateNewSweet(x, y, SweetsType.EMPTY);
                        FilledNotFinshed = true;
                    }
                    else
                    {
                        //-1代表左,1代表右
                        for (int down = -1; down <= 1; down++)
                        {
                            if (down != 0)
                            {
                                int downX = x + down;
                                //排除邊界的時候
                                //左下方
                                if (downX >= 0 && downX < xColumn)
                                {
                                    GameSweet downSweet = sweets[downX, y + 1];
                                    if (downSweet.Type == SweetsType.EMPTY)
                                    {
                                        bool canfill = true;    //用來判斷垂直填充是否可以滿足填充要求
                                        for (int aboutY = y; aboutY >= 0; aboutY--)
                                        {
                                            GameSweet sweetAbove = sweets[downX, aboutY];
                                            if (sweetAbove.CanMove())
                                            {
                                                break;
                                            }
                                            else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetsType.EMPTY)
                                            {
                                                canfill = false;
                                                break;
                                            }
                                        }

                                        if (!canfill)
                                        {
                                            Destroy(downSweet.gameObject);
                                            sweet.MovedComponet.Move(downX, y + 1, fillTime);
                                            sweets[downX, y + 1] = sweet;
                                            CreateNewSweet(x, y, SweetsType.EMPTY);
                                            FilledNotFinshed = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
               
            }

        }
        //最上排的特殊情況
        for (int x = 0; x < xColumn; x++)
           {
               GameSweet sweet = sweets[x, 0];

              if(sweet.Type == SweetsType.EMPTY)
              {
                    GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x,-1), Quaternion.identity);
                    newSweet.transform.parent = transform;

                    sweets[x, 0] = newSweet.GetComponent<GameSweet>();
                    sweets[x, 0].Init(x, -1,this,SweetsType.NORMAL);
                    sweets[x, 0].MovedComponet.Move(x, 0,fillTime);
                    sweets[x, 0].ColorComponet.SetColor((ColorSweet.ColorType)Random.Range(0,sweets[x,0].ColorComponet.NumColors));
                FilledNotFinshed = true;      
              }
        }
        return FilledNotFinshed;
    }

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovedSweet : MonoBehaviour {

    private GameSweet sweet;

    private IEnumerator moveCoroutine;

    private void Awake()
    {
        sweet = GetComponent<GameSweet>();
    }

    //開啟或者結束一個協成
    public void Move(int newX,int newY,float time)
    {
        if(moveCoroutine!=null)
        {
            StopCoroutine(moveCoroutine);
        }

        moveCoroutine = MoveCoroutine(newX,newY,time);
        StartCoroutine(moveCoroutine);
    }

    //負責移動的協成
    private IEnumerator MoveCoroutine(int newX,int newY,float time)
    {
        sweet.X = newX;
        sweet.Y = newY;

        //每一幀移動一點點
        Vector3 startPos = transform.position;
        Vector3 endPos = sweet.gameManager.CorrectPosition(newX,newY);

        for(float t=0;t<time;t+=Time.deltaTime)
        {
            sweet.transform.position = Vector3.Lerp(startPos,endPos,t/time);
            yield return 0;
        }

        sweet.transform.position = endPos;
    }
}
MovedSweet.cs

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColorSweet : MonoBehaviour {

    public enum ColorType
    {
        YELLOW,
        PUPLE,
        RED,
        BLUE,
        GREEN,
        PNGK,
        ANY,
        COUNT
    }

    [System.Serializable]
    public struct ColorSprite
    {
        public ColorType color;
        public Sprite sprite;
    }

    public ColorSprite[] ColorSprites;

    private Dictionary<ColorType, Sprite> colorSpriteDict;

    private SpriteRenderer sprite;

    public int NumColors
    {
        get{ return ColorSprites.Length; }
    }

    public ColorType Color
    {
        get
        {
            return color;
        }

        set
        {
            SetColor(value);
        }
    }

    private ColorType color;



    public void Awake()
    {
        sprite = transform.Find("Sweet").GetComponent<SpriteRenderer>();

        colorSpriteDict = new Dictionary<ColorType, Sprite>();

        for(int i = 0; i < ColorSprites.Length; i++)
        {
            if (!colorSpriteDict.ContainsKey(ColorSprites[i].color))
            {
                colorSpriteDict.Add(ColorSprites[i].color,ColorSprites[i].sprite);
            }
        }
    }

    public void SetColor(ColorType newColor)
    {
        color = newColor;
        if (colorSpriteDict.ContainsKey(newColor))
        {
            sprite.sprite = colorSpriteDict[newColor];
        }
    }

}
ColorSweet.cs

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameSweet : MonoBehaviour {

    private int x;
    private int y;
    public int X
    {
        get
        {
            return x;
        }

        set
        {
            if (CanMove())
            {
                x = value;
            }
        }
    }
    public int Y
    {
        get
        {
            return y;
        }

        set
        {
            if (CanMove())
            {
                y = value;
            }
        }
    }

    private GameManager.SweetsType type;
    public GameManager.SweetsType Type
    {
        get
        {
            return type;
        }
    }


    [HideInInspector]
    public GameManager gameManager;

    public MovedSweet MovedComponet
    {
        get
        {
            return movedComponet;
        }
    }
    private MovedSweet movedComponet;


    public ColorSweet ColorComponet
    {
        get
        {
            return coloredCompent;
        }
    }

    private ColorSweet coloredCompent;

    //判斷甜品是否可以移動
    public bool CanMove()
    {
        return movedComponet != null;
    }

    //判斷是否可以着色
    public bool CanColor()
    {
        return coloredCompent != null;
    }

    private void Awake()
    {
        movedComponet = GetComponent<MovedSweet>();
        coloredCompent = GetComponent<ColorSweet>();
    }

    public void Init(int _x,int _y,GameManager _gameManager,GameManager.SweetsType _type)
    {
        x = _x;
        y = _y;
        gameManager = _gameManager;
        type = _type;
    }


}
GameSweet.cs

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    //甜品的種類
    public enum SweetsType
    {
        EMPTY,
        NORMAL,
        BARRIER,
        ROE_CLEAR,
        COLUMN_CLEAR,
        RAINBOWCANDY,
        COUNT   //標記類型
    }

    //甜品預制體的字典,我們可以通過甜品的種類來得到對應的甜品游戲物體
    public Dictionary<SweetsType, GameObject> sweetPrefabDict;

    [System.Serializable]
    public struct SweetPrefab
    {
        public SweetsType type;
        public GameObject prefab;
    }

    public SweetPrefab[] sweetPrefabs;

    //單例實例化
    private static GameManager _instance;
    public static GameManager Instance
    {
        get
        {
            return _instance;
        }

        set
        {
            _instance = value;
        }
    }

    //大網格的行列數
    public int xColumn;
    public int yRow;

    //填充時間
    public float fillTime;

    public GameObject gridPrefab;

    //甜品數組
    private GameSweet[,] sweets;

    private void Awake()
    {
        _instance = this;
    }

    // Use this for initialization
    void Start() {

        //字典的實例化
        sweetPrefabDict = new Dictionary<SweetsType, GameObject>();

        for(int i=0;i<sweetPrefabs.Length;i++)
        {
            if (!sweetPrefabDict.ContainsKey(sweetPrefabs[i].type))
            {
                sweetPrefabDict.Add(sweetPrefabs[i].type,sweetPrefabs[i].prefab);
            }
        }

       for(int x = 0; x < xColumn; x++)
        {
            for (int y=0;y<yRow;y++)
            {
                GameObject chocolate = Instantiate(gridPrefab,CorrectPosition(x,y),Quaternion.identity);
                chocolate.transform.SetParent(transform);
            }
        }

        sweets = new GameSweet[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                CreateNewSweet(x, y, SweetsType.EMPTY);
            }
        }

        //在(4,4)這個坐標點生成障礙物
        Destroy(sweets[4, 4].gameObject);
        CreateNewSweet(4,4,SweetsType.BARRIER);

        StartCoroutine(AllFill());
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    public Vector3 CorrectPosition(int x ,int y)
    {
        //實際需要實例化巧克力的X位置 = GameManager位置的X坐標-大網格長度的一半+行列對應的X坐標
        // 實際需要實例化巧克力的Y位置 = GameManager位置的Y坐標-大網格長度的一半+行列對應的Y坐標
        return new Vector3(transform.position.x-xColumn/2f+x,transform.position.y+yRow/2f-y);
    }

    //產生甜品的方法
    public GameSweet CreateNewSweet(int x,int y,SweetsType type)
    {
        GameObject newSweet =  Instantiate(sweetPrefabDict[type], CorrectPosition(x, y), Quaternion.identity);
        newSweet.transform.parent = transform;

        sweets[x, y] = newSweet.GetComponent<GameSweet>();
        sweets[x, y].Init(x,y,this,type);

        return sweets[x, y];
    }

    //填充甜品的方法
    public IEnumerator AllFill()
    {
        while (Fill())
        {
            yield return new WaitForSeconds(fillTime);
        }
    }

    public bool Fill()
    {
        bool FilledNotFinshed = false;  //用來判斷本次是否完成

        //行遍歷
        for(int y=yRow-2;y>=0;y--)
        {
            for(int x=0;x<xColumn;x++)
            {
                GameSweet sweet = sweets[x, y]; //得到當前元素位置

                //如果無法移動,則無法往下填充
                if (sweet.CanMove())
                {
                    GameSweet sweetBelow = sweets[x, y + 1]; 

                    if(sweetBelow.Type == SweetsType.EMPTY)//垂直填充
                    {
                        Destroy(sweetBelow.gameObject);
                        sweet.MovedComponet.Move(x,y+1,fillTime);
                        sweets[x, y + 1] = sweet;
                        CreateNewSweet(x, y, SweetsType.EMPTY);
                        FilledNotFinshed = true;
                    }
                    else
                    {
                        //-1代表左,1代表右
                        for (int down = -1; down <= 1; down++)
                        {
                            if (down != 0)
                            {
                                int downX = x + down;
                                //排除邊界的時候
                                //左下方
                                if (downX >= 0 && downX < xColumn)
                                {
                                    GameSweet downSweet = sweets[downX, y + 1];
                                    if (downSweet.Type == SweetsType.EMPTY)
                                    {
                                        bool canfill = true;    //用來判斷垂直填充是否可以滿足填充要求
                                        for (int aboutY = y; aboutY >= 0; aboutY--)
                                        {
                                            GameSweet sweetAbove = sweets[downX, aboutY];
                                            if (sweetAbove.CanMove())
                                            {
                                                break;
                                            }
                                            else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetsType.EMPTY)
                                            {
                                                canfill = false;
                                                break;
                                            }
                                        }

                                        if (!canfill)
                                        {
                                            Destroy(downSweet.gameObject);
                                            sweet.MovedComponet.Move(downX, y + 1, fillTime);
                                            sweets[downX, y + 1] = sweet;
                                            CreateNewSweet(x, y, SweetsType.EMPTY);
                                            FilledNotFinshed = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
               
            }

        }
        //最上排的特殊情況
        for (int x = 0; x < xColumn; x++)
           {
               GameSweet sweet = sweets[x, 0];

              if(sweet.Type == SweetsType.EMPTY)
              {
                    GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x,-1), Quaternion.identity);
                    newSweet.transform.parent = transform;

                    sweets[x, 0] = newSweet.GetComponent<GameSweet>();
                    sweets[x, 0].Init(x, -1,this,SweetsType.NORMAL);
                    sweets[x, 0].MovedComponet.Move(x, 0,fillTime);
                    sweets[x, 0].ColorComponet.SetColor((ColorSweet.ColorType)Random.Range(0,sweets[x,0].ColorComponet.NumColors));
                FilledNotFinshed = true;      
              }
        }
        return FilledNotFinshed;
    }
}
GameManager.cs

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM