Skip to content

Commit

Permalink
Player name connecting screen (#65)
Browse files Browse the repository at this point in the history
* visual changes and starting on connecting.

* changes for connecting view

* Connecting Screen
  • Loading branch information
SatchelF authored Apr 10, 2024
1 parent b02cb87 commit 07ebd6c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 36 deletions.
4 changes: 3 additions & 1 deletion src/Client/ClientMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ protected override void Initialize()
{ MenuStateEnum.Help, new HelpView() },
{ MenuStateEnum.Credits, new AboutView() },
{ MenuStateEnum.ChooseName, new ChooseNameView(playerName)},
{ MenuStateEnum.HowToPlay, new HowToPlayView() }
{ MenuStateEnum.HowToPlay, new HowToPlayView() },
{ MenuStateEnum.Connecting, new ConnectingView() }

};

// Give all game states a chance to initialize, other than constructor
Expand Down
3 changes: 3 additions & 0 deletions src/Client/Menu/ChooseNameView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ public override void render(GameTime gameTime)
(m_graphics.PreferredBackBufferHeight - textSize.Y) / 2);

// Draw "Enter Your Name" text

m_spriteBatch.DrawString(font, enterNameText, textPosition, Colors.displayColor);


// Draw "Press Enter to proceed" below the name text if a name has been entered
if (playerName.Length > 0)
{
Expand All @@ -92,6 +94,7 @@ public override void render(GameTime gameTime)
(m_graphics.PreferredBackBufferWidth - proceedTextSize.X) / 2,
textPosition.Y + textSize.Y + 20); // 20 pixels below the name text


m_spriteBatch.DrawString(font, proceedText, proceedTextPosition, Colors.displayColor);
}

Expand Down
70 changes: 64 additions & 6 deletions src/Client/Menu/ConnectingView.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

namespace Client.Menu
{
internal class ConnectingView
public class ConnectingView : GameStateView
{
private SpriteFont font;
private string connectingMessage = "Connecting to Server";
private bool isConnected = false;
private double elapsedTimeSinceLastAttempt = 0;
private const double attemptInterval = 2000; // Attempt to connect every 2 seconds
private double periodUpdateTime = 500; // Update period for visual

public override void loadContent(ContentManager contentManager)
{
font = contentManager.Load<SpriteFont>("Fonts/menu");
}

public override MenuStateEnum processInput(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
{
return MenuStateEnum.MainMenu; // Or another state if cancelling connection attempt
}
if (isConnected)
{
return MenuStateEnum.GamePlay; // Transition to GamePlay upon successful connection
}
return MenuStateEnum.Connecting; // Stay on Connecting view if not yet connected
}

public override void update(GameTime gameTime)
{
elapsedTimeSinceLastAttempt += gameTime.ElapsedGameTime.TotalMilliseconds;

// Attempt to connect every 2 seconds
if (!isConnected && elapsedTimeSinceLastAttempt >= attemptInterval)
{
isConnected = connectToServer();
elapsedTimeSinceLastAttempt = 0; // Reset timer after each attempt
}
}

public override void render(GameTime gameTime)
{
m_spriteBatch.Begin();
Vector2 position = new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2);
Vector2 origin = font.MeasureString(connectingMessage) / 2;
m_spriteBatch.DrawString(font, connectingMessage, position - origin, Color.White);
m_spriteBatch.End();
}

private bool connectToServer()
{
// Attempt to connect to the server
return MessageQueueClient.instance.initialize("localhost", 3000);
}

public override void RegisterCommands()
{
throw new NotImplementedException();
}

}
}
56 changes: 28 additions & 28 deletions src/Client/Menu/GamePlayView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,36 @@ public override MenuStateEnum processInput(GameTime gameTime)
m_isKeyboardRegistered = true;
}

if (!m_isSetup)
{
setup(gameTime);
}
//if (!m_isSetup)
//{
// setup(gameTime);
//}

