feat: 重构多人游戏界面和代码,使用文件存储游戏配置,重构底层代码

This commit is contained in:
ZZY
2024-11-06 22:13:03 +08:00
parent d323a0bee7
commit d6cbb5e11d
30 changed files with 715 additions and 262 deletions

View File

@ -0,0 +1,59 @@
using Godot;
using System.Collections.Generic;
using System.IO;
class GodotConfigManager {
private readonly string ConfigFilePath;
private readonly ConfigFile config = new();
private readonly bool successful;
public GodotConfigManager(string ConfigFilePath) {
this.ConfigFilePath = ConfigFilePath;
Error err = config.Load(ConfigFilePath);
if (err == Error.Ok) {
successful = true;
} else {
successful = false;
GD.PrintErr("config.Load", err);
// OS.Alert("配置文件加载失败!", "错误");
return;
}
}
public void LoadConfig(string SessionName,
Dictionary<string, Variant> data) {
// 如果文件没有加载,忽略它。
if (!successful) {
return;
}
// 迭代所有小节。
foreach (string session in config.GetSections()) {
if (session == SessionName) {
foreach (var key in config.GetSectionKeys(session)) {
if (data.ContainsKey(key)) {
data[key] = config.GetValue(session, key);
}
}
}
}
}
public void SaveConfig(string SessionName,
Dictionary<string, Variant> data) {
// 将 GlobalConfig 中的键值对写入配置文件
foreach (var key in data.Keys)
{
GD.Print(data[key]);
config.SetValue(SessionName, key, data[key]);
}
// 保存配置文件
if (config.Save(ConfigFilePath) != Error.Ok) {
OS.Alert("配置文件保存失败!", "错误");
}
}
}

View File

@ -7,8 +7,8 @@ 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; // 撤销记录时的回调
private readonly Action<T, T, Vector2, Vector2> onAddRecordCallback; // 添加记录时的回调
private readonly Action<T, T, Vector2, Vector2> onUndoRecordCallback; // 撤销记录时的回调
public MoveRecords(
Action<T, T, Vector2, Vector2> onAddRecordCallback = null,

View File

@ -1,101 +0,0 @@
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));
}
}
}
}

View File

@ -1,40 +0,0 @@
// 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;
}
}