detecting players template

This commit is contained in:
Baipyrus 2023-03-29 08:38:53 +02:00
parent 0575054fba
commit cd6527afb3

View File

@ -242,7 +242,7 @@ namespace RPI_Matrix {
// All inputs on a Gamepad are accessible from within here.
// Interpreting this data may only work in this program using a SNES Controller.
while (true) {
string dir = "/dev/input";
string dir = "/dev/input/";
IEnumerable<string> files = from retrieved in Directory.EnumerateFiles(dir)
where retrieved.Contains("js")
select retrieved;
@ -251,7 +251,7 @@ namespace RPI_Matrix {
gamepadListeners.Add(f);
new Thread(() => {
try {
WatchInputFile(f);
WatchInputFile(dir, f);
} catch (Exception) {
gamepadListeners.Remove(f);
}
@ -261,7 +261,7 @@ namespace RPI_Matrix {
}
}
private static void WatchInputFile(string file) {
private static void WatchInputFile(string dir, string file) {
FileInfo targetFile = new FileInfo(file);
using FileStream fs = targetFile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Initialize Buffer (byte Array) to put read bytes into
@ -275,8 +275,10 @@ namespace RPI_Matrix {
// And do it again in SnakeGame while it's playing.
else if (isSnake && !SnakeGame.SnakeGame.gameOver)
SnakeGame.SnakeGame.keypressFrame = SnakeGame.SnakeGame.frameCount;
// Convert Buffer to Hexadecimal, each Byte separated by a '-'
Action method = BitConverter.ToString(buffer, 4, 4) switch {
Delegate method = BitConverter.ToString(buffer, 4, 4) switch {
"01-00-01-00" => XButtonPressed,
"01-00-01-01" => AButtonPressed,
"01-00-01-02" => BButtonPressed,
@ -287,12 +289,14 @@ namespace RPI_Matrix {
"FF-7F-02-01" => DownArrowPressed,
"01-80-02-00" => LeftArrowPressed,
"FF-7F-02-00" => RightArrowPressed,
_ => () => {}
}; method.Invoke();
_ => (string player) => {}
};
// Execute function mapped by buffer/button press as current player
method.DynamicInvoke(file.Replace(dir, ""));
}
}
private static void RightArrowPressed() {
private static void RightArrowPressed(string player) {
// Code for Arrow-Right Press
// If in SnakeGame, Steer the Snake to the right
if (isSnake) SnakeGame.SnakeGame.SteerRight(SnakeGame.SnakeGame.self);
@ -300,7 +304,7 @@ namespace RPI_Matrix {
else if (isTetris) Tetris.Tetris.moveRightSide();
}
private static void LeftArrowPressed() {
private static void LeftArrowPressed(string player) {
// Code for Arrow-Left Press
// If in SnakeGame, Steer the Snake to the left
if (isSnake) SnakeGame.SnakeGame.SteerLeft(SnakeGame.SnakeGame.self);
@ -308,7 +312,7 @@ namespace RPI_Matrix {
else if (isTetris) Tetris.Tetris.moveLeftSide();
}
private static void DownArrowPressed() {
private static void DownArrowPressed(string player) {
// Code for Arrow-Down Press
// If in the selection screen, decrement index
if (!isInGame && selectionIndex < 4)
@ -340,7 +344,7 @@ namespace RPI_Matrix {
debugIndex++;
}
private static void UpArrowButtonPressed() {
private static void UpArrowButtonPressed(string player) {
// Code for Arrow-Up Press
// If in the selection screen, decrement index
if (!isInGame && selectionIndex > 0)
@ -367,7 +371,7 @@ namespace RPI_Matrix {
debugIndex--;
}
private static void StartButtonPressed() {
private static void StartButtonPressed(string player) {
// Code for Start-Button Press
// If in SnakeGame, end the Game and return to menu
if (isSnake)
@ -387,19 +391,19 @@ namespace RPI_Matrix {
Tetris.Tetris.EndGame();
}
private static void RButtonPressed() {
private static void RButtonPressed(string player) {
// Code for R-Button Press
// If in Tetris, rotate the current Block Clockwise
if (isTetris) Tetris.Tetris.rotateClockwise();
}
private static void LButtonPressed() {
private static void LButtonPressed(string player) {
// Code for L-Button Press
// If in Tetris, rotate the current Block counter-Clockwise
if (isTetris) Tetris.Tetris.rotateCounterClockwise();
}
private static void BButtonPressed() {
private static void BButtonPressed(string player) {
// Code for B-Button Press
if (isFlappy) {
// Jump
@ -419,7 +423,7 @@ namespace RPI_Matrix {
}
}
private static void AButtonPressed() {
private static void AButtonPressed(string player) {
// Code for A-Button Press
// If not already in a Game, now ready up to select one.
if (!isInGame)
@ -434,7 +438,7 @@ namespace RPI_Matrix {
else debugSelect = true;
}
private static void XButtonPressed() {
private static void XButtonPressed(string player) {
// Code for X-Button Press
if (isTetris) Tetris.Tetris.swapMemory();
}