Skip to content

Commit

Permalink
Adds name support and rendering
Browse files Browse the repository at this point in the history
* Fixes crash

* Adds name

* Adds value

* Swithc port

* Separates name parsing

* Adds new entity

* Adds values

* Adds draw player name

* Adds rendering order

* Adds player name rendering

* Removes unnessary files

* Updates the readme
  • Loading branch information
MaxEdwards20 authored Apr 3, 2024
1 parent 4d6ed05 commit 247f123
Show file tree
Hide file tree
Showing 18 changed files with 373 additions and 170 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,36 @@ A game of Snake built using C# in the MonoGame framework, themed around everyone

<!-- ## Project Description -->


## In Progress

- [ ] Satchel: Pick head, body, and tail texture for the sandworm
- [ ] Satchel: Keyboard vs. Mouse input menu screen - Satchel
- [ ] Caden: Map generation
- [ ] Max: Let player name themselves
- [ ] Max: Spice generation when we spawn

## Items to Develop

- [ ] Menu Screen to let player name themselves (probably similar to how control selection screen will work)
- [ ] 3 different animated sprites for the spice - Satchel
- [ ] Mouse input support - Satchel
- [ ] Mouse input support on Menu Screens
- [ ] Sound effects on death of worm and when food is eaten - Satchel
- [ ] Collision detection. Know whether we hit spice or another sandworm
- [ ] Keyboard vs. Mouse input menu screen - Satchel
- [ ] Spice generation that refreshes as we play the game
- [ ] Periodic spice generation throughout the game
- [ ] On collision, sandworm breaks apart and is available as food for other snakes
- [ ] Record players score, kills and highest position. Probably can be added to the `GameScores` object.
- [ ] Game over screen with score, kills, and highest position achieved
- [ ] Particle system for the death of a sandworm
- [ ] Upgrade our movement system to reduce the lag

- [ ] Upgrade our movement system to reduce the lag in rotation

## Done :)
## Done

- [x] Max: Add name support for the player
- [x] Max: Setup Snake Movement in the screen
- [x] Max: Decide how to build the snake. Tons of entities? Or one entity with a list of positions?
- [x] Max: Setup basic menuing
- [x] Caden: Camera movement
- [x] Satchel: Mouse Input Support


## Debugging

### Connection Refused
Expand Down
36 changes: 10 additions & 26 deletions src/Client/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class GameModel
private Systems.Renderer m_systemRenderer;
private Controls m_controls;
private GraphicsDeviceManager m_graphics;
private SpriteFont m_font;

/// <summary>
/// This is where everything performs its update.
Expand Down Expand Up @@ -53,11 +54,12 @@ public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch)
/// </summary>
public bool initialize(ContentManager contentManager, Controls controls, GraphicsDeviceManager graphics)
{
m_font = contentManager.Load<SpriteFont>("Fonts/menu");
m_contentManager = contentManager;
m_entities = new Dictionary<uint, Entity>();
m_systemInterpolation = new Systems.Interpolation();
m_systemCamera = new Systems.Camera(new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight));
m_systemRenderer = new Systems.Renderer(m_systemCamera, graphics);
m_systemRenderer = new Systems.Renderer(m_systemCamera, graphics, m_font);
m_systemNetwork = new Systems.Network();

m_systemNetwork.registerNewEntityHandler(handleNewEntity);
Expand Down Expand Up @@ -109,6 +111,11 @@ private Entity createEntity(Shared.Messages.NewEntity message)
entity.add(new Shared.Components.Input(message.inputs));
}

if (message.hasCollision)
{
entity.add(new Collision());
}

// Worm parts

if (message.hasHead)
Expand All @@ -131,9 +138,9 @@ private Entity createEntity(Shared.Messages.NewEntity message)
entity.add(new ChildId(message.childId));
}

if (message.collision)
if (message.hasName)
{
entity.add(new Collision());
entity.add(new Name(message.name));
}

return entity;
Expand Down Expand Up @@ -190,28 +197,5 @@ private void handleRemoveEntity(Shared.Messages.RemoveEntity message)
{
removeEntity(message.id);
}

