Skip to content

Commit

Permalink
Name (#58)
Browse files Browse the repository at this point in the history
* Adds worm updates

* Adds name

* Updates readme

* Adds name updating
  • Loading branch information
MaxEdwards20 authored Apr 9, 2024
1 parent 55341d8 commit 48b12e9
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 32 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/Client/ClientMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,14 +30,15 @@ public class ClientMain : Game
private GameModel m_gameModel;
private Controls m_controls;
private ControlsPersistence m_ControlsPersistence;
private StringBuilder playerName = new StringBuilder();

public ClientMain()
{
m_graphics = new GraphicsDeviceManager(this);
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();
}
Expand All @@ -58,12 +60,12 @@ protected override void Initialize()
m_states = new Dictionary<MenuStateEnum, IGameState>
{
{ 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() }
};

Expand Down
9 changes: 8 additions & 1 deletion src/Client/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Client.Components;
using Shared.Components;
Expand All @@ -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();
}

/// <summary>
/// This is where everything performs its update.
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 11 additions & 12 deletions src/Client/Menu/ChooseNameView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,37 @@ 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<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();
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
Expand All @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Client/Menu/GamePlayView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions src/Client/Systems/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
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) =>
Expand Down Expand Up @@ -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.
/// </summary>
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));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Server/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
30 changes: 23 additions & 7 deletions src/Shared/Messages/Join.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ public override byte[] serialize()
{
List<byte> data = new List<byte>();
data.AddRange(base.serialize());
data.AddRange(BitConverter.GetBytes(hasName));
if (hasName)
{
data.AddRange(Encoding.UTF8.GetBytes(name));
}
serializeName(data);
return data.ToArray();
}

Expand All @@ -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<byte> 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;
}


}


}

0 comments on commit 48b12e9

Please sign in to comment.