Skip to content

Commit

Permalink
Added BoardAnimation.cs, this allows multiple animations at one time,…
Browse files Browse the repository at this point in the history
… making things like castling and double undos look nicer
  • Loading branch information
Sargates authored and Sargates committed Aug 9, 2023
1 parent 99261d0 commit d0d0e4f
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ bld/
[Oo]bj/
[Ll]og/
[Ll]ogs/
devnotes.md
devnotes.md
settings.txt
4 changes: 0 additions & 4 deletions src/Application/Core/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public Controller() {
model = new Model();
view = new View(screenSize, model, cam);


}

public void MainLoop() {
Expand Down Expand Up @@ -72,8 +71,5 @@ public void MainLoop() {

}

public void HandleInput() {

}
}
}
8 changes: 6 additions & 2 deletions src/Application/Core/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ public class Model {
public bool enforceColorToMove = false;

public Model() {
board = new Board();
// board = new Board("rnbqkbnr/ppp1pppp/8/3P4/8/8/PPPP1PPP/RNBQKBNR b KQkq e6 0 5");
board = new Board("r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1");

Player whitePlayer = new Player('w');
Player blackPlayer = new Player('b');
}


Expand All @@ -23,7 +25,9 @@ public void StartNewGame(string fenString) {
//*
//*


board = new Board(fenString);

}


Expand Down
50 changes: 34 additions & 16 deletions src/Application/Core/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ public class View {
public static Vector2 screenSize;
public Model model;
public Camera2D camera;
int squareClicked;

// Square selected on each interaction, -1 for invalid square
// { leftDown, leftUp, rightDown, rightUp }
public int[] mouseClickInfo = {-1, -1, -1, -1};
public static bool leftReleased, leftPressed, rightPressed, rightReleased;
// in the format of: leftReleased, leftPressed, rightPressed, rightReleased
public static int mouseButtonsClicked; // 0b1111
public static int pressedKey=0;

public List<(int tail, int head)> drawnArrows = new List<(int tail, int head)>();

public List<IInteractable> pipeline;

public static bool IsLeftPressed => (View.mouseButtonsClicked & 8) == 8;
public static bool IsLeftReleased => (View.mouseButtonsClicked & 4) == 4;
public static bool IsRightPressed => (View.mouseButtonsClicked & 2) == 2;
public static bool IsRightReleased => (View.mouseButtonsClicked & 1) == 1;




// Animation?
Expand All @@ -33,7 +41,9 @@ public View(Vector2 screenSize, Model model, Camera2D cam) {

Button button = new Button(new Rectangle(40, 600, 200, 50), "Button");
button.OnLeftPressed = () => {
Piece[] oldBoard = model.board.board.ToArray();
model.StartNewGame(Fen.startpos);
ui.activeAnimation = new BoardAnimation(oldBoard, model.board.board, .12f);
};
AddToPipeline(button);
}
Expand All @@ -46,10 +56,11 @@ public void AddToPipeline(IInteractable interactable) {


public void Update(float dt) {
View.leftPressed = Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT);
View.leftReleased = Raylib.IsMouseButtonReleased(MouseButton.MOUSE_BUTTON_LEFT);
View.rightPressed = Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_RIGHT);
View.rightReleased = Raylib.IsMouseButtonReleased(MouseButton.MOUSE_BUTTON_RIGHT);
mouseButtonsClicked = 0;
mouseButtonsClicked += Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT) ? 8 : 0;
mouseButtonsClicked += Raylib.IsMouseButtonReleased(MouseButton.MOUSE_BUTTON_LEFT) ? 4 : 0;
mouseButtonsClicked += Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_RIGHT) ? 2 : 0;
mouseButtonsClicked += Raylib.IsMouseButtonReleased(MouseButton.MOUSE_BUTTON_RIGHT) ? 1 : 0;
pressedKey = Raylib.GetKeyPressed();

