bugfix 重新提交暂存区
This commit is contained in:
146
Scripts/Controllers/ChessGame.cs
Normal file
146
Scripts/Controllers/ChessGame.cs
Normal file
@ -0,0 +1,146 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
public partial class ChessGame : Node2D
|
||||
{
|
||||
ChessBoard board;
|
||||
Global global;
|
||||
ConfirmationDialog dialog;
|
||||
private bool isSession = false;
|
||||
private Vector2 from;
|
||||
private Vector2 to;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
// Init.Call();
|
||||
global = GetNode<Global>("/root/Global");
|
||||
board = GetNode<ChessBoard>("Chessboard");
|
||||
dialog = new ConfirmationDialog {
|
||||
DialogAutowrap = true,
|
||||
MinSize = new Vector2I(400, 200),
|
||||
Position = new Vector2I(200, 400),
|
||||
};
|
||||
AddChild(dialog);
|
||||
// GetNode<Button>("Undo").Connect("pressed", Callable.From(board.Undo));
|
||||
// GetNode<Button>("ReInit").Connect("pressed", Callable.From(board.ReInit));
|
||||
// GetNode<Button>("Home").Connect("pressed", Callable.From(this.GoHome));
|
||||
|
||||
if (!global.RPClient.GetIsConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
isSession = true;
|
||||
GD.Print("ws is connected");
|
||||
global.RPClient.OnPRCSessionExit += (cmd, code) => {
|
||||
GoHome();
|
||||
};
|
||||
// board.Set("Hello", Callable.From(() => {GD.PrintErr("hello");}));
|
||||
board.board.OnMove += (sender, args) => {
|
||||
Vector2 newPos = args.To;
|
||||
Vector2 fromPos = args.From;
|
||||
if (from.X == fromPos.X && from.Y == fromPos.Y && to.X == newPos.X && to.Y == newPos.Y) {
|
||||
return;
|
||||
}
|
||||
from = fromPos;
|
||||
to = newPos;
|
||||
var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
||||
{"type", "move"},
|
||||
{"from", fromPos},
|
||||
{"to", newPos},
|
||||
{"fromX", fromPos.X},
|
||||
{"fromY", fromPos.Y},
|
||||
{"toX", newPos.X},
|
||||
{"toY", newPos.Y},
|
||||
});
|
||||
GD.Print($"chessMoveFunc Callback {fromPos} -> {newPos} {res}");
|
||||
};
|
||||
global.RPClient.OnPRCSessionRecv += (msg) => {
|
||||
SessionMsgHandle(msg["msg"].AsGodotDictionary());
|
||||
};
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta) {
|
||||
}
|
||||
|
||||
private void SessionMsgHandle(Dictionary msg) {
|
||||
GD.PrintErr($"session msg: {msg}");
|
||||
switch (msg["type"].AsString()) {
|
||||
case "over":
|
||||
if (global.RPClient.GetUserId() == msg["id"].AsString()) {
|
||||
break;
|
||||
}
|
||||
dialog.Title = "Opponent Finished";
|
||||
dialog.DialogText = "Turn On You\n";
|
||||
dialog.Visible = true;
|
||||
break;
|
||||
case "move":
|
||||
Vector2 _to = new(GD.StrToVar(msg["toX"].ToString()).AsInt32(),
|
||||
GD.StrToVar(msg["toY"].ToString()).AsInt32());
|
||||
|
||||
Vector2 _from = new(GD.StrToVar(msg["fromX"].ToString()).AsInt32(),
|
||||
GD.StrToVar(msg["fromY"].ToString()).AsInt32());
|
||||
if (_to.X == to.X && _to.Y == to.Y && _from.X == from.X && _from.Y == from.Y) {
|
||||
return;
|
||||
}
|
||||
to = _to;
|
||||
from = _from;
|
||||
board.playerSelf.MoveAndRecord(to, from);
|
||||
break;
|
||||
case "undo":
|
||||
_Undo();
|
||||
break;
|
||||
case "reInit":
|
||||
_ReInit();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void _Undo() {
|
||||
board.playerSelf.Undo();
|
||||
}
|
||||
|
||||
private void _ReInit() {
|
||||
board.playerSelf.ReInit();
|
||||
board.InitChessBoard();
|
||||
}
|
||||
|
||||
private void BtnOver() {
|
||||
GD.PrintErr($"BtnOver {isSession}");
|
||||
if (isSession == false) {
|
||||
return;
|
||||
}
|
||||
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
||||
{"type", "over"},
|
||||
{"id", global.RPClient.GetUserId()},
|
||||
});
|
||||
}
|
||||
|
||||
public void GoHome() {
|
||||
if (global.RPClient.IsOnline()) {
|
||||
global.RPClient.ExitServer();
|
||||
}
|
||||
global.GotoScene("res://Main.tscn");
|
||||
}
|
||||
|
||||
public void Undo() {
|
||||
GD.PrintErr($"Undo {isSession}");
|
||||
if (isSession == false) {
|
||||
_Undo();
|
||||
}
|
||||
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
||||
{"type", "undo"},
|
||||
});
|
||||
}
|
||||
|
||||
public void ReInit() {
|
||||
GD.PrintErr($"ReInit {isSession}");
|
||||
if (isSession == false) {
|
||||
_ReInit();
|
||||
}
|
||||
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
||||
{"type", "reInit"},
|
||||
});
|
||||
}
|
||||
}
|
136
Scripts/Controllers/Menu.cs
Normal file
136
Scripts/Controllers/Menu.cs
Normal file
@ -0,0 +1,136 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
public partial class Menu : Control
|
||||
{
|
||||
Global global = null;
|
||||
ItemList lists = null;
|
||||
ConfirmationDialog dialog = null;
|
||||
LineEdit nameLineEdit = null;
|
||||
LineEdit urlLineEdit = null;
|
||||
ColorRect colorRect = null;
|
||||
Timer timer = null;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
global = GetNode<Global>("/root/Global");
|
||||
nameLineEdit = GetNode<LineEdit>("Name/LineEdit");
|
||||
urlLineEdit = GetNode<LineEdit>("URL");
|
||||
urlLineEdit.Text = global.URL;
|
||||
urlLineEdit.Set("text_submitted", Callable.From(()=>{
|
||||
global.URL = urlLineEdit.Text;
|
||||
}));
|
||||
|
||||
lists = GetNode<ItemList>("Server/ItemList");
|
||||
dialog = GetNode<ConfirmationDialog>("Dialogs/ConfirmationDialog");
|
||||
colorRect = GetNode<ColorRect>("Server/ColorRect");
|
||||
dialog.DialogAutowrap = true;
|
||||
dialog.MinSize = new Vector2I(400, 200);
|
||||
dialog.Canceled += () => {
|
||||
if (dialog.Title == "Session Created")
|
||||
global.RPClient.SessionAckCreate(dialog.GetMeta("sessionId").ToString(), false);
|
||||
};
|
||||
dialog.Confirmed += () => {
|
||||
// GD.PrintErr("confirm", dialog.GetLabel().Text);
|
||||
// goToSignle();
|
||||
if (dialog.Title == "Session Created")
|
||||
global.RPClient.SessionAckCreate(dialog.GetMeta("sessionId").ToString(), true);
|
||||
};
|
||||
|
||||
global.RPClient.RegSessionAckCreateCallback((
|
||||
sessionId,
|
||||
res,
|
||||
reqUserId,
|
||||
reqUserName) => {
|
||||
if (reqUserId != null) {
|
||||
dialog.Title = "Session Created";
|
||||
dialog.SetMeta("reqUserName", reqUserName);
|
||||
dialog.SetMeta("reqUserId", reqUserId);
|
||||
dialog.SetMeta("sessionId", sessionId);
|
||||
// dialog.GetLabel==>Text = $"{sessdata["reqUserName"]}";
|
||||
dialog.DialogText = $"username: {reqUserName}\n" +
|
||||
$"reqUserId: {reqUserId}\n";
|
||||
dialog.Visible = true;
|
||||
} else {
|
||||
if (res) {
|
||||
global.sessionId = sessionId;
|
||||
global.GotoScene("res://Scenes/ChessGame.tscn");
|
||||
} else {
|
||||
dialog.Title = "Failed";
|
||||
dialog.DialogText = $"session create failed";
|
||||
dialog.Visible = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
timer = new Timer();
|
||||
AddChild(timer);
|
||||
timer.Connect("timeout", Callable.From(() => {
|
||||
if (global.RPClient.GetIsConnected()) {
|
||||
colorRect.Color = Colors.Green;
|
||||
} else {
|
||||
colorRect.Color = Colors.Red;
|
||||
}
|
||||
}));
|
||||
timer.Start(1);
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta) {
|
||||
}
|
||||
|
||||
private void OnItemSelected(int index) {
|
||||
Dictionary item = lists.GetItemMetadata(index).AsGodotDictionary();
|
||||
GD.Print($"Item {index} selected, {item}");
|
||||
string[] strings = { item["id"].ToString() };
|
||||
global.RPClient.SessionCreate(strings);
|
||||
}
|
||||
|
||||
private void FlushData()
|
||||
{
|
||||
global.RPClient.RegionInspect("server", (data) => {
|
||||
GD.Print(data);
|
||||
lists.Clear();
|
||||
foreach (Dictionary<string, string> user in data) {
|
||||
string userId = user["id"].ToString();
|
||||
string userName = user["name"].ToString();
|
||||
if (userId == global.RPClient.GetUserId()) {
|
||||
lists.SetItemDisabled(
|
||||
lists.AddItem($"Name: {userName}"),
|
||||
true);
|
||||
continue;
|
||||
}
|
||||
var idx = lists.AddItem($"Name: {userName}");
|
||||
lists.SetItemMetadata(idx, user);
|
||||
lists.SetItemTooltip(idx, $"User ID: {userId}");
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
GD.Print("Connect");
|
||||
global.RPClient.ExitServer();
|
||||
global.RPClient.ConnectToUrlEx(urlLineEdit.Text);
|
||||
global.SetProcess(true);
|
||||
}
|
||||
|
||||
public void EnterName() {
|
||||
var newLine = nameLineEdit.Text;
|
||||
global.RPClient.UserRename(newLine);
|
||||
nameLineEdit.Text = newLine;
|
||||
}
|
||||
|
||||
private void goToHome() {
|
||||
global.RPClient.ExitServer();
|
||||
global.GotoScene("res://Main.tscn");
|
||||
}
|
||||
|
||||
// private void OnItemListItemClicked(int index, Vector2 atPosition, int mouse_button_index)
|
||||
// {
|
||||
// GD.Print($"Item {index} clicked at {atPosition} with mouse button index {mouse_button_index}");
|
||||
// }
|
||||
}
|
127
Scripts/Controllers/Player.cs
Normal file
127
Scripts/Controllers/Player.cs
Normal file
@ -0,0 +1,127 @@
|
||||
using Godot;
|
||||
|
||||
public class Player
|
||||
{
|
||||
private VirtualBoard board;
|
||||
private SelectedPiece selectedNode;
|
||||
private MoveRecords<VirtualPiece> moveRecords;
|
||||
|
||||
public enum PlayerType {
|
||||
Human,
|
||||
AI
|
||||
}
|
||||
|
||||
public Player(VirtualBoard board, PlayerType type = PlayerType.Human)
|
||||
{
|
||||
this.board = board;
|
||||
this.selectedNode = new SelectedPiece(board);
|
||||
this.moveRecords = new MoveRecords<VirtualPiece>(onUndoRecordCallback: (newNode, oldNode, newPos, oldPos) => {
|
||||
GD.Print("Undo: ", newNode, "->", oldNode, ":", newPos, "->", oldPos);
|
||||
VirtualPiece newPiece = newNode;
|
||||
VirtualPiece oldPiece = oldNode;
|
||||
this.board.MovePiece(newPos, oldPos);
|
||||
if (newPiece != null) {
|
||||
this.board.InsertPiece(newPiece, newPos);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void HandleBoardPosClick(Vector2 clickPos) {
|
||||
if (board.ArrPosOutOfRange(clickPos)) return;
|
||||
GD.Print($"VirtualBoard {clickPos} clicked");
|
||||
VirtualPiece clickChess = board.GetPiece(clickPos);
|
||||
|
||||
if (!selectedNode.HasSelected()) {
|
||||
// Select piece
|
||||
if (clickChess == null) {
|
||||
// selectedNode.Clear();
|
||||
return;
|
||||
}
|
||||
selectedNode.SetPos(clickPos);
|
||||
} else if (clickChess == selectedNode.GetPiece()) {
|
||||
// Unselect piece
|
||||
selectedNode.Clear();
|
||||
} else {
|
||||
// Move piece
|
||||
GD.Print("default MoveFunc Move: ", selectedNode.GetPos(), "->", clickPos);
|
||||
MoveAndRecord(clickPos, selectedNode.GetPos());
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveAndRecord(Vector2 toPos, Vector2 fromPos) {
|
||||
GD.Print($"{fromPos} move to {toPos}");
|
||||
VirtualPiece toChess = board.GetPiece(toPos);
|
||||
VirtualPiece fromChess = board.GetPiece(fromPos);
|
||||
fromChess?.Selected(false);
|
||||
|
||||
VirtualPiece NowNode;
|
||||
if (toChess != null) {
|
||||
NowNode = toChess;
|
||||
board.RemovePiece(toPos);
|
||||
} else {
|
||||
NowNode = toChess;
|
||||
}
|
||||
moveRecords.AddRecord(NowNode, fromChess, toPos, fromPos);
|
||||
board.MovePiece(fromPos, toPos);
|
||||
|
||||
selectedNode.Clear();
|
||||
}
|
||||
|
||||
public void Undo() {
|
||||
// ChessPiece selected = selectedNode.GetPiece();
|
||||
// selected?.DeSelected();
|
||||
selectedNode.Clear();
|
||||
moveRecords.Undo();
|
||||
}
|
||||
|
||||
public void ReInit() {
|
||||
moveRecords.Clear();
|
||||
board.Clear();
|
||||
selectedNode.Clear();
|
||||
// board.InitChessBoard();
|
||||
}
|
||||
|
||||
private class SelectedPiece
|
||||
{
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
private Vector2 selectedNodePos = Vector2.Inf;
|
||||
private VirtualPiece piece;
|
||||
private VirtualBoard board;
|
||||
|
||||
public SelectedPiece(VirtualBoard board)
|
||||
{
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
if (selectedNodePos != Vector2.Inf) {
|
||||
selectedNodePos = Vector2.Inf;
|
||||
piece.Selected(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetPos(Vector2 pos)
|
||||
{
|
||||
// piece = board.GetNodeFromBoard(pos) as VirtualPiece;
|
||||
selectedNodePos = pos;
|
||||
piece = board.GetPiece(selectedNodePos);
|
||||
piece.Selected(true);
|
||||
}
|
||||
|
||||
public VirtualPiece GetPiece()
|
||||
{
|
||||
return piece;
|
||||
}
|
||||
|
||||
public Vector2 GetPos()
|
||||
{
|
||||
return selectedNodePos;
|
||||
}
|
||||
|
||||
public bool HasSelected()
|
||||
{
|
||||
return selectedNodePos != Vector2.Inf;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user