bugfix 重新提交暂存区
This commit is contained in:
59
Scripts/Utilities/MoveRecords.cs
Normal file
59
Scripts/Utilities/MoveRecords.cs
Normal file
@ -0,0 +1,59 @@
|
||||
// using System.Numerics;
|
||||
using Godot;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
public class MoveRecords<T> {
|
||||
private readonly LinkedList<MoveRecord> records = new LinkedList<MoveRecord>(); // 使用队列替换栈
|
||||
private readonly int maxRecords; // 记录上限
|
||||
private Action<T, T, Vector2, Vector2> onAddRecordCallback; // 添加记录时的回调
|
||||
private Action<T, T, Vector2, Vector2> onUndoRecordCallback; // 撤销记录时的回调
|
||||
|
||||
public MoveRecords(
|
||||
Action<T, T, Vector2, Vector2> onAddRecordCallback = null,
|
||||
Action<T, T, Vector2, Vector2> onUndoRecordCallback = null,
|
||||
int maxRecords = 32) {
|
||||
this.maxRecords = maxRecords;
|
||||
this.onAddRecordCallback = onAddRecordCallback;
|
||||
this.onUndoRecordCallback = onUndoRecordCallback;
|
||||
}
|
||||
|
||||
public void AddRecord(T newNode, T 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 T NewNode { get; }
|
||||
public T OldNode { get; }
|
||||
public Vector2 NewPos { get; }
|
||||
public Vector2 OldPos { get; }
|
||||
|
||||
public MoveRecord(T newNode, T oldNode, Vector2 newPos, Vector2 oldPos) {
|
||||
NewNode = newNode;
|
||||
OldNode = oldNode;
|
||||
OldPos = oldPos;
|
||||
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
|
||||
);
|
||||
}
|
101
Scripts/Utilities/VirtualBoard.cs
Normal file
101
Scripts/Utilities/VirtualBoard.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using System;
|
||||
// using System.Numerics;
|
||||
// using System.Transactions;
|
||||
using Godot;
|
||||
|
||||
public class VirtualBoard {
|
||||
private readonly int Rows;
|
||||
private readonly int Cols;
|
||||
private readonly VirtualPiece[,] pieces;
|
||||
|
||||
public class SetPiecePosEventArgs : EventArgs
|
||||
{
|
||||
public VirtualPiece OldPiece { get; set; }
|
||||
public VirtualPiece NewPiece { get; set; }
|
||||
}
|
||||
|
||||
public class MoveEventArgs : EventArgs
|
||||
{
|
||||
public Vector2 From { get; set; }
|
||||
public Vector2 To { get; set; }
|
||||
}
|
||||
|
||||
public event EventHandler<SetPiecePosEventArgs> OnSetPiecePos;
|
||||
public event EventHandler<VirtualPiece> OnInsert;
|
||||
public event EventHandler<VirtualPiece> OnRemove;
|
||||
public event EventHandler<MoveEventArgs> OnMove;
|
||||
|
||||
public VirtualBoard(int rows, int cols) {
|
||||
this.Rows = rows;
|
||||
this.Cols = cols;
|
||||
pieces = new VirtualPiece[rows, cols];
|
||||
}
|
||||
|
||||
public bool ArrPosOutOfRange(Vector2 arrayPos) {
|
||||
return arrayPos.X < 0 || arrayPos.X >= Rows || arrayPos.Y < 0 || arrayPos.Y >= Cols;
|
||||
}
|
||||
|
||||
public VirtualPiece GetPiece(Vector2 arrayPos) {
|
||||
if (ArrPosOutOfRange(arrayPos)) return null;
|
||||
return pieces[(int)arrayPos.X, (int)arrayPos.Y];
|
||||
}
|
||||
|
||||
public VirtualPiece SetPiecePos(VirtualPiece piece, Vector2 arrayPos) {
|
||||
if (ArrPosOutOfRange(arrayPos)) return null;
|
||||
|
||||
VirtualPiece oldPiece = pieces[(int)arrayPos.X, (int)arrayPos.Y];
|
||||
pieces[(int)arrayPos.X, (int)arrayPos.Y] = piece;
|
||||
|
||||
OnSetPiecePos?.Invoke(this, new SetPiecePosEventArgs { OldPiece = oldPiece, NewPiece = piece });
|
||||
|
||||
piece?.Move(arrayPos);
|
||||
|
||||
return oldPiece;
|
||||
}
|
||||
|
||||
public bool InsertPiece(VirtualPiece piece, Vector2 arrayPos) {
|
||||
if (GetPiece(arrayPos) != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
OnInsert?.Invoke(this, piece);
|
||||
SetPiecePos(piece, arrayPos);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool MovePiece(Vector2 from, Vector2 to) {
|
||||
if (ArrPosOutOfRange(to) || ArrPosOutOfRange(from)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (GetPiece(to) != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
OnMove?.Invoke(this, new MoveEventArgs { From = from, To = to });
|
||||
|
||||
SetPiecePos(SetPiecePos(null, from), to);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool RemovePiece(Vector2 pos) {
|
||||
VirtualPiece piece = GetPiece(pos);
|
||||
if (piece == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
OnRemove?.Invoke(this, piece);
|
||||
|
||||
return SetPiecePos(null, pos) != null;
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
for (int i = 0; i < Rows; i++) {
|
||||
for (int j = 0; j < Cols; j++) {
|
||||
RemovePiece(new Vector2(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
40
Scripts/Utilities/VirtualPiece.cs
Normal file
40
Scripts/Utilities/VirtualPiece.cs
Normal file
@ -0,0 +1,40 @@
|
||||
// using System.Numerics;
|
||||
using Godot;
|
||||
|
||||
using System;
|
||||
|
||||
public class VirtualPiece {
|
||||
private Vector2 pos; // 注意这个坐标的非像素坐标而是棋盘坐标
|
||||
|
||||
private string name;
|
||||
private bool isSelected;
|
||||
public object data;
|
||||
|
||||
public event Action<Vector2> OnMove;
|
||||
public event Action<bool> OnSelected;
|
||||
|
||||
public void Move(Vector2 pos) {
|
||||
this.pos = pos;
|
||||
OnMove?.Invoke(pos);
|
||||
}
|
||||
|
||||
public Vector2 Pos() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
public void Selected(bool isSelected) {
|
||||
if (this.isSelected != isSelected) {
|
||||
OnSelected?.Invoke(isSelected);
|
||||
this.isSelected = isSelected;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSelected() {
|
||||
return isSelected;
|
||||
}
|
||||
|
||||
public VirtualPiece(string name = "", Vector2 pos = new Vector2()) {
|
||||
this.name = name;
|
||||
this.pos = pos;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user