【Unity3D】开发【拼图容道】小游戏

时间:2023-02-07 19:02:52

今天给大家分享一个小游戏,应公司需求今天花了点时间做了个华容道拼图小游戏,请大家多少指点,直接上代码

【Unity3D】开发【拼图容道】小游戏

using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using System.Collections.Generic;

public enum MoveDirectionType
{
    UP,
    DOWN,
    LEFT,
    RIGHT,
    NONE,
}

public class KlotskiItem : MonoBehaviour
{
    private void Awake()
    {
        myAwake();
    }

    private MyButton btn_click;

    private CallBack<Vector2[],int,bool> callBack = null;

    private int index = -1;

    private Image klotskiIcon;

    private ManagerVars vars;

    private Vector2 dot;

    private float widthAndHeight = 240;

    private Text txt_test;

    private Vector2[] allAroundDot = new Vector2[4];

    private Vector2 defaultDot;

    private void myAwake() {
        vars = ManagerVars.getManagerVars();
        btn_click = GetComponent<MyButton>();
        btn_click.OnPointerDown(pointerDown);
        klotskiIcon = GetComponent<Image>();
        txt_test = transform.Find("Text").GetComponent<Text>();
    }
    /// <summary>
    /// 按钮按下事件触发
    /// </summary>
    private void pointerDown() {
        //Debug.LogError(dot);
        if (GameApp.klotskiFristMove)
        {
            if (dot.x == 2 && dot.y == 2)
            {
                toMove(MoveDirectionType.RIGHT);
                dot.x = 3;
                refreshDot(dot);
                GameApp.klotskiFristMove = false;
            }
        }
        else
        {
            if (dot.x == 3 && dot.y == 2)
            {
                // 把第一步归为,检查第一个空出的位置是否被占用,并且检查师傅完成拼图
                allAroundDot[0] = new Vector2(-10,-10);
                allAroundDot[1] = new Vector2(-10, -10);
                allAroundDot[2] = new Vector2(2, 2);
                allAroundDot[3] = new Vector2(-10, -10);
                callBack(allAroundDot, index,true);
            }
            else {
                if (callBack != null)
                {
                    findAllAroundDot();
                    callBack(allAroundDot, index,false);
                }
            }
           
        }

    }


    /// <summary>
    /// 设置属性信息
    /// </summary>
    /// <param name="_index"></param>
    /// <param name="_iconLocation"></param>
    /// <param name="_dot"></param>
    /// <param name="clickCallCack"></param>
    public void settingData(int _index,int _iconLocation,Vector2 _dot, Vector2 _defaultDot, CallBack<Vector2[],int,bool> clickCallCack)
    {
        index = _index;
        refreshDot(_dot);
        defaultDot = _defaultDot;
        callBack = clickCallCack;
        klotskiIcon.sprite = vars.getKlotskiSpriteDicByName("1_0"+ _iconLocation);
    }
    /// <summary>
    /// 更新坐标点
    /// </summary>
    /// <param name="_dot"></param>
    private void refreshDot(Vector2 _dot) {
        dot = _dot;
        txt_test.text = dot.ToString();
    }

    public void move(MoveDirectionType mdt)
    {
        toMove(mdt);
        switch (mdt)
        {
            case MoveDirectionType.UP:
                dot.y -= 1;
                break;
            case MoveDirectionType.DOWN:
                dot.y += 1;
                break;
            case MoveDirectionType.LEFT:
                dot.x -= 1;
                break;
            case MoveDirectionType.RIGHT:
                dot.x += 1;
                break;
        }
        refreshDot(dot);
    }

    /// <summary>
    /// 图片移动
    /// </summary>
    /// <param name="mdt"></param>
    private void toMove(MoveDirectionType mdt) {
        if (mdt == MoveDirectionType.NONE) return;
        Vector3 newPosition = Vector3.zero;
        newPosition = transform.localPosition;
        switch (mdt)
        {
            case MoveDirectionType.UP:
                newPosition.y += widthAndHeight;
                break;
            case MoveDirectionType.DOWN:
                newPosition.y -= widthAndHeight;
                break;
            case MoveDirectionType.LEFT:
                newPosition.x -= widthAndHeight;
                break;
            case MoveDirectionType.RIGHT:
                newPosition.x += widthAndHeight;
                break;
        }
        transform.localPosition = newPosition;
    }

    /// <summary>
    /// 检查对应位置
    /// </summary>
    /// <param name="_dot"></param>
    /// <returns></returns>
    public bool checkDot(Vector2 _dot) {
        return dot.x == _dot.x && dot.y == _dot.y;
    }

