bugfix 重新提交暂存区
This commit is contained in:
138
Scripts/Entities/ChessBoard.cs
Normal file
138
Scripts/Entities/ChessBoard.cs
Normal file
@ -0,0 +1,138 @@
|
||||
// Chessboard.cs
|
||||
using Godot;
|
||||
|
||||
public partial class ChessBoard : Node2D {
|
||||
public VirtualBoard board = null;
|
||||
public Player playerSelf = null;
|
||||
|
||||
public delegate bool ChessMoveFunc(Vector2 toPos, Vector2 fromPos);
|
||||
// public Callable chessMoveFunc { get; set; }
|
||||
|
||||
public override void _Ready() {
|
||||
board = new VirtualBoard(9, 10);
|
||||
playerSelf = new Player(board);
|
||||
|
||||
board.OnRemove += (sender, piece) => {
|
||||
if (piece.data != null) {
|
||||
RemoveChild(piece.data as Node);
|
||||
}
|
||||
};
|
||||
|
||||
board.OnInsert += (sender, piece) => {
|
||||
if (piece.data != null) {
|
||||
AddChild(piece.data as Node);
|
||||
}
|
||||
};
|
||||
|
||||
// board.OnMove += (sender, args) => {
|
||||
// // chessMoveFunc.Call(args.To, args.From);
|
||||
// };
|
||||
|
||||
InitChessBoard();
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event) {
|
||||
if (@event is InputEventMouseButton mouseEvent &&
|
||||
mouseEvent.Pressed &&
|
||||
mouseEvent.ButtonIndex == MouseButton.Left) {
|
||||
// HandleMouseClick(GetGlobalTransformWithCanvas().AffineInverse() * mouseButton.Position);
|
||||
HandleMouseClick(GetLocalMousePosition());
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleMouseClick(Vector2 clickPosition) {
|
||||
Vector2 clickBoardPos = (PosTrans.transArrToPix.AffineInverse() *
|
||||
clickPosition).Round();
|
||||
|
||||
playerSelf.HandleBoardPosClick(clickBoardPos);
|
||||
}
|
||||
|
||||
public void InsertNode(ChessPiece node, Vector2 arrayPos) {
|
||||
AddChild(node);
|
||||
VirtualPiece piece = node.GetVirtualPiece();
|
||||
// piece.Move(vector);
|
||||
board.SetPiecePos(piece, arrayPos);
|
||||
}
|
||||
|
||||
public void InitChessBoard() {
|
||||
InitializePieces("black", 0, new[] {
|
||||
("车", 0, 0), ("马", 1, 0), ("象", 2, 0),
|
||||
("士", 3, 0), ("将", 4, 0), ("士", 5, 0),
|
||||
("象", 6, 0), ("马", 7, 0), ("车", 8, 0),
|
||||
("炮", 1, 2), ("炮", 7, 2),
|
||||
("卒", 0, 3), ("卒", 2, 3), ("卒", 4, 3), ("卒", 6, 3), ("卒", 8, 3)
|
||||
});
|
||||
|
||||
InitializePieces("red", 9, new[] {
|
||||
("车", 0, -0), ("马", 1, -0), ("象", 2, -0),
|
||||
("士", 3, -0), ("将", 4, -0), ("士", 5, -0),
|
||||
("象", 6, -0), ("马", 7, -0), ("车", 8, -0),
|
||||
("炮", 1, -2), ("炮", 7, -2),
|
||||
("卒", 0, -3), ("卒", 2, -3), ("卒", 4, -3), ("卒", 6, -3), ("卒", 8, -3)
|
||||
});
|
||||
}
|
||||
|
||||
private void InitializePieces(string color, int baseY, (string label, int x, int y)[] positions) {
|
||||
foreach (var (label, x, y) in positions) {
|
||||
InsertNode(new ChessPiece {
|
||||
PieceLabel = label,
|
||||
LabelColor = new Color(color)
|
||||
}, new Vector2(x, baseY + y));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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或映射表来决定创建哪种棋子
|
||||
// // 返回创建的棋子实例
|
||||
// }
|
||||
|
||||
// // ...其他原有方法...
|
||||
// }
|
||||
}
|
74
Scripts/Entities/ChessPiece.cs
Normal file
74
Scripts/Entities/ChessPiece.cs
Normal file
@ -0,0 +1,74 @@
|
||||
// Chesspiece.cs
|
||||
using Godot;
|
||||
|
||||
public partial class ChessPiece : Sprite2D {
|
||||
// 文字内容
|
||||
[Export]
|
||||
public string PieceLabel { get; set; } = null;
|
||||
// 文字颜色(可导出以编辑器调整)
|
||||
[Export]
|
||||
public Color LabelColor { get; set; } = new Color("black");
|
||||
private Vector2 textureSize;
|
||||
|
||||
private Label labelOfChessName;
|
||||
private VirtualPiece piece;
|
||||
|
||||
public VirtualPiece GetVirtualPiece() {
|
||||
return piece;
|
||||
}
|
||||
|
||||
private void OnMove(Vector2 newPos) {
|
||||
Position = PosTrans.transArrToPix * new Vector2(newPos.X, newPos.Y);
|
||||
}
|
||||
|
||||
public void OnSelected(bool isSelected) {
|
||||
if (isSelected) {
|
||||
GD.Print($"{piece.Pos()} is selected");
|
||||
Transform *= transToSeleted;
|
||||
} else {
|
||||
GD.Print($"{piece.Pos()} is deselected");
|
||||
Transform *= transToSeleted.AffineInverse();
|
||||
}
|
||||
}
|
||||
|
||||
Transform2D transToSeleted = new Transform2D(
|
||||
new Vector2(1.2f, 0),
|
||||
new Vector2(0, 1.2f),
|
||||
new Vector2(0, 0)
|
||||
);
|
||||
|
||||
public ChessPiece() : this("", Vector2.Zero){
|
||||
}
|
||||
|
||||
public ChessPiece(string name, Vector2 pos) {
|
||||
PieceLabel = name;
|
||||
piece = new VirtualPiece(name, pos);
|
||||
piece.OnMove += OnMove;
|
||||
piece.OnSelected += OnSelected;
|
||||
piece.data = this;
|
||||
}
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready() {
|
||||
InitLabel();
|
||||
}
|
||||
|
||||
private void InitLabel() {
|
||||
// this.Texture.ResourcePath = "res://Asserts/ChesspieceBase.tres";
|
||||
Texture ??= (Texture2D)ResourceLoader.Load("res://Asserts/ChesspieceBase.tres");
|
||||
textureSize = Texture.GetSize();
|
||||
Vector2 labalPosition = new(
|
||||
-textureSize.X / 2,
|
||||
-textureSize.Y / 2);
|
||||
labelOfChessName = new Label {
|
||||
Text = PieceLabel,
|
||||
HorizontalAlignment = HorizontalAlignment.Center,
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
Modulate = LabelColor,
|
||||
Position = labalPosition,
|
||||
Size = textureSize,
|
||||
};
|
||||
// labelOfChessName.SetAnchorsPreset(Control.LayoutPreset.FullRect);
|
||||
AddChild(labelOfChessName);
|
||||
}
|
||||
}
|
0
Scripts/Entities/ChessPieces/ChessAdvisor.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessAdvisor.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessCannon.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessCannon.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessChariot.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessChariot.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessElephant.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessElephant.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessGeneral.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessGeneral.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessHorse.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessHorse.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessPawn.cs
Normal file
0
Scripts/Entities/ChessPieces/ChessPawn.cs
Normal file
20
Scripts/Entities/ChessPieces/ChessTypes.cs
Normal file
20
Scripts/Entities/ChessPieces/ChessTypes.cs
Normal file
@ -0,0 +1,20 @@
|
||||
// 帅/将 (General) - 代表双方的最高统帅。
|
||||
// 子类名:ChessGeneral
|
||||
|
||||
// 仕/士 (Advisor) - 保护帅/将的近身侍卫。
|
||||
// 子类名:ChessAdvisor
|
||||
|
||||
// 相/象 (Elephant) - 行动受限,走田字格,不能过河。
|
||||
// 子类名:ChessElephant
|
||||
|
||||
// 車/车 (Chariot) - 横竖移动,威力巨大。
|
||||
// 子类名:ChessChariot
|
||||
|
||||
// 馬/马 (Horse) - 走日字形,跳跃式移动。
|
||||
// 子类名:ChessHorse
|
||||
|
||||
// 砲/炮 (Cannon) - 需要隔子才能吃子,直线移动。
|
||||
// 子类名:ChessCannon
|
||||
|
||||
// 兵/卒 (Pawn) - 最基础的棋子,过河后可横移。
|
||||
// 子类名:ChessPawn
|
Reference in New Issue
Block a user