Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Willnilges/update #44

Merged
merged 5 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions onboard/DevcadeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class DevcadeClient
{
private readonly string _apiDomain;

// Basically a semaphore for communicating between the main thread (doing the menu animations)
// and the thread that loads the game.
public bool DownloadFailed = false;

public DevcadeClient()
Expand Down
53 changes: 51 additions & 2 deletions onboard/Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ private enum MenuState
// To indicate that there was a problem running the game
private bool _errorLoading = false;

// If we can't fetch the game list (like if the API is down)
private bool _cantFetch = false;

public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Expand Down Expand Up @@ -92,8 +95,16 @@ protected override void LoadContent()
loadingSpin = Content.Load<Texture2D>("loadingSheet");

// TODO: use this.Content to load your game content here
_mainMenu.gameTitles = _client.GetGames();
_mainMenu.setCards(_client, GraphicsDevice);
try
{
_mainMenu.gameTitles = _client.GetGames();
_mainMenu.setCards(_client, GraphicsDevice);
} catch (System.AggregateException e)
{
Console.WriteLine($"Failed to fetch games: {e}");
state = MenuState.Loading;
_cantFetch = true;
}
}

protected override void Update(GameTime gameTime)
Expand Down Expand Up @@ -126,6 +137,27 @@ protected override void Update(GameTime gameTime)
break;

case MenuState.Loading:

if (_cantFetch)
{
if (myState.IsKeyDown(Keys.Space) || (Input.GetButton(1, Input.ArcadeButtons.Menu) && Input.GetButton(2, Input.ArcadeButtons.Menu)))
{
try {
_mainMenu.clearGames();
_mainMenu.gameTitles = _client.GetGames();
_mainMenu.setCards(_client, GraphicsDevice);
_cantFetch = false;
state = MenuState.Input;
} catch (System.AggregateException e)
{
Console.WriteLine($"Failed to fetch games: {e}");
state = MenuState.Loading;
_cantFetch = true;
}

}
}

_errorLoading = false; // Clear error loading if we successfully load.
// Check for process that matches last launched game and display loading screen if it's running
// This can be done easier by keeping a reference to the process spawned and .HasExited property...
Expand Down Expand Up @@ -154,6 +186,21 @@ protected override void Update(GameTime gameTime)
_mainMenu.descFadeOut(gameTime);
_mainMenu.cardFadeIn(gameTime);

if (myState.IsKeyDown(Keys.Space) || (Input.GetButton(1, Input.ArcadeButtons.Menu) && Input.GetButton(2, Input.ArcadeButtons.Menu)))
{
_mainMenu.clearGames();
try
{
_mainMenu.gameTitles = _client.GetGames();
_mainMenu.setCards(_client, GraphicsDevice);
} catch (System.AggregateException e)
{
Console.WriteLine($"Failed to fetch games: {e}");
state = MenuState.Loading;
_cantFetch = true;
}
}

if (((myState.IsKeyDown(Keys.Down)) || // Keyboard down
Input.GetButton(1, Input.ArcadeButtons.StickDown) || // or joystick down
Input.GetButton(2, Input.ArcadeButtons.StickDown))) // of either player
Expand Down Expand Up @@ -243,6 +290,8 @@ protected override void Draw(GameTime gameTime)
case MenuState.Loading:
_mainMenu.drawLoading(_spriteBatch, loadingSpin, fadeColor);
_mainMenu.drawTitle(_spriteBatch, titleTextureWhite, fadeColor);
if (_cantFetch)
_mainMenu.drawError(_spriteBatch, _devcadeMenuBig);
break;
}

Expand Down
45 changes: 43 additions & 2 deletions onboard/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ public void updateDims(GraphicsDeviceManager _graphics)
// Empties the gameTitles and cards lists. Called when the reload buttons are pressed
public void clearGames()
{
try {
gameTitles.Clear();
cards.Clear();
itemSelected = 0;
} catch (System.NullReferenceException e) {
Console.WriteLine($"No game titles or cards yet. {e}");
}
}

public void setCards(DevcadeClient _client, GraphicsDevice graphics)
Expand Down Expand Up @@ -176,8 +180,8 @@ public void drawTitle(SpriteBatch _spriteBatch, Texture2D titleTexture, float co

public void drawInstructions(SpriteBatch _spriteBatch, SpriteFont font)
{
List<string> instructions = wrapText("Press the Red button to play!", 25);
float instructSize = font.MeasureString("Press the Red button to play!").Y;
List<string> instructions = wrapText("Press the Red button to play! Press both Black Buttons to refresh", 25);
float instructSize = font.MeasureString("Press the Red button to play! Press both Black Buttons to refresh").Y;
int lineNum = 0;

foreach (string line in instructions)
Expand All @@ -192,6 +196,27 @@ public void drawInstructions(SpriteBatch _spriteBatch, SpriteFont font)
}
}


public void drawError(SpriteBatch _spriteBatch, SpriteFont font)
{
string error = "Error: Could not get game list. Is API Down? Press both black buttons to reload.";
List<string> instructions = wrapText(error, 25);
float instructSize = font.MeasureString(error).Y;
int lineNum = 0;

foreach (string line in instructions)
{
writeString(_spriteBatch,
font,
line,
new Vector2(_sWidth / 2.0f, (int)(500 * scalingAmount) + (instructSize * lineNum)), // 500 is the height of the title, this string goes right beneath that
1f,
Color.Red
);
lineNum++;
}
}

public void drawLoading(SpriteBatch _spriteBatch, Texture2D loadingSpin, float col)
{
if (loadingCol > 4)
Expand Down Expand Up @@ -339,6 +364,22 @@ public static List<string> wrapText(string desc, int lineLimit)
return lines;
}

public void writeString(SpriteBatch _spriteBatch, SpriteFont font, string str, Vector2 pos, float opacity, Color color)
{
Vector2 strSize = font.MeasureString(str);

_spriteBatch.DrawString(font,
str,
pos,
color,
0f,
new Vector2(strSize.X / 2, strSize.Y / 2),
(float)(1f * scalingAmount),
SpriteEffects.None,
0f
);
}

public void writeString(SpriteBatch _spriteBatch, SpriteFont font, string str, Vector2 pos, float opacity)
{
Vector2 strSize = font.MeasureString(str);
Expand Down