Unity3D 2019.3开发的中国象棋的源代码

Unity3D 2019.3开发的中国象棋的源代码

在这里插入图片描述

1 ChessEnum

public enum ChessSide
{
    RED,
    BLACK,
    Barrier
}
public enum ChessType
{
   Soldier=0,
   Cannon=1,
   Chariot=2,
   Elephant =3,
   Horse=4,
   Advisor=5,
   General = 6,
   Barrier =7
}
public class ChessKey {
    public ChessSide side;
    public ChessType type;
    public ChessKey(){}
    public ChessKey(ChessSide side, ChessType type) {
        this.side = side;
        this.type = type;
    }

}

2 ChessManger

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

public class ChessManger : MonoBehaviour
{
    public static ChessManger instance;
    public GameObject chessPrefab;
    public int xColumn = 9;
    public int yRow = 10;
    public int[,] chessArray = new int[,]
    {
/*      {3,4,5,6,7,6,5,4,3 },//1
        {0,0,0,0,0,0,0,0,0 },//2
        {0,2,0,0,0,0,0,2,0 },//3
        {1,0,1,0,1,0,1,0,1 },//4
        {0,0,0,0,0,0,0,0,0 },//5
        {0,0,0,0,0,0,0,0,0 },//6
        {1,0,1,0,1,0,1,0,1 },//7
        {0,2,0,0,0,0,0,2,0 },//8
        {0,0,0,0,0,0,0,0,0 },//9
        {3,4,5,6,7,6,5,4,3 },//10
*/    // 1 2 3 4 5 6 7 8 9 10
        {2,-1,-1,0, -1,-1, 0,-1,-1,2 },//1
        {4,-1, 1,-1,-1,-1,-1, 1,-1,4 },//2
        {3,-1,-1,0, -1,-1, 0,-1,-1,3 },//3
        {5,-1,-1,-1,-1,-1,-1,-1,-1,5 },//4
        {6,-1,-1,0, -1,-1, 0,-1,-1,6},//5
        {5,-1,-1,-1,-1,-1,-1,-1,-1,5},//6
        {3,-1,-1,0, -1,-1, 0,-1,-1,3},//7
        {4,-1, 1,-1,-1,-1,-1, 1,-1,4 },//8
        {2,-1,-1,0, -1,-1,0, -1,-1,2 },//9
    };
    public GameObject[,] gameObjectsArray;
    public LinkedList<GameObject> BarrierGameObject = new LinkedList<GameObject>();
    public Piece selectPiece = null;
    public Piece lastSelectPiece = null;
    private void Awake()
    {
        gameObjectsArray = new GameObject[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                ChessSide tempside = new ChessSide();
                if (chessArray[x, y] != -1)
                {
                    if (y <= 5) { tempside = ChessSide.RED; } else { tempside = ChessSide.BLACK; }
                    ChessType tempType = (ChessType)chessArray[x, y];
                    GameObject tempGameObject = GameObject.Instantiate(chessPrefab, correctPosition(x, y, 1), Quaternion.identity);
                    Piece tempPiece = tempGameObject.GetComponent<Piece>();
                    tempGameObject.transform.parent = transform;
                    ChessKey tempChessKey = new ChessKey();
                    tempChessKey.side = tempside;
                    tempChessKey.type = tempType;
                    tempPiece.init(x, y, tempChessKey);
                    gameObjectsArray[x, y] = tempGameObject;
                }
            }
        }
        instance = this;
    }
    public ChessSide currentChessSide = ChessSide.BLACK;
    // Start is called before the first frame update
    void Start()
    {
        currentChessSide = SwitchSide.instance.chessSideMethon(currentChessSide);
    }
    Vector3 correctPosition(float x, float y, float z) {
        float tempX;
        float tempY;
        tempX = (x - xColumn / 2);
        tempY = (y - (yRow) / 2);
        return new Vector3(tempX, tempY, z);
    }
    // Update is called once per frame


    public List<Vector2> getMoveable(Piece piece) {
        int x0 = 3;
        int x1 = 5;
        int y2 = -1;
        int y3 = -1;

        List<Vector2> moveables = new List<Vector2>();
        switch (piece.chessKey.type)
        {
            case ChessType.Soldier:
                if (piece.chessKey.side == ChessSide.RED)
                {
                    if (piece.y <= 4)
                    {
                        if (gameObjectsArray[piece.x, piece.y + 1] != null &&
                             gameObjectsArray[piece.x, piece.y + 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x, piece.y + 1));
                        }
                        else if (gameObjectsArray[piece.x, piece.y + 1] == null)
                        {
                            moveables.Add(new Vector2(piece.x, piece.y + 1));
                        }

                    } else if (piece.y < yRow) {
                        if (piece.y + 1 < yRow) {
                            if (gameObjectsArray[piece.x, piece.y + 1] != null &&
                                gameObjectsArray[piece.x, piece.y + 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                            {
                                moveables.Add(new Vector2(piece.x, piece.y + 1));
                            }
                            else if (gameObjectsArray[piece.x, piece.y + 1] == null)
                            {
                                moveables.Add(new Vector2(piece.x, piece.y + 1));
                            }
                        }
                        if (piece.x - 1 >= 0)
                        {
                            if (gameObjectsArray[piece.x - 1, piece.y] != null &&
                                gameObjectsArray[piece.x - 1, piece.y].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                            {
                                moveables.Add(new Vector2(piece.x - 1, piece.y));
                            }
                            else if (gameObjectsArray[piece.x - 1, piece.y] == null)
                            {
                                moveables.Add(new Vector2(piece.x - 1, piece.y));
                            }
                        }

                        if (piece.x + 1 < xColumn)
                        {
                            if (gameObjectsArray[piece.x + 1, piece.y] != null &&
                                gameObjectsArray[piece.x + 1, piece.y].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                            {
                                moveables.Add(new Vector2(piece.x + 1, piece.y));
                            }
                            else if (gameObjectsArray[piece.x + 1, piece.y] == null)
                            {
                                moveables.Add(new Vector2(piece.x + 1, piece.y));
                            }
                        }

                    }
                }
                else if (piece.chessKey.side == ChessSide.BLACK)
                {
                    if (piece.y >= 5)
                    {
                        if (gameObjectsArray[piece.x, piece.y - 1] != null &&
                            gameObjectsArray[piece.x, piece.y - 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x, piece.y - 1));
                        }
                        else if (gameObjectsArray[piece.x, piece.y - 1] == null)
                        {
                            moveables.Add(new Vector2(piece.x, piece.y - 1));
                        }

                    }
                    else if (piece.y >= 0)
                    {
                        if (piece.y - 1 >= 0)
                        {
                            if (gameObjectsArray[piece.x, piece.y - 1] != null &&
                                gameObjectsArray[piece.x, piece.y - 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                            {
                                moveables.Add(new Vector2(piece.x, piece.y - 1));
                            }
                            else if (gameObjectsArray[piece.x, piece.y - 1] == null)
                            {
                                moveables.Add(new Vector2(piece.x, piece.y - 1));
                            }
                        }

                        if (piece.x - 1 >= 0)
                        {
                            if (gameObjectsArray[piece.x - 1, piece.y] != null &&
                                gameObjectsArray[piece.x - 1, piece.y].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                            {
                                moveables.Add(new Vector2(piece.x - 1, piece.y));
                            }
                            else if (gameObjectsArray[piece.x - 1, piece.y] == null)
                            {
                                moveables.Add(new Vector2(piece.x - 1, piece.y));
                            }
                        }

                        if (piece.x + 1 < xColumn)
                        {
                            if (gameObjectsArray[piece.x + 1, piece.y] != null &&
                                gameObjectsArray[piece.x + 1, piece.y].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                            {
                                moveables.Add(new Vector2(piece.x + 1, piece.y));
                            }
                            else if (gameObjectsArray[piece.x + 1, piece.y] == null)
                            {
                                moveables.Add(new Vector2(piece.x + 1, piece.y));
                            }
                        }

                    }

                }
                break;
            case ChessType.Cannon:
                for (int x = piece.x - 1; x >= 0; x--)
                {
                    if (gameObjectsArray[x, piece.y] != null)
                    {
                        break;
                    }
                    moveables.Add(new Vector2(x, piece.y));
                }
                for (int x = piece.x + 1; x < xColumn; x++)
                {
                    if (gameObjectsArray[x, piece.y] != null)
                    {
                        break;
                    }
                    moveables.Add(new Vector2(x, piece.y));
                }
                for (int y = piece.y + 1; y < yRow; y++)
                {
                    if (gameObjectsArray[piece.x, y] != null)
                    {
                        break;
                    }
                    moveables.Add(new Vector2(piece.x, y));
                }
                for (int y = piece.y - 1; y >= 0; y--)
                {
                    if (gameObjectsArray[piece.x, y] != null)
                    {
                        break;
                    }
                    moveables.Add(new Vector2(piece.x, y));
                }

                for (int x = piece.x - 1; x >= 0; x--)
                {
                    if (gameObjectsArray[x, piece.y] != null)
                    {
                        for (x = x - 1; x >= 0; x--)
                        {
                            if (gameObjectsArray[x, piece.y] != null)
                            {
                                Piece tempPiece = gameObjectsArray[x, piece.y].GetComponent<Piece>();
                                if (piece.chessKey.side != tempPiece.chessKey.side)
                                {
                                    moveables.Add(new Vector2(x, piece.y));

                                }
                                break;
                            }

                        }
                        break;
                    }
                }

                for (int x = piece.x + 1; x < xColumn; x++)
                {
                    if (gameObjectsArray[x, piece.y] != null)
                    {
                        for (x = x + 1; x < xColumn; x++)
                        {
                            if (gameObjectsArray[x, piece.y] != null)
                            {
                                Piece tempPiece = gameObjectsArray[x, piece.y].GetComponent<Piece>();
                                if (piece.chessKey.side != tempPiece.chessKey.side)
                                {
                                    moveables.Add(new Vector2(x, piece.y));

                                }
                                break;
                            }

                        }
                        break;
                    }
                }

                for (int y = piece.y - 1; y >= 0; y--)
                {
                    if (gameObjectsArray[piece.x, y] != null)
                    {
                        for (y = y - 1; y >= 0; y--)
                        {
                            if (gameObjectsArray[piece.x, y] != null)
                            {
                                Piece tempPiece = gameObjectsArray[piece.x, y].GetComponent<Piece>();
                                if (piece.chessKey.side != tempPiece.chessKey.side)
                                {
                                    moveables.Add(new Vector2(piece.x, y));

                                }
                                break;
                            }

                        }
                        break;
                    }
                }

                for (int y = piece.y + 1; y < yRow; y++)
                {
                    if (gameObjectsArray[piece.x, y] != null)
                    {
                        for (y = y + 1; y < yRow; y++)
                        {
                            if (gameObjectsArray[piece.x, y] != null)
                            {
                                Piece tempPiece = gameObjectsArray[piece.x, y].GetComponent<Piece>();
                                if (piece.chessKey.side != tempPiece.chessKey.side)
                                {
                                    moveables.Add(new Vector2(piece.x, y));

                                }
                                break;
                            }

                        }
                        break;
                    }
                }
                break;
            case ChessType.Chariot:
                for (int x = piece.x - 1; x >= 0; x--)
                {
                    if (gameObjectsArray[x, piece.y] != null)
                    {
                        break;
                    }
                    moveables.Add(new Vector2(x, piece.y));
                }
                for (int x = piece.x + 1; x < xColumn; x++)
                {
                    if (gameObjectsArray[x, piece.y] != null)
                    {
                        break;
                    }
                    moveables.Add(new Vector2(x, piece.y));
                }
                for (int y = piece.y + 1; y < yRow; y++)
                {
                    if (gameObjectsArray[piece.x, y] != null)
                    {
                        break;
                    }
                    moveables.Add(new Vector2(piece.x, y));
                }
                for (int y = piece.y - 1; y >= 0; y--)
                {
                    if (gameObjectsArray[piece.x, y] != null)
                    {
                        break;
                    }
                    moveables.Add(new Vector2(piece.x, y));
                }


                for (int x = piece.x - 1; x >= 0; x--)
                {
                    if (gameObjectsArray[x, piece.y] != null)
                    {
                        Piece tempPiece = gameObjectsArray[x, piece.y].GetComponent<Piece>();
                        if (piece.chessKey.side != tempPiece.chessKey.side)
                        {
                            moveables.Add(new Vector2(x, piece.y));

                        }
                        break;
                    }
                }//
                for (int x = piece.x + 1; x < xColumn; x++)
                {
                    if (gameObjectsArray[x, piece.y] != null)
                    {
                        Piece tempPiece = gameObjectsArray[x, piece.y].GetComponent<Piece>();
                        if (piece.chessKey.side != tempPiece.chessKey.side)
                        {
                            moveables.Add(new Vector2(x, piece.y));

                        }
                        break;
                    }
                }//

                for (int y = piece.y + 1; y < yRow; y++)
                {
                    if (gameObjectsArray[piece.x, y] != null)
                    {
                        Piece tempPiece = gameObjectsArray[piece.x, y].GetComponent<Piece>();
                        if (piece.chessKey.side != tempPiece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x, y));

                        }
                        break;
                    }
                }
                for (int y = piece.y - 1; y >= 0; y--)
                {
                    if (gameObjectsArray[piece.x, y] != null)
                    {
                        Piece tempPiece = gameObjectsArray[piece.x, y].GetComponent<Piece>();
                        if (piece.chessKey.side != tempPiece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x, y));

                        }
                        break;
                    }
                }
                break;
            case ChessType.Elephant:
                int y0 = -1;
                int y1 = -1;
                if (piece.chessKey.side == ChessSide.RED)
                {
                    y0 = 0;
                    y1 = 4;
                }
                else if (piece.chessKey.side == ChessSide.BLACK)
                {
                    y0 = 5;
                    y1 = 9;
                }
                if (piece.x - 2 >= 0 && piece.y + 2 <= y1)
                {
                    if (gameObjectsArray[piece.x - 1, piece.y + 1] == null)
                    {
                        if (gameObjectsArray[piece.x - 2, piece.y + 2] != null &&
                            gameObjectsArray[piece.x - 2, piece.y + 2].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x - 2, piece.y + 2));
                        }
                        else if (gameObjectsArray[piece.x - 2, piece.y + 2] == null)
                        {
                            moveables.Add(new Vector2(piece.x - 2, piece.y + 2));
                        }
                    }
                }
                if (piece.x - 2 >= 0 && piece.y - 2 >= y0)
                {
                    if (gameObjectsArray[piece.x - 1, piece.y - 1] == null)
                    {
                        if (gameObjectsArray[piece.x - 2, piece.y - 2] != null &&
                             gameObjectsArray[piece.x - 2, piece.y - 2].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x - 2, piece.y - 2));
                        }
                        else if (gameObjectsArray[piece.x - 2, piece.y - 2] == null)
                        {
                            moveables.Add(new Vector2(piece.x - 2, piece.y - 2));
                        }
                    }
                }
                if (piece.x + 2 < xColumn && piece.y + 2 <= y1)
                {
                    if (gameObjectsArray[piece.x + 1, piece.y + 1] == null)
                    {
                        if (gameObjectsArray[piece.x + 2, piece.y + 2] != null &&
                        gameObjectsArray[piece.x + 2, piece.y + 2].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x + 2, piece.y + 2));
                        }
                        else if (gameObjectsArray[piece.x + 2, piece.y + 2] == null)
                        {
                            moveables.Add(new Vector2(piece.x + 2, piece.y + 2));
                        }
                    }
                }
                if (piece.x + 2 < xColumn && piece.y - 2 >= y0)
                {
                    if (gameObjectsArray[piece.x + 1, piece.y - 1] == null)
                    {
                        if (gameObjectsArray[piece.x + 2, piece.y - 2] != null &&
                            gameObjectsArray[piece.x + 2, piece.y - 2].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x + 2, piece.y - 2));
                        }
                        else if (gameObjectsArray[piece.x + 2, piece.y - 2] == null)
                        {
                            moveables.Add(new Vector2(piece.x + 2, piece.y - 2));
                        }

                    }
                }
                break;
            case ChessType.Horse:
                if (piece.x - 1 >= 0 && piece.y + 2 < yRow)
                {
                    if (gameObjectsArray[piece.x, piece.y + 1] == null)
                    {
                        if (gameObjectsArray[piece.x - 1, piece.y + 2] != null &&
                            gameObjectsArray[piece.x - 1, piece.y + 2].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x - 1, piece.y + 2));
                        }
                        else if (gameObjectsArray[piece.x - 1, piece.y + 2] == null)
                        {
                            moveables.Add(new Vector2(piece.x - 1, piece.y + 2));
                        }

                    }
                }
                if (piece.x - 2 >= 0 && piece.y + 1 < yRow)
                {
                    if (gameObjectsArray[piece.x - 1, piece.y] == null)
                    {
                        if (gameObjectsArray[piece.x - 2, piece.y + 1] != null &&
                                gameObjectsArray[piece.x - 2, piece.y + 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x - 2, piece.y + 1));
                        }
                        else if (gameObjectsArray[piece.x - 2, piece.y + 1] == null)
                        {
                            moveables.Add(new Vector2(piece.x - 2, piece.y + 1));
                        }

                    }
                }

                if (piece.x + 1 < xColumn && piece.y + 2 < yRow)
                {
                    if (gameObjectsArray[piece.x, piece.y + 1] == null)
                    {
                        if (gameObjectsArray[piece.x + 1, piece.y + 2] != null &&
                             gameObjectsArray[piece.x + 1, piece.y + 2].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x + 1, piece.y + 2));
                        }
                        else if (gameObjectsArray[piece.x + 1, piece.y + 2] == null)
                        {
                            moveables.Add(new Vector2(piece.x + 1, piece.y + 2));
                        }
                    }
                }
                if (piece.x + 2 < xColumn && piece.y + 1 < yRow)
                {
                    if (gameObjectsArray[piece.x + 1, piece.y] == null)
                    {
                        if (gameObjectsArray[piece.x + 2, piece.y + 1] != null &&
                                gameObjectsArray[piece.x + 2, piece.y + 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x + 2, piece.y + 1));
                        }
                        else if (gameObjectsArray[piece.x + 2, piece.y + 1] == null)
                        {
                            moveables.Add(new Vector2(piece.x + 2, piece.y + 1));
                        }
                    }
                }


                if (piece.x - 1 >= 0 && piece.y - 2 >= 0)
                {
                    if (gameObjectsArray[piece.x, piece.y - 1] == null)
                    {
                        if (gameObjectsArray[piece.x - 1, piece.y - 2] != null &&
                             gameObjectsArray[piece.x - 1, piece.y - 2].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x - 1, piece.y - 2));
                        }
                        else if (gameObjectsArray[piece.x - 1, piece.y - 2] == null)
                        {
                            moveables.Add(new Vector2(piece.x - 1, piece.y - 2));
                        }
                    }
                }
                if (piece.x - 2 >= 0 && piece.y - 1 >= 0)
                {
                    if (gameObjectsArray[piece.x - 1, piece.y] == null)
                    {
                        if (gameObjectsArray[piece.x - 2, piece.y - 1] != null &&
                             gameObjectsArray[piece.x - 2, piece.y - 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x - 2, piece.y - 1));
                        }
                        else if (gameObjectsArray[piece.x - 2, piece.y - 1] == null)
                        {
                            moveables.Add(new Vector2(piece.x - 2, piece.y - 1));
                        }
                    }
                }

                if (piece.x + 1 < xColumn && piece.y - 2 >= 0)
                {
                    if (gameObjectsArray[piece.x, piece.y - 1] == null)
                    {
                        if (gameObjectsArray[piece.x + 1, piece.y - 2] != null &&
                             gameObjectsArray[piece.x + 1, piece.y - 2].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x + 1, piece.y - 2));
                        }
                        else if (gameObjectsArray[piece.x + 1, piece.y - 2] == null)
                        {
                            moveables.Add(new Vector2(piece.x + 1, piece.y - 2));
                        }
                    }
                }
                if (piece.x + 2 < xColumn && piece.y - 1 >= 0)
                {
                    if (gameObjectsArray[piece.x + 1, piece.y] == null)
                    {
                        if (gameObjectsArray[piece.x + 2, piece.y - 1] != null &&
                             gameObjectsArray[piece.x + 2, piece.y - 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                        {
                            moveables.Add(new Vector2(piece.x + 2, piece.y - 1));
                        }
                        else if (gameObjectsArray[piece.x + 2, piece.y - 1] == null)
                        {
                            moveables.Add(new Vector2(piece.x + 2, piece.y - 1));
                        }
                    }
                }
                break;
            case ChessType.General:

                if (piece.chessKey.side == ChessSide.RED)
                {
                    y2 = 0;
                    y3 = 2;
                    for (int y = piece.y + 1; y < yRow; y++)
                    {
                        if (gameObjectsArray[piece.x, y] != null &&
                            gameObjectsArray[piece.x, y].GetComponent<Piece>().chessKey.type == ChessType.General)
                        {
                            moveables.Add(new Vector2(piece.x, y));
                        }
                        else if (gameObjectsArray[piece.x, y] != null &&
                          gameObjectsArray[piece.x, y].GetComponent<Piece>().chessKey.type != ChessType.General)
                        {
                            break;
                        }
                        else if (gameObjectsArray[piece.x, y] == null)
                        {
                            continue;
                        }
                    }
                }
                else if (piece.chessKey.side == ChessSide.BLACK)
                {
                    y2 = 7;
                    y3 = 9;
                    for (int y = piece.y - 1; y >= 0; y--)
                    {
                        if (gameObjectsArray[piece.x, y] != null &&
                            gameObjectsArray[piece.x, y].GetComponent<Piece>().chessKey.type == ChessType.General)
                        {
                            moveables.Add(new Vector2(piece.x, y));
                        }
                        else if (gameObjectsArray[piece.x, y] != null &&
                          gameObjectsArray[piece.x, y].GetComponent<Piece>().chessKey.type != ChessType.General)
                        {
                            break;
                        }
                        else if (gameObjectsArray[piece.x, y] == null)
                        {
                            continue;
                        }
                    }
                }
                if (piece.x - 1 >= x0)
                {
                    if (gameObjectsArray[piece.x - 1, piece.y] != null &&
                        gameObjectsArray[piece.x - 1, piece.y].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                    {
                        moveables.Add(new Vector2(piece.x - 1, piece.y));
                    }
                    else if (gameObjectsArray[piece.x - 1, piece.y] == null)
                    {
                        moveables.Add(new Vector2(piece.x - 1, piece.y));
                    }
                }
                if (piece.y - 1 >= y2)
                {
                    if (gameObjectsArray[piece.x, piece.y - 1] != null &&
                         gameObjectsArray[piece.x, piece.y - 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                    {
                        moveables.Add(new Vector2(piece.x, piece.y - 1));
                    }
                    else if (gameObjectsArray[piece.x, piece.y - 1] == null)
                    {
                        moveables.Add(new Vector2(piece.x, piece.y - 1));
                    }
                }
                if (piece.x + 1 <= x1)
                {
                    if (gameObjectsArray[piece.x + 1, piece.y] != null &&
                    gameObjectsArray[piece.x + 1, piece.y].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                    {
                        moveables.Add(new Vector2(piece.x + 1, piece.y));
                    }
                    else if (gameObjectsArray[piece.x + 1, piece.y] == null)
                    {
                        moveables.Add(new Vector2(piece.x + 1, piece.y));
                    }
                }
                if (piece.y + 1 <= y3)
                {
                    if (gameObjectsArray[piece.x, piece.y + 1] != null &&
                        gameObjectsArray[piece.x, piece.y + 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                    {
                        moveables.Add(new Vector2(piece.x, piece.y + 1));
                    }
                    else if (gameObjectsArray[piece.x, piece.y + 1] == null)
                    {
                        moveables.Add(new Vector2(piece.x, piece.y + 1));
                    }
                }

                break;
            case ChessType.Advisor:

                // int x0 = 3;
                // int x1 = 5;
                // int y2 = -1;
                // int y3 = -1;
                if (piece.chessKey.side == ChessSide.RED)
                {
                    y2 = 0;
                    y3 = 2;
                }
                else if (piece.chessKey.side == ChessSide.BLACK)
                {
                    y2 = 7;
                    y3 = 9;
                }
                if (piece.x - 1 >= x0 && piece.y + 1 <= y3)
                {
                    if (gameObjectsArray[piece.x - 1, piece.y + 1] != null &&
                        gameObjectsArray[piece.x - 1, piece.y + 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                    {
                        moveables.Add(new Vector2(piece.x - 1, piece.y + 1));
                    }
                    else if (gameObjectsArray[piece.x - 1, piece.y + 1] == null)
                    {
                        moveables.Add(new Vector2(piece.x - 1, piece.y + 1));
                    }
                }
                if (piece.x - 1 >= x0 && piece.y - 1 >= y2)
                {
                    if (gameObjectsArray[piece.x - 1, piece.y - 1] != null &&
                         gameObjectsArray[piece.x - 1, piece.y - 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                    {
                        moveables.Add(new Vector2(piece.x - 1, piece.y - 1));
                    }
                    else if (gameObjectsArray[piece.x - 1, piece.y - 1] == null)
                    {
                        moveables.Add(new Vector2(piece.x - 1, piece.y - 1));
                    }
                }
                if (piece.x + 1 <= x1 && piece.y + 1 <= y3)
                {
                    if (gameObjectsArray[piece.x + 1, piece.y + 1] != null &&
                    gameObjectsArray[piece.x + 1, piece.y + 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                    {
                        moveables.Add(new Vector2(piece.x + 1, piece.y + 1));
                    }
                    else if (gameObjectsArray[piece.x + 1, piece.y + 1] == null)
                    {
                        moveables.Add(new Vector2(piece.x + 1, piece.y + 1));
                    }
                }
                if (piece.x + 1 <= x1 && piece.y - 1 >= y2)
                {
                    if (gameObjectsArray[piece.x + 1, piece.y - 1] != null &&
                        gameObjectsArray[piece.x + 1, piece.y - 1].GetComponent<Piece>().chessKey.side != piece.chessKey.side)
                    {
                        moveables.Add(new Vector2(piece.x + 1, piece.y - 1));
                    }
                    else if (gameObjectsArray[piece.x + 1, piece.y - 1] == null)
                    {
                        moveables.Add(new Vector2(piece.x + 1, piece.y - 1));
                    }
                }
                break;
            case ChessType.Barrier:

                break;
            default:
                break;
        }

        return moveables;
    }
    public void onMousePressEvents(Piece piece) {


        if (selectPiece != null)
        {
            return;
        }
        selectPiece = piece;
        if (selectPiece.chessKey.side != currentChessSide)
        {
            selectPiece = null;
            return;
        }

        List<Vector2> moveablepiece = getMoveable(piece);
        if (moveablepiece.Count <= 0)
        {
            selectPiece = null;
            return;
        }
        foreach (Vector2 item in moveablepiece)
        {
            GameObject tempGameObject = Instantiate(chessPrefab, correctPosition(item.x, item.y, -3), Quaternion.identity);
            Piece tempPiece = tempGameObject.GetComponent<Piece>();
            ChessKey tempChessKey = new ChessKey();
            tempChessKey.side = ChessSide.Barrier;
            tempChessKey.type = ChessType.Barrier;
            tempPiece.init((int)(item.x), (int)item.y, tempChessKey);
            BarrierGameObject.AddFirst(tempGameObject);
        }
    }

    public void onMouseUpEvents() {


        if (lastSelectPiece == null)
        {
            return;
        }

        chessArray[selectPiece.x, selectPiece.y] = -1;
        gameObjectsArray[selectPiece.x, selectPiece.y] = null;
        chessArray[lastSelectPiece.x, lastSelectPiece.y] = (int)selectPiece.chessKey.type;
        selectPiece.x = lastSelectPiece.x;
        selectPiece.y = lastSelectPiece.y;
        selectPiece.transform.position = lastSelectPiece.transform.position;
        if (gameObjectsArray[lastSelectPiece.x, lastSelectPiece.y] != null)
        {
            DestroyImmediate(gameObjectsArray[lastSelectPiece.x, lastSelectPiece.y]);

        }
        gameObjectsArray[lastSelectPiece.x, lastSelectPiece.y] = selectPiece.transform.gameObject;

        foreach (GameObject item in BarrierGameObject)
        {
            DestroyImmediate(item);
        }
        //Debug.Log(currentChessSide);
        currentChessSide = SwitchSide.instance.chessSideMethon(currentChessSide);
        selectPiece = null;
        lastSelectPiece = null;

    }
    public void onMouseOverEvents(Piece piece)
    {
        if (piece.chessKey.type == ChessType.Barrier)
        {
            lastSelectPiece = piece;
            selectPiece.transform.position = piece.transform.position;
        }
    }
    void Update()
    {
        if (selectPiece != null && lastSelectPiece == null & Input.GetMouseButtonDown(1))
        {
            selectPiece = null;
            foreach (GameObject item in BarrierGameObject)
            {
                DestroyImmediate(item);
            }
        }
    }
    public void restart(){
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);   
  }
}

3 ChessMove

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

public class ChessMove : MonoBehaviour
{
    // Start is called before the first frame update
   public Piece piece;
    private void Awake()
    {
        piece = this.transform.GetComponent<Piece>();
    }
    private void OnMouseDown()
    {
        //Debug.Log(piece.ToString());
        ChessManger.instance.onMousePressEvents(this.piece);
    }
    private void OnMouseUp()
    {
        ChessManger.instance.onMouseUpEvents();
        //Debug.Log(piece.ToString());
    }
  
    private void OnMouseEnter()
    {
        ChessManger.instance.onMouseOverEvents(this.piece);
        //Debug.Log("last piece:" + piece.ToString());
    }
}

4 Piece

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

public class Piece : MonoBehaviour
{
    public int x;
    public int y;
    public ChessKey chessKey;
    SpriteRenderer spriteRenderer;
    // Start is called before the first frame update
    private void Awake()
    {
        spriteRenderer =this.transform.GetComponent<SpriteRenderer>();
    }
    public void init(int x, int y, ChessKey chessKey) {
        this.x = x;
        this.y = y;
        this.chessKey = chessKey;
        //Debug.Log("PieceDirect.getSprite(chessKey)" + PieceDirect.getSprite(chessKey).name);
        spriteRenderer.sprite = PieceDirect.getSprite(chessKey);
    
    }
    public override string ToString()
    {
        return "x:" + x + "y:" + y + "chessSide:" + chessKey.side + "chessType:" + chessKey.type;
    }
}

5 PieceDirect

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

public class PieceDirect : MonoBehaviour
{
    [System.Serializable]
    public struct ChessSideStruct
    {
        public ChessSide side;
        public Sprite sprite;
    }
    [System.Serializable]
    public struct ChessTypeStruct
    {
        public ChessType type;
        public ChessSideStruct sideStruct;
    }
    public ChessTypeStruct[] chessStructs;
    // Start is called before the first frame update
  
    public static   Dictionary<ChessKey,Sprite> chessDirct;

    void Awake()
    {
        chessDirct = new Dictionary<ChessKey, Sprite>();
        for (int i = 0; i < chessStructs.Length; i++)
        {
            ChessType chessType = chessStructs[i].type;
            ChessSideStruct chessSideStruct = chessStructs[i].sideStruct;
            ChessSide chessSide = chessSideStruct.side;
            Sprite sprite = chessSideStruct.sprite;
            ChessKey tempchessKey = new ChessKey();
            tempchessKey.side = chessSide;
            tempchessKey.type = chessType;            
            chessDirct.Add(tempchessKey, sprite);
            //Debug.Log(chessDirct.Keys.Count);
        }
    }

    public static Sprite getSprite(ChessKey chessKey)
    {
        Sprite tempSprite =null;
        foreach (ChessKey temp in chessDirct.Keys)
        {
            //Debug.Log(temp.side+":"+temp.type);
            if (temp.side == chessKey.side && temp.type == chessKey.type) {
                tempSprite= chessDirct[temp];
               // Debug.Log("tempSprite" + tempSprite.name);
                break;
            }
        }
        return tempSprite;
    }
}

6 SwitchSide

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SwitchSide : MonoBehaviour
{
    public static SwitchSide instance; 
    public Text sideDes;
    public Image sideSprite;
    public ChessKey tempKey;
     void Awake()
    {
        instance = this;
    }
    public  ChessSide chessSideMethon(ChessSide chessSide) {

        tempKey = new ChessKey();
        tempKey.type = ChessType.General;
        switch (chessSide)
        {
            case ChessSide.RED:
                //sideDes.text = "黑方".ToString();

                tempKey.side = ChessSide.BLACK;
                //sideSprite.sprite = PieceDirect.getSprite(tempKey);
                Debug.Log(tempKey.side);
                break;
            case ChessSide.BLACK:
               // sideDes.text = "红方".ToString();
                tempKey.side = ChessSide.RED;
                //sideSprite.sprite = PieceDirect.getSprite(tempKey);
                Debug.Log(tempKey.side);
                break;
            case ChessSide.Barrier:
                break;
            default:
                break;
        }
        Debug.Log(tempKey.side);
        return tempKey.side;
    }
     void Update()
    {
        if (tempKey==null)
        {
            return;
        }
        switch (tempKey.side)
        {
            case ChessSide.BLACK:
                sideDes.text = "黑方".ToString();

                //tempKey.side = ChessSide.BLACK;
                sideSprite.sprite = PieceDirect.getSprite(tempKey);
                Debug.Log(tempKey.side);
                break;
            case ChessSide.RED:
                sideDes.text = "红方".ToString();
                //tempKey.side = ChessSide.RED;
                sideSprite.sprite = PieceDirect.getSprite(tempKey);
                //Debug.Log(tempKey.side);
                break;
        }
    }
}
  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值