private Color parseColor(string color)
{
// Pattern to extract the RGBA values from the string
var pattern = @"\{R:(\d+)\sG:(\d+)\sB:(\d+)\sA:(\d+)\}";
var match = Regex.Match(color, pattern);
if (match.Success)
{
// Extracting the RGBA values
int r = int.Parse(match.Groups[1].Value);
int g = int.Parse(match.Groups[2].Value);
int b = int.Parse(match.Groups[3].Value);
int a = int.Parse(match.Groups[4].Value);

// Creating a new Color object with the extracted values
return new Color(r, g, b, a);
}
else
{
// If the string does not match the pattern, return a default color
return Color.White;
}
}
}

6 changes: 3 additions & 3 deletions src/Client/Menu/ControlSettingsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public override void render(GameTime gameTime)
var rectangleSize = new Vector2(headerStringSize.X + horizontalOffset * 3, headerStringSize.Y * 6);
Drawing.DrawBlurredRectangle(m_spriteBatch, rectangleOrigin, rectangleSize, 5);

Drawing.DrawShadedString(m_font, MESSAGE, new Vector2(halfWidth, halfHeight - headerStringSize.Y), Colors.displayColor, m_spriteBatch);
Drawing.DrawShadedString(m_font, "Move Left " + m_controls.SnakeLeft.key, new Vector2(halfWidth, halfHeight - headerStringSize.Y + 2 + 50), getStringColor(ControlStateEnum.SnakeLeft), m_spriteBatch);
Drawing.DrawShadedString(m_font, "Move Right " + m_controls.SnakeRight.key, new Vector2(halfWidth, halfHeight - headerStringSize.Y + 2 + 100), getStringColor(ControlStateEnum.SnakeRight), m_spriteBatch);
Drawing.CustomDrawString(m_font, MESSAGE, new Vector2(halfWidth, halfHeight - headerStringSize.Y), Colors.displayColor, m_spriteBatch);
Drawing.CustomDrawString(m_font, "Move Left " + m_controls.SnakeLeft.key, new Vector2(halfWidth, halfHeight - headerStringSize.Y + 2 + 50), getStringColor(ControlStateEnum.SnakeLeft), m_spriteBatch);
Drawing.CustomDrawString(m_font, "Move Right " + m_controls.SnakeRight.key, new Vector2(halfWidth, halfHeight - headerStringSize.Y + 2 + 100), getStringColor(ControlStateEnum.SnakeRight), m_spriteBatch);
m_spriteBatch.End();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Client/Menu/CreditsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override MenuStateEnum processInput(GameTime gameTime)
public override void render(GameTime gameTime)
{
m_spriteBatch.Begin();
Drawing.DrawShadedString(m_font, MESSAGE, new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2), Colors.displayColor ,m_spriteBatch);
Drawing.CustomDrawString(m_font, MESSAGE, new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2), Colors.displayColor ,m_spriteBatch);
m_spriteBatch.End();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Client/Menu/GamePlayView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public override void RegisterCommands()

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

private void escape(GameTime gameTime, float scale)
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Menu/HelpView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override MenuStateEnum processInput(GameTime gameTime)
public override void render(GameTime gameTime)
{
m_spriteBatch.Begin();
Drawing.DrawShadedString(m_font, MESSAGE, new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2), Colors.displayColor ,m_spriteBatch, boxed: true);
Drawing.CustomDrawString(m_font, MESSAGE, new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2), Colors.displayColor ,m_spriteBatch, boxed: true);
m_spriteBatch.End();
}