Raylib.BeginMode2D(camera);
Expand Down Expand Up @@ -78,7 +89,7 @@ public void Update(float dt) {
if (pressedKey != 0) {
HandleKeyboardInput();
}
if (ui.activeAnimation is null && View.leftPressed || View.leftReleased || View.rightPressed || View.rightReleased) {
if (ui.activeAnimation is null && mouseButtonsClicked > 0) {
HandleMouseInput();
}
}
Expand Down Expand Up @@ -115,9 +126,12 @@ public void HandleKeyboardInput() {
}

case (int) KeyboardKey.KEY_P :{
foreach (Fen fenString in model.board.stateHistory) {
ConsoleHelper.WriteLine($"{fenString}", ConsoleColor.Cyan);
}
Console.WriteLine(Convert.ToString(model.board.currentFen.castlePrivsBin, 2));
// foreach (Fen fenString in model.board.stateHistory) {
// ConsoleHelper.WriteLine($"{fenString}", ConsoleColor.Cyan);
// }
// Console.WriteLine(Raylib.GetMousePosition());
// Console.WriteLine((new Vector2(ui.selectedIndex & 0b111, 7-(ui.selectedIndex >> 3)) - new Vector2(4, 4)) * BoardUI.squareSize);
break;
}
default: {
Expand All @@ -129,7 +143,7 @@ public void HandleKeyboardInput() {
public void HandleMouseInput() {
// TODO Test how ineffective it would be to constantly update mousePos and check if mouse is on a square
Piece clickedPiece = Piece.None;
squareClicked = -1;
int squareClicked = -1;
Move validMove = new Move(0);

Vector2 pos = Raylib.GetMousePosition() - screenSize/2;
Expand All @@ -155,13 +169,14 @@ public void HandleMouseInput() {
}
}

if (View.leftPressed) {
if (IsLeftPressed) {
mouseClickInfo[0] = squareClicked;
ui.highlightedSquares = new bool[64];
if (! validMove.IsNull ) { // Case 3
ui.DeselectActiveSquare();
Piece[] oldState = model.board.board.ToArray();
model.board.MakeMove(validMove);
ui.activeAnimation = new Animation(validMove.StartSquare, validMove.TargetSquare, model.board.GetSquare(validMove.TargetSquare), .12f);
ui.activeAnimation = new BoardAnimation(oldState, model.board.board, .12f);
//* ANIMATION HERE
} else
if (ui.selectedIndex != -1 && squareClicked == ui.selectedIndex) { // Case 5
Expand All @@ -186,7 +201,7 @@ public void HandleMouseInput() {
}
}

if (View.leftReleased) {
if (IsLeftReleased) {
mouseClickInfo[1] = squareClicked;
ui.isDraggingPiece = false;

Expand All @@ -198,16 +213,19 @@ public void HandleMouseInput() {
mouseClickInfo[0] = -1; mouseClickInfo[1] = -1;
}

if (View.rightPressed) {
if (IsRightPressed) {
mouseClickInfo[2] = squareClicked;
ui.DeselectActiveSquare();
ui.isDraggingPiece = false;
}

if (View.rightReleased) {
if (IsRightReleased) {
mouseClickInfo[3] = squareClicked;
if (ui.selectedIndex == -1 && mouseClickInfo[0] == -1) {
ui.highlightedSquares[squareClicked] = ! ui.highlightedSquares[squareClicked];
} else
if (true) {
drawnArrows.Add((mouseClickInfo[2], mouseClickInfo[3]));
}
// Console.WriteLine(string.Join(", ", mouseClickInfo));
mouseClickInfo[2] = -1; mouseClickInfo[3] = -1;
Expand Down
143 changes: 143 additions & 0 deletions src/Application/Gameplay/Input.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// using Raylib_cs;
// using System.Numerics;
// using ChessBot.Engine;
// using ChessBot.Helpers;

// namespace ChessBot.Application;
// public static class Input {

// // Square selected on each interaction, -1 for invalid square
// // { leftDown, leftUp, rightDown, rightUp }
// public static int[] mouseClickInfo = {-1, -1, -1, -1};
// public static bool leftReleased, leftPressed, rightPressed, rightReleased;
// public static int pressedKey=0;


// public static void HandleKeyboardInput(BoardUI ui, Model model) {
// if (! (Input.pressedKey != 0)) { return; }

// switch (Input.pressedKey) {
// case (int) KeyboardKey.KEY_Z :{
// ui.DeselectActiveSquare();
// model.board.SetPrevState();
// break;
// }
// case (int) KeyboardKey.KEY_X :{
// ui.DeselectActiveSquare();
// model.board.SetNextState();
// break;
// }

// case (int) KeyboardKey.KEY_P :{
// // foreach (Fen fenString in model.board.stateHistory) {
// // ConsoleHelper.WriteLine($"{fenString}", ConsoleColor.Cyan);
// // }
// // Console.WriteLine(Raylib.GetMousePosition());
// // Console.WriteLine((new Vector2(ui.selectedIndex & 0b111, 7-(ui.selectedIndex >> 3)) - new Vector2(4, 4)) * BoardUI.squareSize);
// break;
// }
// default: {
// break;
// }
// }
// }

// public static void HandleMouseInput(BoardUI ui, Model model) {
// if (! (ui.activeAnimation is null && Input.leftPressed || Input.leftReleased || Input.rightPressed || Input.rightReleased)) { return; }

// // TODO Test how ineffective it would be to constantly update mousePos and check if mouse is on a square
// Piece clickedPiece = Piece.None;
// int squareClicked = -1;
// Move validMove = new Move(0);

// Vector2 pos = Raylib.GetMousePosition() - screenSize/2;

// Vector2 boardPos = (pos/BoardUI.squareSize)+new Vector2(4);

// if ((0 <= boardPos.X && boardPos.X < 8 && 0 <= boardPos.Y && boardPos.Y < 8) ) {
// squareClicked = 8*((int)(8-boardPos.Y))+(int)boardPos.X;
// clickedPiece = model.board.GetSquare(squareClicked);
// } // If the interaction (click/release) is in bounds, set square clicked and clicked piece, otherwise they will be -1 and {Piece.None}

// if (squareClicked == -1) { // Case 1
// ui.DeselectActiveSquare();
// return;
// } // Passes guard clause if the click was in bounds

// if (ui.selectedIndex != -1) {
// foreach (Move move in ui.movesForSelected) {
// if (move == new Move(ui.selectedIndex, squareClicked)) {
// validMove = move;
// break;
// }
// }
// }

// if (Input.leftPressed) {
// mouseClickInfo[0] = squareClicked;
// ui.highlightedSquares = new bool[64];
// if (! validMove.IsNull ) { // Case 3
// ui.DeselectActiveSquare();
// model.board.MakeMove(validMove);
// ui.activeAnimation = new Animation(validMove.StartSquare, validMove.TargetSquare, model.board.GetSquare(validMove.TargetSquare), .12f);
// //* ANIMATION HERE
// } else
// if (ui.selectedIndex != -1 && squareClicked == ui.selectedIndex) { // Case 5
// ui.isDraggingPiece = true;
// } else
// if (ui.selectedIndex == -1 && clickedPiece != Piece.None) { // Case 2
// if (model.enforceColorToMove && clickedPiece.Color == model.board.activeColor) {
// ui.selectedIndex = squareClicked;
// ui.movesForSelected = MoveGenerator.GetMoves(model.board, squareClicked);
// ui.isDraggingPiece = true;
// }
// } else
// if (validMove.IsNull && clickedPiece.Type != Piece.None) { // Case 6
// if (model.enforceColorToMove && clickedPiece.Color == model.board.activeColor) {
// ui.selectedIndex = squareClicked;
// ui.movesForSelected = MoveGenerator.GetMoves(model.board, squareClicked);
// ui.isDraggingPiece = true;
// }
// } else
// if (validMove.IsNull) { // Case 4
// ui.DeselectActiveSquare();
// }
// }

// if (Input.leftReleased) {
// mouseClickInfo[1] = squareClicked;
// ui.isDraggingPiece = false;

// if (! validMove.IsNull) {
// ui.DeselectActiveSquare();
// model.board.MakeMove(validMove);
// }
// // Console.WriteLine(string.Join(", ", mouseClickInfo));
// mouseClickInfo[0] = -1; mouseClickInfo[1] = -1;
// }

// if (Input.rightPressed) {
// mouseClickInfo[2] = squareClicked;
// ui.DeselectActiveSquare();
// ui.isDraggingPiece = false;
// }

// if (Input.rightReleased) {
// mouseClickInfo[3] = squareClicked;
// if (ui.selectedIndex == -1 && mouseClickInfo[0] == -1) {
// ui.highlightedSquares[squareClicked] = ! ui.highlightedSquares[squareClicked];
// }
// // Console.WriteLine(string.Join(", ", mouseClickInfo));
// mouseClickInfo[2] = -1; mouseClickInfo[3] = -1;
// }

// //* Case 1: No square is selected, and square clicked is out of bounds => call DeselectActiveSquare ✓
// //* Case 2: No square is selected and piece is clicked => Set selectedIndex to square clicked ✘
// //* Case 3: Square is selected and square clicked is a valid move => call model.board.MakeMove ✘
// //* Case 4: Square is selected and square clicked is not a valid move => Deselect piece and fallthrough to case 7 ✘
// //* Case 5: Square is selected and square clicked == selected index => set isDragging to true ✘
// //* Case 6: Square is selected and clicked piece is the same color => Subset of Case 7 ✓
// //* Case 7: Square is selected and clicked piece is not in the valid moves => Superset of case 4 ✘
// //* Case 7.1: If clicked square is a piece, select that square
// }
// }
4 changes: 3 additions & 1 deletion src/Application/Gameplay/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public Player(char color) {
this.color = color;
}

public void HandleInput() {}
public void Think() {

}
}
}
Loading

0 comments on commit d0d0e4f

Please sign in to comment.