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

Satchel input screen #39

Merged
merged 4 commits into from
Apr 7, 2024
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
4 changes: 3 additions & 1 deletion src/Client/ClientMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ protected override void Initialize()
{ MenuStateEnum.HighScores, new HighScoresView() },
{ MenuStateEnum.Controls, new ControlSettingsView(m_controls) },
{ MenuStateEnum.Help, new HelpView() },
{ MenuStateEnum.Credits, new AboutView() }
{ MenuStateEnum.Credits, new AboutView() },
{ MenuStateEnum.ChooseName, new ChooseNameView() },
{ MenuStateEnum.HowToPlay, new HowToPlayView() }
};

// Give all game states a chance to initialize, other than constructor
Expand Down
130 changes: 130 additions & 0 deletions src/Client/Menu/ChooseNameView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using Client.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Text;
using static System.Formats.Asn1.AsnWriter;

namespace Client.Menu
{
public class ChooseNameView : GameStateView
{
private StringBuilder playerName = new StringBuilder();
private SpriteFont font;
private KeyboardState oldState;
private GameModel m_gameModel;

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

public override MenuStateEnum processInput(GameTime gameTime)
{
KeyboardState newState = Keyboard.GetState(); // Get the new state



// Simple example for input handling
foreach (var key in newState.GetPressedKeys())
{


// Check for Escape key press to return to MainMenu
if (newState.IsKeyDown(Keys.Escape))
{
playerName.Clear();
return MenuStateEnum.MainMenu; // Immediately return to MainMenu when Escape is pressed
}


if (!oldState.IsKeyDown(key)) // Only take action if the key was not pressed before
{
if (key == Keys.Back && playerName.Length > 0) // Handle backspace
{
playerName.Remove(playerName.Length - 1, 1);
}
else if (key == Keys.Enter && playerName.Length > 0) // Confirm with Enter key
{


// TODO SET PLAYER NAME

// Transition to the next state (e.g., HowToPlay)
return MenuStateEnum.HowToPlay;
}
else
{
char keyChar = GetCharFromKey(key);
if (keyChar != '\0')
{
playerName.Append(keyChar);
}
}
}
}

oldState = newState; // Set the old state to the new state for the next frame

return MenuStateEnum.ChooseName;
}

public override void render(GameTime gameTime)
{
m_spriteBatch.Begin();

// Calculate the center position for "Enter Your Name" text
string enterNameText = $"Enter Your Name: {playerName}";
Vector2 textSize = font.MeasureString(enterNameText);
Vector2 textPosition = new Vector2(
(m_graphics.PreferredBackBufferWidth - textSize.X) / 2,
(m_graphics.PreferredBackBufferHeight - textSize.Y) / 2);

// Draw "Enter Your Name" text
m_spriteBatch.DrawString(font, enterNameText, textPosition, Color.White);

// Draw "Press Enter to proceed" below the name text if a name has been entered
if (playerName.Length > 0)
{
string proceedText = "Press Enter to proceed";
Vector2 proceedTextSize = font.MeasureString(proceedText);
Vector2 proceedTextPosition = new Vector2(
(m_graphics.PreferredBackBufferWidth - proceedTextSize.X) / 2,
textPosition.Y + textSize.Y + 20); // 20 pixels below the name text

m_spriteBatch.DrawString(font, proceedText, proceedTextPosition, Color.White);
}

m_spriteBatch.End();
}


// Implement this method based on your needs and localization requirements
private char GetCharFromKey(Keys key)
{
// This is a simplified way to convert Keys to char. You might want to expand this method
// to handle different cases (e.g., upper/lowercase, special characters) depending on the keyboard layout and CapsLock state.
if (key >= Keys.A && key <= Keys.Z)
{
return (char)key;
}

return '\0'; // Return a null character for keys that don't map directly
}

public override void RegisterCommands()
{

}



public override void update(GameTime gameTime)
{
// Implement any updating logic for this view, if necessary.
// If there's nothing to update, you can leave it empty.
}

}
}
1 change: 0 additions & 1 deletion src/Client/Menu/ControlSettingsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public override void update(GameTime gameTime)
}

// TODO: Add a pill toggle to change whether we are in keyboard or mouse mode
// TODO: Add a menu mouse input system to handle the mouse input

}
public override void render(GameTime gameTime)
Expand Down
83 changes: 83 additions & 0 deletions src/Client/Menu/HowToPlay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Client.Menu
{
public class HowToPlayView : GameStateView
{
private SpriteFont font;
private string howToPlayMessage = "The snake will change direction based on keypress";
private string titleMessage = "HOW TO PLAY";
private string continueMessage = "Press Enter to continue";
private KeyboardState oldState;
private bool isEnterReleased;
private bool isKeyboardRegistered = false;
private double enterKeyDelay = 500; // 500 milliseconds delay
private double timeSinceLastEnterPress;

// ... other members ...

public override void loadContent(ContentManager contentManager)
{
font = contentManager.Load<SpriteFont>("Fonts/menu");
oldState = Keyboard.GetState(); // Initialize the old state
timeSinceLastEnterPress = 0; // Initialize the timer
}

public override MenuStateEnum processInput(GameTime gameTime)
{
KeyboardState newState = Keyboard.GetState();

timeSinceLastEnterPress += gameTime.ElapsedGameTime.TotalMilliseconds;

// 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
}
// Update the enter released state
if (!newState.IsKeyDown(Keys.Enter))
{
isEnterReleased = true;
}

oldState = newState; // Update the old keyboard state
return MenuStateEnum.HowToPlay;
}

public override void update(GameTime gameTime)
{
// No updating logic needed for static view
}

public override void render(GameTime gameTime)
{
m_spriteBatch.Begin();

// Title
Vector2 titlePosition = new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 4);
Vector2 titleOrigin = font.MeasureString(titleMessage) / 2;
m_spriteBatch.DrawString(font, titleMessage, titlePosition - titleOrigin, Color.White);

// How to Play Instructions
Vector2 instructionsPosition = new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2);
Vector2 instructionsOrigin = font.MeasureString(howToPlayMessage) / 2;
m_spriteBatch.DrawString(font, howToPlayMessage, instructionsPosition - instructionsOrigin, Color.White);

// 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, Color.White);

m_spriteBatch.End();
}

public override void RegisterCommands()
{
// If you have commands to register, such as an Exit command, register them here
isKeyboardRegistered = true;
}
}
}
2 changes: 1 addition & 1 deletion src/Client/Menu/MainMenuView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void Select(GameTime gameTime, float scale)
{
if (m_currentSelection == MenuState.NewGame)
{
newState = MenuStateEnum.GamePlay;
newState = MenuStateEnum.ChooseName;
}
if (m_currentSelection == MenuState.HighScores)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Client/Menu/MenuStateEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public enum MenuStateEnum
{
MainMenu,
GamePlay,
ChooseName,
HowToPlay,
HighScores,
Controls,
Help,
Expand Down
Loading