Expand Down
6 changes: 3 additions & 3 deletions src/Client/Menu/HighScoresView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override void update(GameTime gameTime)
public override void render(GameTime gameTime)
{
m_spriteBatch.Begin();
Drawing.DrawShadedString(m_font, MESSAGE, new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 4), Colors.displayColor ,m_spriteBatch);
Drawing.CustomDrawString(m_font, MESSAGE, new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 4), Colors.displayColor ,m_spriteBatch);
m_spriteBatch.End();
renderScores();
}
Expand All @@ -85,14 +85,14 @@ private void renderScores() {
float y = -100;
if (highScores.scores == null || highScores.scores.Count == 0)
{
Drawing.DrawShadedString(m_font, "No Scores Yet", new Vector2(halfWidth, halfHeight), Colors.displayColor, m_spriteBatch);
Drawing.CustomDrawString(m_font, "No Scores Yet", new Vector2(halfWidth, halfHeight), Colors.displayColor, m_spriteBatch);
}
else
{
foreach (var score in highScores.scores.Take(numDisplayScores))
{
y += 100;
Drawing.DrawShadedString(m_font, score.score.ToString(), new Vector2(halfWidth, halfHeight + y), Colors.displayColor, m_spriteBatch);
Drawing.CustomDrawString(m_font, score.score.ToString(), new Vector2(halfWidth, halfHeight + y), Colors.displayColor, m_spriteBatch);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Client/Menu/MainMenuView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public override void render(GameTime gameTime)
private float drawMenuItem(SpriteFont font, string text, float y, Color color)
{
Vector2 stringSize = font.MeasureString(text);
Drawing.DrawShadedString(font, text, new Vector2(m_graphics.PreferredBackBufferWidth / 2, y), color, m_spriteBatch);
Drawing.CustomDrawString(font, text, new Vector2(m_graphics.PreferredBackBufferWidth / 2, y), color, m_spriteBatch);
return y + stringSize.Y;
}

Expand Down
14 changes: 9 additions & 5 deletions src/Client/Menu/Styles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ public class Colors {
}

public class Drawing {
public static void DrawShadedString(SpriteFont font, string message, Vector2 position, Color color, SpriteBatch spriteBatch, bool centered = true, bool boxed = false, bool shaded = true) {
public static void CustomDrawString(SpriteFont font, string message, Vector2 position, Color color, SpriteBatch spriteBatch, bool centered = true, bool boxed = false, bool shaded = true, float scale = 1.0f) {
if (centered) {
position = new Vector2(position.X - font.MeasureString(message).X / 2, position.Y - font.MeasureString(message).Y / 2);
}
if (boxed) {
DrawBlurredRectangle(spriteBatch, position, font.MeasureString(message), 5, 0.6f);
}
if (shaded) {
for (int i = 1; i < 3; i++) {
spriteBatch.DrawString(font, message, new Vector2(position.X - i, position.Y - i), Color.Black);
for (int i = 1; i < 3; i++) {
spriteBatch.DrawString(font, message, new Vector2(position.X - i, position.Y - i), Color.Black, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
}
}
spriteBatch.DrawString(font, message,
position, color);
spriteBatch.DrawString(font, message, position, color, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
}

public static void DrawBlurredRectangle(SpriteBatch spriteBatch, Vector2 position, Vector2 size, int blurRadius, float transparency = 0.8f)
Expand All @@ -38,6 +37,11 @@ private static void DrawRectangle(SpriteBatch spriteBatch, Rectangle rectangle,
dummyTexture.SetData(new Color[] { color });
spriteBatch.Draw(dummyTexture, rectangle, color);
}

public static void DrawPlayerName(SpriteFont font, string name, Vector2 position, Color color, SpriteBatch spriteBatch) {
// We need to downsize the font for the player name
CustomDrawString(font, name, position, color, spriteBatch, false, false, false, scale:0.4f);
}

}
}
46 changes: 45 additions & 1 deletion src/Client/Systems/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
using Shared.Components.Appearance;

using System;
using System.Collections.Generic;
using Client.Menu;

namespace Client.Systems;

public class Renderer : Shared.Systems.System
{
private Systems.Camera m_camera;
private GraphicsDeviceManager m_graphics;
private SpriteFont m_font;

public Renderer(Systems.Camera camera, GraphicsDeviceManager graphics) :
public Renderer(Systems.Camera camera, GraphicsDeviceManager graphics, SpriteFont font) :
base(
typeof(Client.Components.Sprite),
typeof(Shared.Components.Position),
Expand All @@ -24,6 +27,7 @@ public Renderer(Systems.Camera camera, GraphicsDeviceManager graphics) :
{
m_camera = camera;
m_graphics = graphics;
m_font = font;
}

public override void update(TimeSpan elapsedTime) { }
Expand All @@ -42,8 +46,40 @@ public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch)

spriteBatch.Begin(transformMatrix: matrix);
// TODO: Adjust this to render all of the tails first, then body segments, then heads
var heads = new List<Entity>();
var bodies = new List<Entity>();
var tails = new List<Entity>();
var others = new List<Entity>();

foreach (Entity entity in m_entities.Values)
{
if (entity.contains<Head>())
heads.Add(entity);
else if (entity.contains<Tail>())
tails.Add(entity);
else if (entity.contains<ParentId>()) // The body has these
bodies.Add(entity);
else
others.Add(entity);
}

foreach (Entity entity in others)
{
renderEntity(elapsedTime, spriteBatch, entity);
}
foreach (Entity entity in tails)
{
renderEntity(elapsedTime, spriteBatch, entity);
}
foreach (Entity entity in bodies)
{
renderEntity(elapsedTime, spriteBatch, entity);
}
foreach (Entity entity in heads)
{
renderEntity(elapsedTime, spriteBatch, entity);
}

spriteBatch.End();
}

Expand Down Expand Up @@ -71,6 +107,14 @@ private void renderEntity(TimeSpan elapsedTime, SpriteBatch spriteBatch, Entity
texCenter,
SpriteEffects.None,
0);

if (entity.contains<Name>())
{
// We want the name position to be above the entity
Vector2 namePosition = new Vector2(position.X - size.X + 10, position.Y - size.Y - 10);
Drawing.DrawPlayerName(m_font, entity.get<Name>().name, namePosition, Color.White, spriteBatch);
}

}

}
13 changes: 7 additions & 6 deletions src/Server/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,20 @@ private void reportAllEntities(int clientId)
/// added to the server game model, and notifies the requesting client
/// of the player.
/// </summary>
private void handleJoin(int clientId)
private void handleJoin(int clientId, Shared.Messages.Message message)
{
// Create a default name for the player
var joinMessage = (Join)message;
string name = "Player" + clientId;
// Step 1: Tell the newly connected player about all other entities
reportAllEntities(clientId);

// Step 2: Create a new wormHead for the newly joined player and sent it
// to the newly joined client
createNewWorm(clientId);

createNewWorm(clientId, name);
}

private void createNewWorm(int clientId)
private void createNewWorm(int clientId, string name)
{
var startLocation = new Vector2(200, 200);
var rotationRate = (float) Math.PI / 1000;
Expand All @@ -138,7 +140,7 @@ private void createNewWorm(int clientId)
var bodySize = 80;

// Create the head
Entity player = WormHead.create(startLocation, 100, moveRate, rotationRate);
Entity player = WormHead.create(startLocation, 100, moveRate, rotationRate, name);

// Create a body segment
Entity segment = WormSegment.create( new Vector2(startLocation.X + 75, startLocation.Y - 20) , bodySize, moveRate, rotationRate, player.id);
Expand All @@ -156,7 +158,6 @@ private void createNewWorm(int clientId)
m_clientToEntityId[clientId] = segment.id;
m_clientToEntityId[clientId] = tail.id;


// Step 3: Send the new player entity to the newly joined client
MessageQueueServer.instance.sendMessage(clientId, new NewEntity(player));
MessageQueueServer.instance.sendMessage(clientId, new NewEntity(segment));
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Server": {
"commandName": "Project",
"commandLineArgs": "--port 4000"
"commandLineArgs": "--port 3050"
}
}
}
4 changes: 2 additions & 2 deletions src/Server/Systems/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Server.Systems
public class Network : Shared.Systems.System
{
public delegate void Handler(int clientId, TimeSpan elapsedTime, Shared.Messages.Message message);
public delegate void JoinHandler(int clientId);
public delegate void JoinHandler(int clientId, Shared.Messages.Message message);
public delegate void DisconnectHandler(int clientId);
public delegate void InputHandler(Entity entity, Shared.Components.Input.Type type, TimeSpan elapsedTime);

Expand All @@ -31,7 +31,7 @@ public Network() :
{
if (m_joinHandler != null)
{
m_joinHandler(clientId);
m_joinHandler(clientId, message);
}
});

Expand Down
Loading

0 comments on commit 247f123

Please sign in to comment.