init basic signal player chinese chess game
This commit is contained in:
57
Scripts/Utilities/MoveRecords.cs
Normal file
57
Scripts/Utilities/MoveRecords.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using Godot;
|
||||
using System.Linq;
|
||||
|
||||
public class MoveRecords {
|
||||
private readonly LinkedList<MoveRecord> records = new LinkedList<MoveRecord>(); // 使用队列替换栈
|
||||
private readonly int maxRecords; // 记录上限
|
||||
private Action<Node, Node, Vector2, Vector2> onAddRecordCallback; // 添加记录时的回调
|
||||
private Action<Node, Node, Vector2, Vector2> onUndoRecordCallback; // 撤销记录时的回调
|
||||
public MoveRecords(
|
||||
Action<Node, Node, Vector2, Vector2> onAddRecordCallback = null,
|
||||
Action<Node, Node, Vector2, Vector2> onUndoRecordCallback = null,
|
||||
int maxRecords = 32) {
|
||||
this.maxRecords = maxRecords;
|
||||
this.onAddRecordCallback = onAddRecordCallback;
|
||||
this.onUndoRecordCallback = onUndoRecordCallback;
|
||||
}
|
||||
|
||||
public void AddRecord(Node newNode, Node oldNode, Vector2 newPos, Vector2 oldPos) {
|
||||
// 达到记录上限时,移除最远的记录(队首元素)
|
||||
if (records.Count >= maxRecords) {
|
||||
records.RemoveFirst();
|
||||
}
|
||||
var record = new MoveRecord(newNode, oldNode, newPos, oldPos);
|
||||
// 触发添加记录的回调
|
||||
onAddRecordCallback?.Invoke(newNode, oldNode, newPos, oldPos);
|
||||
// GD.Print("In func Addrecord: ", record.NewNode, "->", record.OldNode, ":", record.NewPos, "->", record.OldPos);
|
||||
records.AddLast(record); // 将新记录加入队尾
|
||||
}
|
||||
|
||||
public void Undo() {
|
||||
if (records.Count == 0) return;
|
||||
MoveRecord record = records.Last.Value; // 移除并获取队首的记录以执行撤销操作
|
||||
records.RemoveLast();
|
||||
// 触发撤销记录的回调
|
||||
onUndoRecordCallback?.Invoke(record.NewNode, record.OldNode, record.NewPos, record.OldPos);
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
records.Clear();
|
||||
}
|
||||
|
||||
private class MoveRecord {
|
||||
public Node NewNode { get; }
|
||||
public Node OldNode { get; }
|
||||
public Vector2 NewPos { get; }
|
||||
public Vector2 OldPos { get; }
|
||||
|
||||
public MoveRecord(Node newNode, Node oldNode, Vector2 newPos, Vector2 oldPos) {
|
||||
this.NewNode = newNode;
|
||||
this.OldNode = oldNode;
|
||||
this.OldPos = oldPos;
|
||||
this.NewPos = newPos;
|
||||
}
|
||||
}
|
||||
}
|
10
Scripts/Utilities/Transforms.cs
Normal file
10
Scripts/Utilities/Transforms.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using Godot;
|
||||
|
||||
public static class PosTrans {
|
||||
private static readonly int pixGripSize = 32;
|
||||
public static Transform2D transArrToPix = new(
|
||||
new Vector2(pixGripSize, 0),
|
||||
new Vector2(0, pixGripSize),
|
||||
new Vector2(-4, -4.5f) * pixGripSize
|
||||
);
|
||||
}
|
316
Scripts/Utilities/VirtualBoard.cs
Normal file
316
Scripts/Utilities/VirtualBoard.cs
Normal file
@ -0,0 +1,316 @@
|
||||
using System.Dynamic;
|
||||
using Godot;
|
||||
|
||||
class VirtualBoard {
|
||||
private static readonly int boradRows = 9;
|
||||
private static readonly int boradCols = 10;
|
||||
private readonly Node BoardRoot;
|
||||
private readonly Node[,] nodesBorad = new Node[boradRows, boradCols];
|
||||
|
||||
public static bool ArrPosOutOfRange(Vector2 arrayPos) {
|
||||
return arrayPos.X < 0 || arrayPos.X >= boradRows || arrayPos.Y < 0 || arrayPos.Y >= boradCols;
|
||||
}
|
||||
|
||||
public Node getNodeFromBoard(Vector2 arrayPos) {
|
||||
if (ArrPosOutOfRange(arrayPos)) return null;
|
||||
return nodesBorad[(int)arrayPos.X, (int)arrayPos.Y];
|
||||
}
|
||||
|
||||
public void setNodeToBoard(Vector2 arrayPos, Node node) {
|
||||
if (ArrPosOutOfRange(arrayPos)) return;
|
||||
nodesBorad[(int)arrayPos.X, (int)arrayPos.Y] = node;
|
||||
}
|
||||
|
||||
public VirtualBoard(Node root = null) {
|
||||
//?? //GetTree().CurrentScene; // 如果未提供root,默认为当前场景
|
||||
BoardRoot = root;
|
||||
}
|
||||
|
||||
public bool MoveNode(Vector2 newPos, Vector2 oldPos) {
|
||||
if (ArrPosOutOfRange(newPos) || ArrPosOutOfRange(oldPos)) {
|
||||
return false;
|
||||
}
|
||||
if (getNodeFromBoard(newPos) != null) {
|
||||
return false;
|
||||
}
|
||||
if (getNodeFromBoard(oldPos) is ChessPiece chessPiece) {
|
||||
chessPiece.movePos(newPos);
|
||||
}
|
||||
setNodeToBoard(newPos, getNodeFromBoard(oldPos));
|
||||
setNodeToBoard(oldPos, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool RemoveNode(Vector2 arrayPos) {
|
||||
if (ArrPosOutOfRange(arrayPos)) return false;
|
||||
if (getNodeFromBoard(arrayPos) is ChessPiece chessPiece) {
|
||||
chessPiece.QueueFree();
|
||||
}
|
||||
setNodeToBoard(arrayPos, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void InsertNode(ChessPiece chess, Vector2 arrayPos) {
|
||||
chess.Position = PosTrans.transArrToPix * arrayPos;
|
||||
setNodeToBoard(arrayPos, chess);
|
||||
BoardRoot.AddChild(chess);
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
foreach (var node in BoardRoot.GetChildren()) {
|
||||
if (node is ChessPiece chessPiece) {
|
||||
chessPiece.QueueFree();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// using Godot;
|
||||
|
||||
// public enum ChessType
|
||||
// {
|
||||
// Carriage,
|
||||
// Horse,
|
||||
// Elephant,
|
||||
// Advisor,
|
||||
// General,
|
||||
// Cannon,
|
||||
// Pawn
|
||||
// }
|
||||
|
||||
// public class VirtualBoard
|
||||
// {
|
||||
// // ...其他原有成员变量...
|
||||
|
||||
// public VirtualBoard(Node root = null)
|
||||
// {
|
||||
// BoardRoot = root ?? GetTree().CurrentScene; // 如果未提供root,默认为当前场景
|
||||
// }
|
||||
|
||||
// public void InitChessBoard()
|
||||
// {
|
||||
// // 定义黑色和红色棋子的初始位置和类型
|
||||
// var positions = new (Vector2 position, ChessType type)[]
|
||||
// {
|
||||
// // 黑方棋子初始化...
|
||||
// // 示例省略具体位置和类型,你需要根据实际情况填写
|
||||
// // (new Vector2(x, y), ChessType.Pawn),
|
||||
|
||||
// // 红方棋子初始化...
|
||||
// // 同上
|
||||
// };
|
||||
|
||||
// // 初始化棋子
|
||||
// foreach (var (position, type) in positions)
|
||||
// {
|
||||
// var color = position.Y == 0 ? new Color("black") : new Color("red"); // 根据行判断颜色
|
||||
// var piece = CreateChessPiece(type, color);
|
||||
// InsertChess(piece, position);
|
||||
// }
|
||||
// }
|
||||
|
||||
// private ChessPiece CreateChessPiece(ChessType type, Color labelColor)
|
||||
// {
|
||||
// // 根据ChessType创建对应的棋子实例并设置Label和颜色
|
||||
// // 这里需要你实现具体的逻辑,例如switch case或映射表来决定创建哪种棋子
|
||||
// // 返回创建的棋子实例
|
||||
// }
|
||||
|
||||
// // ...其他原有方法...
|
||||
// }
|
||||
|
||||
public void InitChessBoard() {
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "车",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(0, 0));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "马",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(1, 0));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "象",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(2, 0));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "士",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(3, 0));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "将",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(4, 0));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "士",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(5, 0));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "象",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(6, 0));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "马",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(7, 0));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "车",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(8, 0));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "炮",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(1, 2));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "炮",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(7, 2));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "卒",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(0, 3));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "卒",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(2, 3));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "卒",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(4, 3));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "卒",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(6, 3));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "卒",
|
||||
LabelColor = new Color("black"),
|
||||
}, new Vector2(8, 3));
|
||||
|
||||
// -----------------------------------
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "车",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(0, 9));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "马",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(1, 9));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "象",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(2, 9));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "士",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(3, 9));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "将",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(4, 9));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "士",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(5, 9));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "象",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(6, 9));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "马",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(7, 9));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "车",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(8, 9));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "炮",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(1, 7));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "炮",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(7, 7));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "卒",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(0, 6));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "卒",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(2, 6));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "卒",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(4, 6));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "卒",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(6, 6));
|
||||
|
||||
InsertNode(new ChessPiece
|
||||
{
|
||||
PieceLabel = "卒",
|
||||
LabelColor = new Color("red"),
|
||||
}, new Vector2(8, 6));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user