From 48b12e9eae4954cff5a6d1693a50430b891dd109 Mon Sep 17 00:00:00 2001 From: Maxwell Edwards <93385879+MaxEdwards20@users.noreply.github.com> Date: Tue, 9 Apr 2024 12:11:38 -0600 Subject: [PATCH] Name (#58) * Adds worm updates * Adds name * Updates readme * Adds name updating --- README.md | 1 - src/Client/ClientMain.cs | 8 +++++--- src/Client/GameModel.cs | 9 ++++++++- src/Client/Menu/ChooseNameView.cs | 23 +++++++++++------------ src/Client/Menu/GamePlayView.cs | 7 +++++-- src/Client/Systems/Network.cs | 9 ++++----- src/Server/GameModel.cs | 2 +- src/Shared/Messages/Join.cs | 30 +++++++++++++++++++++++------- 8 files changed, 57 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 290bfe2..db4139d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ A game of Snake built using C# in the MonoGame framework, themed around everyone - [ ] Caden: Map generation - [ ] Caden: Spice generation when we spawn - [ ] Caden: Periodic spice generation throughout the game -- [ ] Menu Screen to let player name themselves (probably similar to how control selection screen will work) - Satchel - [ ] Max: Grow the worm on eating food. ## Items to Develop diff --git a/src/Client/ClientMain.cs b/src/Client/ClientMain.cs index 4692b90..18c1380 100644 --- a/src/Client/ClientMain.cs +++ b/src/Client/ClientMain.cs @@ -9,6 +9,7 @@ using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Media; using System.Collections.Generic; +using System.Text; using Client.Components; using Client.Systems; using Shared.Components; @@ -29,6 +30,7 @@ public class ClientMain : Game private GameModel m_gameModel; private Controls m_controls; private ControlsPersistence m_ControlsPersistence; + private StringBuilder playerName = new StringBuilder(); public ClientMain() { @@ -36,7 +38,7 @@ public ClientMain() Content.RootDirectory = "Content"; m_menuKeyboardInput = new MenuKeyboardInput(); IsMouseVisible = true; - m_gameModel = new GameModel(); + m_gameModel = new GameModel(playerName); m_controls = new Controls(); m_ControlsPersistence = new ControlsPersistence(); } @@ -58,12 +60,12 @@ protected override void Initialize() m_states = new Dictionary { { MenuStateEnum.MainMenu, new MainMenuView() }, - { MenuStateEnum.GamePlay, new GamePlayView(m_controls) }, + { MenuStateEnum.GamePlay, new GamePlayView(m_controls, playerName) }, { MenuStateEnum.HighScores, new HighScoresView() }, { MenuStateEnum.Controls, new ControlSettingsView(m_controls) }, { MenuStateEnum.Help, new HelpView() }, { MenuStateEnum.Credits, new AboutView() }, - { MenuStateEnum.ChooseName, new ChooseNameView() }, + { MenuStateEnum.ChooseName, new ChooseNameView(playerName)}, { MenuStateEnum.HowToPlay, new HowToPlayView() } }; diff --git a/src/Client/GameModel.cs b/src/Client/GameModel.cs index b630a44..c482b25 100644 --- a/src/Client/GameModel.cs +++ b/src/Client/GameModel.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; +using System.Text; using System.Text.RegularExpressions; using Client.Components; using Shared.Components; @@ -28,6 +29,12 @@ public class GameModel private GraphicsDeviceManager m_graphics; private SpriteFont m_font; private Texture2D m_sand; + private String m_playerName; + + public GameModel(StringBuilder playerName) + { + m_playerName = playerName.ToString(); + } /// /// This is where everything performs its update. @@ -69,7 +76,7 @@ public bool initialize(ContentManager contentManager, Controls controls, Graphic m_renderer = new Systems.Renderer(m_systemCamera, graphics, m_font, m_sand); m_systemCollisionHandler = new Shared.Systems.CollisionHandler(); m_systemWormMovement = new Shared.Systems.WormMovement(); - m_systemNetwork = new Systems.Network(); + m_systemNetwork = new Systems.Network(m_playerName); m_systemNetwork.registerNewEntityHandler(handleNewEntity); m_systemNetwork.registerRemoveEntityHandler(handleRemoveEntity); diff --git a/src/Client/Menu/ChooseNameView.cs b/src/Client/Menu/ChooseNameView.cs index 2b47209..7074ed4 100644 --- a/src/Client/Menu/ChooseNameView.cs +++ b/src/Client/Menu/ChooseNameView.cs @@ -10,11 +10,17 @@ namespace Client.Menu { public class ChooseNameView : GameStateView { - private StringBuilder playerName = new StringBuilder(); + private StringBuilder playerName; private SpriteFont font; private KeyboardState oldState; private GameModel m_gameModel; + public ChooseNameView(StringBuilder name) + { + this.playerName = name; + oldState = Keyboard.GetState(); + } + public override void loadContent(ContentManager contentManager) { font = contentManager.Load("Fonts/menu"); @@ -22,23 +28,19 @@ public override void loadContent(ContentManager contentManager) 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(); + playerName.Clear(); // Clear the player name when Escape is pressed 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 @@ -47,10 +49,7 @@ public override MenuStateEnum processInput(GameTime gameTime) } 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; } diff --git a/src/Client/Menu/GamePlayView.cs b/src/Client/Menu/GamePlayView.cs index 46b88f7..a44c0aa 100644 --- a/src/Client/Menu/GamePlayView.cs +++ b/src/Client/Menu/GamePlayView.cs @@ -6,6 +6,7 @@ using Microsoft.Xna.Framework.Graphics; using Client.IO; using System.Runtime.Serialization; +using System.Text; using Client.Components; using Microsoft.Xna.Framework.Input; using Shared.Components; @@ -21,15 +22,17 @@ public class GamePlayView : GameStateView private GameModel m_gameModel; private TimeSpan m_connectToServerTime = TimeSpan.Zero; private Controls m_controls; + private StringBuilder playerName; - public GamePlayView(Controls controls) + public GamePlayView(Controls controls, StringBuilder name) { m_controls = controls; + playerName = name; } public override void initialize() { - m_gameModel = new GameModel(); + m_gameModel = new GameModel(playerName); m_gameModel.initialize(m_contentManager, m_controls, m_graphics); m_isSetup = false; m_isKeyboardRegistered = false; diff --git a/src/Client/Systems/Network.cs b/src/Client/Systems/Network.cs index 2ccf11e..014fa0e 100644 --- a/src/Client/Systems/Network.cs +++ b/src/Client/Systems/Network.cs @@ -29,13 +29,12 @@ public class Network : Shared.Systems.System /// Primary activity in the constructor is to setup the command map // that maps from message types to their handlers. /// - public Network() : + public Network(String playerName) : base(typeof(Shared.Components.Position)) { - registerHandler(Shared.Messages.Type.ConnectAck, (TimeSpan elapsedTime, Message message) => { - handleConnectAck(elapsedTime, (ConnectAck)message); + handleConnectAck(elapsedTime, (ConnectAck)message, playerName); }); registerHandler(Shared.Messages.Type.NewEntity, (TimeSpan elapsedTime, Message message) => @@ -153,9 +152,9 @@ public void registerRemoveEntityHandler(RemoveEntityHandler handler) /// assigned to it by the server, it also sends a request to the server /// to join the game. /// - private void handleConnectAck(TimeSpan elapsedTime, ConnectAck message) + private void handleConnectAck(TimeSpan elapsedTime, ConnectAck message, string name) { - MessageQueueClient.instance.sendMessage(new Join()); + MessageQueueClient.instance.sendMessage(new Join(name)); } /// diff --git a/src/Server/GameModel.cs b/src/Server/GameModel.cs index d1b2cd4..dc0297b 100644 --- a/src/Server/GameModel.cs +++ b/src/Server/GameModel.cs @@ -137,7 +137,7 @@ private void handleJoin(int clientId, Shared.Messages.Message message) { // Create a default name for the player var joinMessage = (Join)message; - string name = "Player" + clientId; + string name = joinMessage.name; // Step 1: Tell the newly connected player about all other entities reportAllEntities(clientId); diff --git a/src/Shared/Messages/Join.cs b/src/Shared/Messages/Join.cs index abdbe5d..8ccc219 100644 --- a/src/Shared/Messages/Join.cs +++ b/src/Shared/Messages/Join.cs @@ -27,11 +27,7 @@ public override byte[] serialize() { List data = new List(); data.AddRange(base.serialize()); - data.AddRange(BitConverter.GetBytes(hasName)); - if (hasName) - { - data.AddRange(Encoding.UTF8.GetBytes(name)); - } + serializeName(data); return data.ToArray(); } @@ -42,16 +38,36 @@ public override byte[] serialize() public override int parse(byte[] data) { int offset = base.parse(data); + offset = parseName(data, offset); + return offset; + } + + private void serializeName(List data) + { + data.AddRange(BitConverter.GetBytes(hasName)); + if (hasName) + { + data.AddRange(Encoding.UTF8.GetBytes(name)); + } + } + + private int parseName(byte[] data, int offset) + { this.hasName = BitConverter.ToBoolean(data, offset); offset += sizeof(bool); if (hasName) { - int nameSize = BitConverter.ToInt32(data, offset); - offset += sizeof(Int32); + // int nameSize = BitConverter.ToInt32(data, offset); + // offset += sizeof(Int32); + int nameSize = data.Length - offset; this.name = Encoding.UTF8.GetString(data, offset, nameSize); offset += nameSize; } return offset; } + + } + + }