    /// <summary>
    /// 获取当前图片四周位置
    /// </summary>
    private void findAllAroundDot() {
        settingDot(dot.x, dot.y - 1,0);
        settingDot(dot.x, dot.y + 1, 1);
        settingDot(dot.x - 1, dot.y, 2);
        settingDot(dot.x + 1, dot.y, 3);
        
    }
    /// <summary>
    /// 把符合要求的点放入列表
    /// </summary>
    /// <param name="_x"></param>
    /// <param name="_y"></param>
    private void settingDot(float _x,float _y,int _index) {
        Vector2 newDot = Vector2.zero;
        if (checkJudge(_x) && checkJudge(_y))
        {
            newDot.x = _x;
            newDot.y = _y;
            allAroundDot[_index] = newDot;
        }
        else {
            newDot.x = -10;
            newDot.y = -10;
            allAroundDot[_index] = newDot;
        }
        
    }
    /// <summary>
    /// 检查范围
    /// </summary>
    /// <param name="_value"></param>
    /// <returns></returns>
    private bool checkJudge(float _value) {
        return _value >= 0 && _value < 3;
    }

    public bool checkPositionIsEqual() {
        return  dot.x == defaultDot.x && dot.y == defaultDot.y;
    }
}

  

【Unity3D】开发【拼图容道】小游戏【Unity3D】开发【拼图容道】小游戏

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

public class KlotskiPanel : MonoBehaviour
{
private void Awake()
{
myAwake();
}

public Vector2[] defaultDot;

private Button btn_back;
private Button btn_reset;

private List<KlotskiItem> allKlotskiItem;
private float _X,_Y;
private int maxCount = 9;
private float widthAndHeight = 240;
private Vector3 newDot = Vector3.zero;
private int[] allLocation = { 2,3,6,5,1,8,7,4,9};

private ManagerVars vars;
private void myAwake() {

btn_back = transform.Find("back").GetComponent<Button>();
btn_back.onClick.AddListener(()=> { gameObject.SetActive(false); });
btn_reset = transform.Find("reset").GetComponent<Button>();
btn_reset.onClick.AddListener(resetEvent);
vars = ManagerVars.getManagerVars();
allKlotskiItem = new List<KlotskiItem>();
_X = -300;
_Y = 240;
gameObject.SetActive(false);

//init();
}

 

private void init()
{
gameObject.SetActive(true);
if (!GameApp.isStartKlotskiGame)
{
GameApp.isStartKlotskiGame = true;
resetEvent();
}
}

private void resetEvent() {
GameApp.klotskiFristMove = true;
// 初始化九宫格位置点
for (int i = 0; i < maxCount; i++)
{
Vector2 dot = new Vector2((i % 3), (i / 3));
newDot.x = (i % 3) * widthAndHeight + _X;
newDot.y = _Y - (i / 3) * widthAndHeight;
instantiateItem(i, dot);
}
}

private void instantiateItem(int _index, Vector2 _dot) {
int length = allKlotskiItem.Count;
KlotskiItem ki = null;
if (_index < length)
{
ki = allKlotskiItem[_index];
}
else {
GameObject go = Instantiate(vars.klotskiItem);
go.transform.SetParent(transform, false);
ki = go.GetComponent<KlotskiItem>();
allKlotskiItem.Add(ki);
}
ki.gameObject.transform.localPosition = newDot;
int location = allLocation[_index];
ki.settingData(_index, location, _dot, defaultDot[location - 1], clickCallCack);
}

//private void instantiateItem(int _index,Vector2 _dot) {
// GameObject go = Instantiate(vars.klotskiItem);
// go.transform.SetParent(transform, false);
// go.transform.localPosition = newDot;
// KlotskiItem ki = go.GetComponent<KlotskiItem>();

// allKlotskiItem.Add(ki);
//}

private void clickCallCack(Vector2[] _allAround,int _index,bool isWin) {
int length = _allAround.Length;
for (int i = 0; i < length; i++)
{
Vector2 dot = _allAround[i];
if (dot.x != -10 && dot.y != -10)
{
bool ret = checkAllKlotskiItem(dot);
if (!ret)
{
allKlotskiItem[_index].move((MoveDirectionType)i);
if (isWin)
{
checkGameWin();
}
break;
}
}
}

}

private bool checkAllKlotskiItem(Vector2 _dot) {
bool ret = false;
int length = allKlotskiItem.Count;
for (int i = 0; i < length; i++)
{
KlotskiItem ki = allKlotskiItem[i];
ret = ki.checkDot(_dot);
if (ret) break;
}
return ret;
}

private void checkGameWin() {
int length = allKlotskiItem.Count;
for (int i = 0; i < length; i++)
{
KlotskiItem ki = allKlotskiItem[i];
bool ret = ki.checkPositionIsEqual();
if (!ret) {
GameApp.klotskiFristMove = true;
Debug.LogError("未完成拼图");
return;
}
}
Debug.LogError("胜利");
}
}