MenuKeyboardInput.Update(gameTime); // essentially just checking for whether we have escaped to the main menu
if (m_newState != MenuStateEnum.GamePlay){return handleSwitchToMainMenu();}
return MenuStateEnum.GamePlay;
}

private void setup(GameTime gameTime)
{
if (m_connectToServerTime == TimeSpan.Zero)
{
m_connectToServerTime = TimeSpan.FromSeconds(2); // Try to connect every 2 seconds
var res = connectToServer();
if (res)
{
m_isSetup = true;
}
}
else
{
m_connectToServerTime -= gameTime.ElapsedGameTime;
if (m_connectToServerTime <= TimeSpan.Zero)
{
m_connectToServerTime = TimeSpan.Zero;
}
}
}
//private void setup(GameTime gameTime)
//{
// if (m_connectToServerTime == TimeSpan.Zero)
// {
// m_connectToServerTime = TimeSpan.FromSeconds(2); // Try to connect every 2 seconds
// var res = connectToServer();
// if (res)
// {
// m_isSetup = true;
// }
// }
// else
// {
// m_connectToServerTime -= gameTime.ElapsedGameTime;
// if (m_connectToServerTime <= TimeSpan.Zero)
// {
// m_connectToServerTime = TimeSpan.Zero;
// }
// }
//}

public override void update(GameTime gameTime)
{
Expand All @@ -98,10 +98,10 @@ public override void RegisterCommands()
MenuKeyboardInput.registerCommand(MenuKeyboardInput.Escape, true, escape);
}

private bool connectToServer()
{
return MessageQueueClient.instance.initialize("localhost", 3000);
}
//private bool connectToServer()
//{
// return MessageQueueClient.instance.initialize("localhost", 3000);
//}

private void escape(GameTime gameTime, float scale)
{
Expand Down
6 changes: 5 additions & 1 deletion src/Client/Menu/HowToPlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override MenuStateEnum processInput(GameTime gameTime)
// Proceed to the next game state if the player presses Enter and enough time has passed since the last Enter press
if (newState.IsKeyDown(Keys.Enter) && oldState.IsKeyUp(Keys.Enter) && timeSinceLastEnterPress >= enterKeyDelay)
{
return MenuStateEnum.GamePlay; // Transition to the gameplay state
return MenuStateEnum.Connecting; // Transition to the gameplay state
}
// Update the enter released state
if (!newState.IsKeyDown(Keys.Enter))
Expand Down Expand Up @@ -67,21 +67,25 @@ public override void render(GameTime gameTime)
Vector2 titleOrigin = font.MeasureString(titleMessage) / 2;
m_spriteBatch.DrawString(font, titleMessage, titlePosition - (titleOrigin * textScale), Colors.displayColor, 0f, Vector2.Zero, textScale, SpriteEffects.None, 0f);


// How to Play Instructions
Vector2 instructionsPosition = new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2.5f);
string[] lines = howToPlayMessage.Split('\n');
foreach (string line in lines)
{
Vector2 lineSize = font.MeasureString(line) * textScale;

m_spriteBatch.DrawString(font, line, instructionsPosition - new Vector2(lineSize.X / 2, 0), Colors.displayColor, 0f, Vector2.Zero, textScale, SpriteEffects.None, 0f);
instructionsPosition.Y += lineSize.Y + 5; // Adjust spacing between lines if necessary, taking scale into account
}

// Continue Prompt
Vector2 continuePosition = new Vector2(m_graphics.PreferredBackBufferWidth / 2, (m_graphics.PreferredBackBufferHeight / 4) * 3);
Vector2 continueOrigin = font.MeasureString(continueMessage) / 2;

m_spriteBatch.DrawString(font, continueMessage, continuePosition - (continueOrigin * textScale), Colors.displayColor, 0f, Vector2.Zero, textScale, SpriteEffects.None, 0f);


m_spriteBatch.End();
}

Expand Down

0 comments on commit 07ebd6c

Please sign in to comment.